Update AccountTransformer, cache status count

This commit is contained in:
Daniel Supernault 2019-06-05 01:38:19 -06:00
parent 339a0a3882
commit 377394f0f2
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
4 changed files with 17 additions and 30 deletions

View file

@ -347,6 +347,7 @@ class InternalApiController extends Controller
NewStatusPipeline::dispatch($status);
Cache::forget('user:account:id:'.$profile->user_id);
Cache::forget('profile:status_count:'.$profile->id);
return $status->url();
}
}

View file

@ -196,7 +196,8 @@ class StatusController extends Controller
}
$status->type = (new self)::mimeTypeCheck($mimes);
$status->save();
Cache::forget('profile:status_count:'.$profile->id);
NewStatusPipeline::dispatch($status);
// TODO: Send to subscribers
@ -215,6 +216,7 @@ class StatusController extends Controller
$status = Status::findOrFail($request->input('item'));
if ($status->profile_id === Auth::user()->profile->id || Auth::user()->is_admin == true) {
Cache::forget('profile:status_count:'.$status->profile_id);
StatusDelete::dispatch($status);
}
if($request->wantsJson()) {

View file

@ -125,7 +125,7 @@ class Profile extends Model
public function avatarUrl()
{
$url = Cache::remember("avatar:{$this->id}", now()->addYears(1), function () {
$url = Cache::remember('avatar:'.$this->id, now()->addYears(1), function () {
$avatar = $this->avatar;
$path = $avatar->media_path;
$version = hash('sha256', $avatar->change_count);
@ -139,36 +139,20 @@ class Profile extends Model
public function statusCount()
{
return $this->statuses()
->getQuery()
->whereHas('media')
->whereNull('in_reply_to_id')
->whereNull('reblog_of_id')
->count();
return Cache::remember('profile:status_count:'.$this->id, now()->addMonths(1), function() {
return $this->statuses()
->getQuery()
->whereHas('media')
->whereNull('in_reply_to_id')
->whereNull('reblog_of_id')
->count();
});
}
// deprecated
public function recommendFollowers()
{
$follows = $this->following()->pluck('followers.id');
$following = $this->following()
->orderByRaw('rand()')
->take(3)
->pluck('following_id');
$following->push(Auth::id());
$following = Follower::whereNotIn('profile_id', $follows)
->whereNotIn('following_id', $following)
->whereNotIn('following_id', $follows)
->whereIn('profile_id', $following)
->orderByRaw('rand()')
->distinct('id')
->limit(3)
->pluck('following_id');
$recommended = [];
foreach ($following as $follow) {
$recommended[] = self::findOrFail($follow);
}
return $recommended;
return collect([]);
}
public function keyId()

View file

@ -19,7 +19,7 @@ class AccountTransformer extends Fractal\TransformerAbstract
'created_at' => null,
'followers_count' => $profile->followerCount(),
'following_count' => $profile->followingCount(),
'statuses_count' => $profile->statusCount(),
'statuses_count' => (int) $profile->statusCount(),
'note' => $profile->bio,
'url' => $profile->url(),
'avatar' => $profile->avatarUrl(),
@ -30,7 +30,7 @@ class AccountTransformer extends Fractal\TransformerAbstract
'moved' => null,
'fields' => null,
'bot' => null,
'website' => $profile->website,
'website' => null,
'software' => 'pixelfed',
'is_admin' => (bool) $is_admin
];