diff --git a/app/Jobs/DeletePipeline/DeleteAccountPipeline.php b/app/Jobs/DeletePipeline/DeleteAccountPipeline.php index 3e6c974f4..24683e621 100644 --- a/app/Jobs/DeletePipeline/DeleteAccountPipeline.php +++ b/app/Jobs/DeletePipeline/DeleteAccountPipeline.php @@ -11,6 +11,7 @@ use DB; use Storage; use Illuminate\Support\Str; use App\Services\AccountService; +use App\Services\FollowerService; use App\Services\PublicTimelineService; use App\{ AccountInterstitial, @@ -133,7 +134,10 @@ class DeleteAccountPipeline implements ShouldQueue ->forceDelete(); Follower::whereProfileId($id) ->orWhere('following_id', $id) - ->forceDelete(); + ->each(function($follow) { + FollowerService::remove($follow->profile_id, $follow->following_id); + $follow->delete(); + }); Like::whereProfileId($id)->forceDelete(); }); diff --git a/app/Observers/UserObserver.php b/app/Observers/UserObserver.php index 66e55eacc..7b41d27f7 100644 --- a/app/Observers/UserObserver.php +++ b/app/Observers/UserObserver.php @@ -9,6 +9,7 @@ use App\User; use App\UserSetting; use App\Jobs\FollowPipeline\FollowPipeline; use DB; +use App\Services\FollowerService; class UserObserver { @@ -85,6 +86,16 @@ class UserObserver ]); }); } - } + + /** + * Handle the user "deleted" event. + * + * @param \App\User $user + * @return void + */ + public function deleted(User $user) + { + FollowerService::delCache($user->profile_id); + } } diff --git a/app/Services/FollowerService.php b/app/Services/FollowerService.php index cbf176ef0..45aeac248 100644 --- a/app/Services/FollowerService.php +++ b/app/Services/FollowerService.php @@ -14,6 +14,8 @@ use App\{ class FollowerService { const CACHE_KEY = 'pf:services:followers:'; + const FOLLOWERS_SYNC_KEY = 'pf:services:followers:sync-followers:'; + const FOLLOWING_SYNC_KEY = 'pf:services:followers:sync-following:'; const FOLLOWING_KEY = 'pf:services:follow:following:id:'; const FOLLOWERS_KEY = 'pf:services:follow:followers:id:'; @@ -35,17 +37,45 @@ class FollowerService public static function followers($id, $start = 0, $stop = 10) { + self::cacheSyncCheck($id, 'followers'); return Redis::zrange(self::FOLLOWERS_KEY . $id, $start, $stop); } public static function following($id, $start = 0, $stop = 10) { + self::cacheSyncCheck($id, 'following'); return Redis::zrange(self::FOLLOWING_KEY . $id, $start, $stop); } public static function follows(string $actor, string $target) { - return Follower::whereProfileId($actor)->whereFollowingId($target)->exists(); + self::cacheSyncCheck($target, 'followers'); + return (bool) Redis::zScore(self::FOLLOWERS_KEY . $target, $actor); + } + + public static function cacheSyncCheck($id, $scope = 'followers') + { + if($scope === 'followers') { + if(Cache::get(self::FOLLOWERS_SYNC_KEY . $id) != null) { + return; + } + $followers = Follower::whereFollowingId($id)->pluck('profile_id'); + $followers->each(function($fid) use($id) { + self::add($fid, $id); + }); + Cache::put(self::FOLLOWERS_SYNC_KEY . $id, 1, 604800); + } + if($scope === 'following') { + if(Cache::get(self::FOLLOWING_SYNC_KEY . $id) != null) { + return; + } + $followers = Follower::whereProfileId($id)->pluck('following_id'); + $followers->each(function($fid) use($id) { + self::add($id, $fid); + }); + Cache::put(self::FOLLOWING_SYNC_KEY . $id, 1, 604800); + } + return; } public static function audience($profile, $scope = null) @@ -114,4 +144,12 @@ class FollowerService }); } + public static function delCache($id) + { + Redis::del(self::CACHE_KEY . $id); + Redis::del(self::FOLLOWING_KEY . $id); + Redis::del(self::FOLLOWERS_KEY . $id); + Redis::del(self::FOLLOWERS_SYNC_KEY . $id); + Redis::del(self::FOLLOWING_SYNC_KEY . $id); + } } diff --git a/app/Services/RelationshipService.php b/app/Services/RelationshipService.php index 7c4091f0b..a5642e79e 100644 --- a/app/Services/RelationshipService.php +++ b/app/Services/RelationshipService.php @@ -14,8 +14,8 @@ class RelationshipService public static function get($aid, $tid) { - $actor = AccountService::get($aid); - $target = AccountService::get($tid); + $actor = AccountService::get($aid, true); + $target = AccountService::get($tid, true); if(!$actor || !$target) { return self::defaultRelation($tid); }