1
0
Fork 0

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')
->findOrFail($id);
$private = (bool) $target->is_private;
$remote = (bool) $target->domain;
$blocked = UserFilter::whereUserId($target->id)
@ -701,6 +700,7 @@ class ApiV1Controller extends Controller
(new FollowerController())->sendFollow($user->profile, $target);
}
FollowPipeline::dispatch($follower);
$target->increment('followers_count');
}
RelationshipService::refresh($user->profile_id, $target->id);
@ -778,6 +778,10 @@ class ApiV1Controller extends Controller
->whereFollowingId($target->id)
->delete();
FollowerService::remove($user->profile_id, $target->id);
$target->decrement('followers_count');
if($remote == true && config('federation.activitypub.remoteFollow') == true) {
(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\{
AvatarObserver,
FollowerObserver,
LikeObserver,
NotificationObserver,
ModLogObserver,
@ -15,6 +16,7 @@ use App\Observers\{
};
use App\{
Avatar,
Follower,
Like,
Notification,
ModLog,
@ -47,6 +49,7 @@ class AppServiceProvider extends ServiceProvider
Schema::defaultStringLength(191);
Paginator::useBootstrap();
Avatar::observe(AvatarObserver::class);
Follower::observe(FollowerObserver::class);
Like::observe(LikeObserver::class);
Notification::observe(NotificationObserver::class);
ModLog::observe(ModLogObserver::class);

View File

@ -23,18 +23,21 @@ class FollowerService
public static function add($actor, $target)
{
$ts = (int) microtime(true);
RelationshipService::refresh($actor, $target);
Redis::zadd(self::FOLLOWING_KEY . $actor, $target, $target);
Redis::zadd(self::FOLLOWERS_KEY . $target, $actor, $actor);
Redis::zadd(self::FOLLOWING_KEY . $actor, $ts, $target);
Redis::zadd(self::FOLLOWERS_KEY . $target, $ts, $actor);
}
public static function remove($actor, $target)
{
RelationshipService::refresh($actor, $target);
Redis::zrem(self::FOLLOWING_KEY . $actor, $target);
Redis::zrem(self::FOLLOWERS_KEY . $target, $actor);
Cache::forget('pf:services:follow:audience:' . $actor);
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)