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

View file

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

View file

@ -4,6 +4,7 @@ namespace App\Services;
use Cache; use Cache;
use App\Profile; use App\Profile;
use App\Status;
use App\Transformer\Api\AccountTransformer; use App\Transformer\Api\AccountTransformer;
use League\Fractal; use League\Fractal;
use League\Fractal\Serializer\ArraySerializer; use League\Fractal\Serializer\ArraySerializer;
@ -35,4 +36,30 @@ class AccountService
return Cache::forget(self::CACHE_KEY . $id); 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) 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('pf:services:sh:id:' . $id);
Cache::forget('status:transformer:media:attachments:' . $id); Cache::forget('status:transformer:media:attachments:' . $id);
PublicTimelineService::rem($id); PublicTimelineService::rem($id);