pixelfed/app/Http/Controllers/LikeController.php

70 lines
1.6 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use App\Jobs\LikePipeline\LikePipeline;
use App\Jobs\LikePipeline\UnlikePipeline;
2018-08-28 03:07:36 +00:00
use App\Like;
use App\Status;
use App\User;
use Auth;
use Cache;
use Illuminate\Http\Request;
use App\Services\StatusService;
class LikeController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function store(Request $request)
{
$this->validate($request, [
'item' => 'required|integer|min:1',
]);
2018-05-26 22:59:31 +00:00
$user = Auth::user();
$profile = $user->profile;
$status = Status::findOrFail($request->input('item'));
2018-08-28 03:07:36 +00:00
if (Like::whereStatusId($status->id)->whereProfileId($profile->id)->exists()) {
$like = Like::whereProfileId($profile->id)->whereStatusId($status->id)->firstOrFail();
UnlikePipeline::dispatch($like);
} else {
$count = $status->likes_count > 4 ? $status->likes_count : $status->likes()->count();
$like = Like::firstOrCreate([
'profile_id' => $user->profile_id,
'status_id' => $status->id
]);
if($like->wasRecentlyCreated == true) {
$count++;
$status->likes_count = $count;
$like->status_profile_id = $status->profile_id;
$like->is_comment = in_array($status->type, [
'photo',
'photo:album',
'video',
'video:album',
'photo:video:album'
]) == false;
$like->save();
$status->save();
LikePipeline::dispatch($like);
}
}
2018-08-28 03:07:36 +00:00
Cache::forget('status:'.$status->id.':likedby:userid:'.$user->id);
StatusService::refresh($status->id);
2019-06-18 06:47:57 +00:00
if ($request->ajax()) {
$response = ['code' => 200, 'msg' => 'Like saved', 'count' => 0];
} else {
$response = redirect($status->url());
}
2018-06-04 02:46:55 +00:00
return $response;
}
}