diff --git a/app/Http/Controllers/LikeController.php b/app/Http/Controllers/LikeController.php index 2b87353d..08e0a421 100644 --- a/app/Http/Controllers/LikeController.php +++ b/app/Http/Controllers/LikeController.php @@ -20,10 +20,11 @@ class LikeController extends Controller public function store(Request $request) { $this->validate($request, [ - 'item' => 'required|integer|min:1', - ]); + 'item' => 'required|integer|min:1', + ]); - $profile = Auth::user()->profile; + $user = Auth::user(); + $profile = $user->profile; $status = Status::withCount('likes')->findOrFail($request->input('item')); $count = $status->likes_count; @@ -32,19 +33,24 @@ class LikeController extends Controller $like = Like::whereProfileId($profile->id)->whereStatusId($status->id)->firstOrFail(); $like->forceDelete(); $count--; + if($count >= 0) { + $status->likes_count = $count; + $status->save(); + } } else { $like = new Like(); $like->profile_id = $profile->id; $like->status_id = $status->id; $like->save(); $count++; + if($count >= 0) { + $status->likes_count = $count; + $status->save(); + } LikePipeline::dispatch($like); } - $likes = Like::whereProfileId($profile->id) - ->orderBy('id', 'desc') - ->take(1000) - ->pluck('status_id'); + Cache::forget('status:'.$status->id.':likedby:userid:'.$user->id); if ($request->ajax()) { $response = ['code' => 200, 'msg' => 'Like saved', 'count' => $count]; diff --git a/app/Http/Controllers/StatusController.php b/app/Http/Controllers/StatusController.php index fe2ac646..136c3ba5 100644 --- a/app/Http/Controllers/StatusController.php +++ b/app/Http/Controllers/StatusController.php @@ -234,7 +234,8 @@ class StatusController extends Controller 'item' => 'required|integer|min:1', ]); - $profile = Auth::user()->profile; + $user = Auth::user(); + $profile = $user->profile; $status = Status::withCount('shares')->findOrFail($request->input('item')); $count = $status->shares_count; @@ -259,6 +260,13 @@ class StatusController extends Controller $count++; SharePipeline::dispatch($share); } + + if($count >= 0) { + $status->reblogs_count = $count; + $status->save(); + } + + Cache::forget('status:'.$status->id.':sharedby:userid:'.$user->id); if ($request->ajax()) { $response = ['code' => 200, 'msg' => 'Share saved', 'count' => $count]; diff --git a/app/Status.php b/app/Status.php index f29e88e0..769f0c71 100644 --- a/app/Status.php +++ b/app/Status.php @@ -150,8 +150,12 @@ class Status extends Model if(Auth::check() == false) { return false; } - $profile = Auth::user()->profile; - return Like::whereProfileId($profile->id)->whereStatusId($this->id)->count(); + $user = Auth::user(); + $id = $this->id; + return Cache::remember('status:'.$this->id.':likedby:userid:'.$user->id, now()->addHours(30), function() use($user, $id) { + $profile = $user->profile; + return Like::whereProfileId($profile->id)->whereStatusId($id)->count(); + }); } public function likedBy() @@ -191,9 +195,12 @@ class Status extends Model if(Auth::check() == false) { return false; } - $profile = Auth::user()->profile; - - return self::whereProfileId($profile->id)->whereReblogOfId($this->id)->count(); + $user = Auth::user(); + $id = $this->id; + return Cache::remember('status:'.$this->id.':sharedby:userid:'.$user->id, now()->addHours(30), function() use($user, $id) { + $profile = $user->profile; + return self::whereProfileId($profile->id)->whereReblogOfId($id)->count(); + }); } public function sharedBy() diff --git a/app/Transformer/Api/StatusTransformer.php b/app/Transformer/Api/StatusTransformer.php index c74f4ed6..1c033c1f 100644 --- a/app/Transformer/Api/StatusTransformer.php +++ b/app/Transformer/Api/StatusTransformer.php @@ -27,8 +27,8 @@ class StatusTransformer extends Fractal\TransformerAbstract 'content' => $status->rendered ?? $status->caption, 'created_at' => $status->created_at->format('c'), 'emojis' => [], - 'reblogs_count' => $status->shares()->count(), - 'favourites_count' => $status->likes()->count(), + 'reblogs_count' => $status->reblogs_count != 0 ? $status->reblogs_count: $status->shares()->count(), + 'favourites_count' => $status->likes_count != 0 ? $status->likes_count: $status->likes()->count(), 'reblogged' => $status->shared(), 'favourited' => $status->liked(), 'muted' => null, @@ -47,7 +47,7 @@ class StatusTransformer extends Fractal\TransformerAbstract 'comments_disabled' => $status->comments_disabled ? true : false, 'thread' => false, 'replies' => [], - 'parent' => $status->parent() ? $this->transform($status->parent()) : [], + 'parent' => [], ]; } diff --git a/public/js/activity.js b/public/js/activity.js index 7c8a688c..6952e855 100644 Binary files a/public/js/activity.js and b/public/js/activity.js differ diff --git a/public/mix-manifest.json b/public/mix-manifest.json index c92679d4..a6851bc8 100644 Binary files a/public/mix-manifest.json and b/public/mix-manifest.json differ diff --git a/resources/assets/js/components/Activity.vue b/resources/assets/js/components/Activity.vue index 3bcdac29..c237ee61 100644 --- a/resources/assets/js/components/Activity.vue +++ b/resources/assets/js/components/Activity.vue @@ -225,6 +225,7 @@ export default { break; case 'like': case 'favourite': + case 'comment': return n.status.url; break; }