From 3f8acb12666495b807657195dadae8f9ab044e50 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Mon, 26 Jul 2021 23:59:38 -0600 Subject: [PATCH] Update AccountService, add syncPostCount method --- .../Controllers/Api/BaseApiController.php | 8 +++--- app/Http/Controllers/PublicApiController.php | 21 ++++++++++----- app/Services/AccountService.php | 27 +++++++++++++++++++ app/Services/StatusService.php | 2 ++ 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/Api/BaseApiController.php b/app/Http/Controllers/Api/BaseApiController.php index 70401eba5..c700f2434 100644 --- a/app/Http/Controllers/Api/BaseApiController.php +++ b/app/Http/Controllers/Api/BaseApiController.php @@ -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]; } diff --git a/app/Http/Controllers/PublicApiController.php b/app/Http/Controllers/PublicApiController.php index 5059f8161..566d2504a 100644 --- a/app/Http/Controllers/PublicApiController.php +++ b/app/Http/Controllers/PublicApiController.php @@ -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); } diff --git a/app/Services/AccountService.php b/app/Services/AccountService.php index 8f9f5c3b9..955e168b7 100644 --- a/app/Services/AccountService.php +++ b/app/Services/AccountService.php @@ -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; + } } diff --git a/app/Services/StatusService.php b/app/Services/StatusService.php index 892005f7e..d503f906f 100644 --- a/app/Services/StatusService.php +++ b/app/Services/StatusService.php @@ -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);