Merge pull request #1418 from pixelfed/frontend-ui-refactor

Update StatusTransformer, cache counts
This commit is contained in:
daniel 2019-06-18 01:18:15 -06:00 committed by GitHub
commit 0b4dc109f8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 38 additions and 16 deletions

View file

@ -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];

View file

@ -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];

View file

@ -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()

View file

@ -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

Binary file not shown.

Binary file not shown.

View file

@ -225,6 +225,7 @@ export default {
break;
case 'like':
case 'favourite':
case 'comment':
return n.status.url;
break;
}