Update AccountService, add syncPostCount method

This commit is contained in:
Daniel Supernault 2021-07-26 23:59:38 -06:00
parent b34814e9ff
commit 3f8acb1266
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
4 changed files with 48 additions and 10 deletions

View file

@ -37,6 +37,7 @@ use App\Jobs\VideoPipeline\{
VideoPostProcess,
VideoThumbnail
};
use App\Services\AccountService;
use App\Services\NotificationService;
use App\Services\MediaPathService;
use App\Services\MediaBlocklistService;
@ -311,10 +312,8 @@ class BaseApiController extends Controller
$status->scope = 'archived';
$status->visibility = 'draft';
$status->save();
StatusService::del($status->id);
// invalidate caches
AccountService::syncPostCount($status->profile_id);
return [200];
}
@ -339,8 +338,9 @@ class BaseApiController extends Controller
$status->scope = $archive->original_scope;
$status->visibility = $archive->original_scope;
$status->save();
$archive->delete();
StatusService::del($status->id);
AccountService::syncPostCount($status->profile_id);
return [200];
}

View file

@ -675,9 +675,7 @@ class PublicApiController extends Controller
$limit = $request->limit ?? 9;
$max_id = $request->max_id;
$min_id = $request->min_id;
$scope = $request->only_media == true ?
['photo', 'photo:album', 'video', 'video:album'] :
['photo', 'photo:album', 'video', 'video:album', 'share', 'reply', 'text'];
$scope = ['photo', 'photo:album', 'video', 'video:album'];
if($profile->is_private) {
if(!$user) {
@ -713,6 +711,8 @@ class PublicApiController extends Controller
'created_at'
)
->whereProfileId($profile->id)
->whereNull('in_reply_to_id')
->whereNull('reblog_of_id')
->whereIn('type', $scope)
->where('id', $dir, $id)
->whereIn('scope', $visibility)
@ -720,12 +720,21 @@ class PublicApiController extends Controller
->orderByDesc('id')
->get()
->map(function($s) use($user) {
$status = StatusService::get($s->id, false);
if($user) {
try {
$status = StatusService::get($s->id, false);
} catch (\Exception $e) {
$status = false;
}
if($user && $status) {
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
}
return $status;
});
})
->filter(function($s) {
return $s;
})
->values();
return response()->json($res);
}

View file

@ -4,6 +4,7 @@ namespace App\Services;
use Cache;
use App\Profile;
use App\Status;
use App\Transformer\Api\AccountTransformer;
use League\Fractal;
use League\Fractal\Serializer\ArraySerializer;
@ -35,4 +36,30 @@ class AccountService
return Cache::forget(self::CACHE_KEY . $id);
}
public static function syncPostCount($id)
{
$profile = Profile::find($id);
if(!$profile) {
return false;
}
$key = self::CACHE_KEY . 'pcs:' . $id;
if(Cache::has($key)) {
return;
}
$count = Status::whereProfileId($id)
->whereNull('in_reply_to_id')
->whereNull('reblog_of_id')
->whereIn('scope', ['public', 'unlisted', 'private'])
->count();
$profile->status_count = $count;
$profile->save();
Cache::put($key, 1, 900);
return true;
}
}

View file

@ -41,6 +41,8 @@ class StatusService {
public static function del($id)
{
Cache::forget('status:thumb:nsfw0' . $id);
Cache::forget('status:thumb:nsfw1' . $id);
Cache::forget('pf:services:sh:id:' . $id);
Cache::forget('status:transformer:media:attachments:' . $id);
PublicTimelineService::rem($id);