创建黑名单表迁移:php artisan make:model Models/BlackFeeds -m (生成模型和迁移文件)
迁移文件中创建如下字段:
public function up()
{
if (!Schema::hasTable('black_feeds')) {
Schema::create('black_feeds', function (Blueprint $table) {
$table->integer('user_id')->unsigned()->comment('main user');
$table->integer('target_id')->unsigned()->comment('blacked user id');
$table->timestamps();
$table->index('user_id');
});
}
DB::statement("alter table `black_feeds` comment'拉黑动态用户表'");//表注释
}
加入黑名单:用户表(users)
/*
* 动态加入黑名单.
* @Author Wayne
* @DateTime 2018-04-17
* @Email [email protected]
* @param Request $request [description]
* @param UserModel $user [description]
* @return [type] [description]
*/
public function black(Request $request, UserModel $targetUser, UserBlackFeedModel $blackList)
{
$target_id = $targetUser->id;
$user_id = $request->user()->id;
if ($target_id === $user_id) {
return response()->json(['message' => '不能将自己加入黑名单'], 422);
}
$record = $blackList->where(['user_id' => $user_id, 'target_id' => $target_id])
->first();
if (! $record) {
$record = new UserBlackFeedModel();
$record->user_id = $user_id;
$record->target_id = $target_id;
} $record->save();
$cacheKey = sprintf('user-feeded:%s,%s', $target_id, $user_id);
Cache::forever($cacheKey, true); return response()->json(['message' => '操作成功'], 201);
}
移出黑名单:
/**
* 动态移出黑名单.
* @Author Wayne
* @DateTime 2018-04-17
* @Email [email protected]
* @param Request $request [description]
* @param UserModel $user [description]
* @return [type] [description]
*/
public function unBlack(Request $request, UserModel $targetUser, UserBlackFeedModel $blackList)
{
$target_id = $targetUser->id;
$user_id = $request->user()->id;
if ($target_id === $user_id) {
return response()->json(['message' => '不能对自己进行操作'], 422);
}
$blackList->where(['user_id' => $user_id, 'target_id' => $target_id])
->delete();
$cacheKey = sprintf('user-feeded:%s,%s', $target_id, $user_id);
Cache::forget($cacheKey); return response()->json('', 204);
}
黑名单列表:
public function blackList(Request $request)
{
$user = $request->user();
$offset = $request->input('offset', 0);
$limit = $request->input('limit', 15);
$blacks = $user->blackfeeds()
->with('user')
->latest()
->limit($limit)
->offset($offset)
->get(); $blacks = $blacks->map(function ($black) use ($user) {
$black->user->blackfeeded = true; return $black->user;
}); return response()->json($blacks, 200);
}
用户(User)模型中use以下:
trait UserHasBlackFeeds:
<?php
declare(strict_types=1); namespace Zhiyi\Plus\Models\Relations; use Zhiyi\Plus\Models\User;
use Zhiyi\Plus\Models\BlackFeeds;
use Illuminate\Support\Facades\Cache; trait UserHasBlackFeeds
{
/**
* get blacklists of current user.
* @Email [email protected]
* @return [type] [description]
*/
public function blackfeeds()
{
return $this->hasMany(BlackFeeds::class, 'user_id', 'id');
} /**
* is user blacked by current_user.
* @Email [email protected]
* @param [type] $user [description]
* @return [type] [description]
*/
public function blackfeeded($user): bool
{
if ($user instanceof User) {
$user = $user->id;
} $cacheKey = sprintf('user-feeded:%s,%s', $user, $this->id);
if (Cache::has($cacheKey)) {
return Cache::get($cacheKey);
} $status = $this->blackfeeds()
->where('target_id', $user)
->first() !== null;
Cache::forever($cacheKey, $status); return $status;
}
}
比如现在如果想查动态时不显示黑名单中用户的动态就可以如此查询:(动态表Feed)
动态表中增加关联如下:(user_id发布动态用户id)
public function blackfeeds()
{
return $this->hasMany(BlackFeeds::class, 'target_id', 'user_id');
}
动态列表查询(屏蔽黑名单中用户):
$limit = $request->query('limit', 10);
$after = $request->query('after');
if (is_null($user = $request->user('api'))) {
abort(401);
}
$user_id=$request->user('api')->id;
$feedModel->whereDoesntHave('blackfeeds', function ($query) use ($user_id) {
return $query->where('user_id', $user_id);
})
->orderBy('id', 'desc')
->when((bool) $after, function ($query) use ($after) {
return $query->where('id', '<', $after);
})
->limit($limit)
->get();