2018-04-17 01:35:01 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2018-05-27 02:26:44 +00:00
|
|
|
use App\Jobs\LikePipeline\LikePipeline;
|
2021-07-21 09:41:28 +00:00
|
|
|
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;
|
2021-01-30 23:25:50 +00:00
|
|
|
use App\Services\StatusService;
|
2018-04-17 01:35:01 +00:00
|
|
|
|
|
|
|
class LikeController extends Controller
|
|
|
|
{
|
2021-05-01 22:01:32 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware('auth');
|
|
|
|
}
|
2018-05-27 02:26:44 +00:00
|
|
|
|
2021-05-01 22:01:32 +00:00
|
|
|
public function store(Request $request)
|
|
|
|
{
|
|
|
|
$this->validate($request, [
|
|
|
|
'item' => 'required|integer|min:1',
|
|
|
|
]);
|
2018-05-26 22:59:31 +00:00
|
|
|
|
2023-10-31 03:16:28 +00:00
|
|
|
abort(422, 'Deprecated API Endpoint');
|
2022-08-08 02:59:36 +00:00
|
|
|
|
2021-05-01 22:01:32 +00:00
|
|
|
$user = Auth::user();
|
|
|
|
$profile = $user->profile;
|
|
|
|
$status = Status::findOrFail($request->input('item'));
|
2018-08-28 03:07:36 +00:00
|
|
|
|
2021-07-25 09:19:48 +00:00
|
|
|
if (Like::whereStatusId($status->id)->whereProfileId($profile->id)->exists()) {
|
2021-05-01 22:01:32 +00:00
|
|
|
$like = Like::whereProfileId($profile->id)->whereStatusId($status->id)->firstOrFail();
|
2023-10-31 03:16:28 +00:00
|
|
|
UnlikePipeline::dispatch($like)->onQueue('feed');
|
2021-05-01 22:01:32 +00:00
|
|
|
} else {
|
2022-05-09 06:33:01 +00:00
|
|
|
abort_if(
|
|
|
|
Like::whereProfileId($user->profile_id)
|
|
|
|
->where('created_at', '>', now()->subDay())
|
|
|
|
->count() >= Like::MAX_PER_DAY,
|
|
|
|
429
|
|
|
|
);
|
2021-07-21 09:41:28 +00:00
|
|
|
$count = $status->likes_count > 4 ? $status->likes_count : $status->likes()->count();
|
2021-05-01 22:01:32 +00:00
|
|
|
$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();
|
2023-10-31 03:16:28 +00:00
|
|
|
LikePipeline::dispatch($like)->onQueue('feed');
|
2021-05-01 22:01:32 +00:00
|
|
|
}
|
|
|
|
}
|
2018-08-28 03:07:36 +00:00
|
|
|
|
2021-05-01 22:01:32 +00:00
|
|
|
Cache::forget('status:'.$status->id.':likedby:userid:'.$user->id);
|
2021-10-22 01:02:15 +00:00
|
|
|
StatusService::refresh($status->id);
|
2019-06-18 06:47:57 +00:00
|
|
|
|
2021-05-01 22:01:32 +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
|
|
|
|
2021-05-01 22:01:32 +00:00
|
|
|
return $response;
|
|
|
|
}
|
2018-04-17 01:35:01 +00:00
|
|
|
}
|