Update LikeController, hide like counts

pull/2746/head
Daniel Supernault 2021-05-01 16:01:32 -06:00
rodzic 477db75896
commit ea68724054
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 0DEF1C662C9033F7
1 zmienionych plików z 49 dodań i 49 usunięć

Wyświetl plik

@ -13,60 +13,60 @@ use App\Services\StatusService;
class LikeController extends Controller class LikeController extends Controller
{ {
public function __construct() public function __construct()
{ {
$this->middleware('auth'); $this->middleware('auth');
} }
public function store(Request $request) public function store(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'item' => 'required|integer|min:1', 'item' => 'required|integer|min:1',
]); ]);
$user = Auth::user(); $user = Auth::user();
$profile = $user->profile; $profile = $user->profile;
$status = Status::findOrFail($request->input('item')); $status = Status::findOrFail($request->input('item'));
$count = $status->likes()->count(); $count = $status->likes()->count();
if ($status->likes()->whereProfileId($profile->id)->count() !== 0) { if ($status->likes()->whereProfileId($profile->id)->count() !== 0) {
$like = Like::whereProfileId($profile->id)->whereStatusId($status->id)->firstOrFail(); $like = Like::whereProfileId($profile->id)->whereStatusId($status->id)->firstOrFail();
$like->forceDelete(); $like->forceDelete();
$count--; $count--;
$status->likes_count = $count; $status->likes_count = $count;
$status->save(); $status->save();
} else { } else {
$like = Like::firstOrCreate([ $like = Like::firstOrCreate([
'profile_id' => $user->profile_id, 'profile_id' => $user->profile_id,
'status_id' => $status->id 'status_id' => $status->id
]); ]);
if($like->wasRecentlyCreated == true) { if($like->wasRecentlyCreated == true) {
$count++; $count++;
$status->likes_count = $count; $status->likes_count = $count;
$like->status_profile_id = $status->profile_id; $like->status_profile_id = $status->profile_id;
$like->is_comment = in_array($status->type, [ $like->is_comment = in_array($status->type, [
'photo', 'photo',
'photo:album', 'photo:album',
'video', 'video',
'video:album', 'video:album',
'photo:video:album' 'photo:video:album'
]) == false; ]) == false;
$like->save(); $like->save();
$status->save(); $status->save();
LikePipeline::dispatch($like); LikePipeline::dispatch($like);
} }
} }
Cache::forget('status:'.$status->id.':likedby:userid:'.$user->id); Cache::forget('status:'.$status->id.':likedby:userid:'.$user->id);
StatusService::del($status->id); StatusService::del($status->id);
if ($request->ajax()) { if ($request->ajax()) {
$response = ['code' => 200, 'msg' => 'Like saved', 'count' => $count]; $response = ['code' => 200, 'msg' => 'Like saved', 'count' => 0];
} else { } else {
$response = redirect($status->url()); $response = redirect($status->url());
} }
return $response; return $response;
} }
} }