forked from mirror/pixelfed
Merge pull request #1418 from pixelfed/frontend-ui-refactor
Update StatusTransformer, cache counts
This commit is contained in:
commit
0b4dc109f8
7 changed files with 38 additions and 16 deletions
|
@ -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];
|
||||
|
|
|
@ -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];
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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' => [],
|
||||
];
|
||||
}
|
||||
|
||||
|
|
BIN
public/js/activity.js
vendored
BIN
public/js/activity.js
vendored
Binary file not shown.
Binary file not shown.
|
@ -225,6 +225,7 @@ export default {
|
|||
break;
|
||||
case 'like':
|
||||
case 'favourite':
|
||||
case 'comment':
|
||||
return n.status.url;
|
||||
break;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue