Update FollowerService, improve cache invalidation

This commit is contained in:
Daniel Supernault 2022-12-07 02:50:13 -07:00
parent d277b8e8ed
commit 81f7d17263
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7
4 changed files with 56 additions and 4 deletions

View file

@ -650,7 +650,6 @@ class ApiV1Controller extends Controller
->whereNull('status') ->whereNull('status')
->findOrFail($id); ->findOrFail($id);
$private = (bool) $target->is_private; $private = (bool) $target->is_private;
$remote = (bool) $target->domain; $remote = (bool) $target->domain;
$blocked = UserFilter::whereUserId($target->id) $blocked = UserFilter::whereUserId($target->id)
@ -701,6 +700,7 @@ class ApiV1Controller extends Controller
(new FollowerController())->sendFollow($user->profile, $target); (new FollowerController())->sendFollow($user->profile, $target);
} }
FollowPipeline::dispatch($follower); FollowPipeline::dispatch($follower);
$target->increment('followers_count');
} }
RelationshipService::refresh($user->profile_id, $target->id); RelationshipService::refresh($user->profile_id, $target->id);
@ -778,6 +778,10 @@ class ApiV1Controller extends Controller
->whereFollowingId($target->id) ->whereFollowingId($target->id)
->delete(); ->delete();
FollowerService::remove($user->profile_id, $target->id);
$target->decrement('followers_count');
if($remote == true && config('federation.activitypub.remoteFollow') == true) { if($remote == true && config('federation.activitypub.remoteFollow') == true) {
(new FollowerController())->sendUndoFollow($user->profile, $target); (new FollowerController())->sendUndoFollow($user->profile, $target);
} }

View file

@ -0,0 +1,42 @@
<?php
namespace App\Observers;
use App\Follower;
use App\Services\FollowerService;
class FollowerObserver
{
/**
* Handle the Follower "created" event.
*
* @param \App\Follower $follower
* @return void
*/
public function created(Follower $follower)
{
FollowerService::add($follower->profile_id, $follower->following_id);
}
/**
* Handle the Follower "deleted" event.
*
* @param \App\Follower $follower
* @return void
*/
public function deleted(Follower $follower)
{
FollowerService::remove($follower->profile_id, (string) $follower->following_id);
}
/**
* Handle the Follower "force deleted" event.
*
* @param \App\Follower $follower
* @return void
*/
public function forceDeleted(Follower $follower)
{
FollowerService::remove($follower->profile_id, (string) $follower->following_id);
}
}

View file

@ -4,6 +4,7 @@ namespace App\Providers;
use App\Observers\{ use App\Observers\{
AvatarObserver, AvatarObserver,
FollowerObserver,
LikeObserver, LikeObserver,
NotificationObserver, NotificationObserver,
ModLogObserver, ModLogObserver,
@ -15,6 +16,7 @@ use App\Observers\{
}; };
use App\{ use App\{
Avatar, Avatar,
Follower,
Like, Like,
Notification, Notification,
ModLog, ModLog,
@ -47,6 +49,7 @@ class AppServiceProvider extends ServiceProvider
Schema::defaultStringLength(191); Schema::defaultStringLength(191);
Paginator::useBootstrap(); Paginator::useBootstrap();
Avatar::observe(AvatarObserver::class); Avatar::observe(AvatarObserver::class);
Follower::observe(FollowerObserver::class);
Like::observe(LikeObserver::class); Like::observe(LikeObserver::class);
Notification::observe(NotificationObserver::class); Notification::observe(NotificationObserver::class);
ModLog::observe(ModLogObserver::class); ModLog::observe(ModLogObserver::class);

View file

@ -23,18 +23,21 @@ class FollowerService
public static function add($actor, $target) public static function add($actor, $target)
{ {
$ts = (int) microtime(true);
RelationshipService::refresh($actor, $target); RelationshipService::refresh($actor, $target);
Redis::zadd(self::FOLLOWING_KEY . $actor, $target, $target); Redis::zadd(self::FOLLOWING_KEY . $actor, $ts, $target);
Redis::zadd(self::FOLLOWERS_KEY . $target, $actor, $actor); Redis::zadd(self::FOLLOWERS_KEY . $target, $ts, $actor);
} }
public static function remove($actor, $target) public static function remove($actor, $target)
{ {
RelationshipService::refresh($actor, $target);
Redis::zrem(self::FOLLOWING_KEY . $actor, $target); Redis::zrem(self::FOLLOWING_KEY . $actor, $target);
Redis::zrem(self::FOLLOWERS_KEY . $target, $actor); Redis::zrem(self::FOLLOWERS_KEY . $target, $actor);
Cache::forget('pf:services:follow:audience:' . $actor); Cache::forget('pf:services:follow:audience:' . $actor);
Cache::forget('pf:services:follow:audience:' . $target); Cache::forget('pf:services:follow:audience:' . $target);
AccountService::del($actor);
AccountService::del($target);
RelationshipService::refresh($actor, $target);
} }
public static function followers($id, $start = 0, $stop = 10) public static function followers($id, $start = 0, $stop = 10)