Update UnfollowPipeline, fix follower count cache bug

This commit is contained in:
Daniel Supernault 2024-04-06 03:29:17 -06:00
parent 858fcbf606
commit 6bdf73de4d
No known key found for this signature in database
GPG key ID: 23740873EE6F76A1

View file

@ -4,114 +4,115 @@ namespace App\Jobs\FollowPipeline;
use App\Follower; use App\Follower;
use App\FollowRequest; use App\FollowRequest;
use App\Jobs\HomeFeedPipeline\FeedUnfollowPipeline;
use App\Notification; use App\Notification;
use App\Profile; use App\Profile;
use App\Services\AccountService;
use App\Services\FollowerService;
use App\Services\NotificationService;
use Cache; use Cache;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Log;
use Illuminate\Support\Facades\Redis;
use App\Services\AccountService;
use App\Services\FollowerService;
use App\Services\NotificationService;
use App\Jobs\HomeFeedPipeline\FeedUnfollowPipeline;
class UnfollowPipeline implements ShouldQueue class UnfollowPipeline implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $actor; protected $actor;
protected $target;
/** protected $target;
* Create a new job instance.
*
* @return void
*/
public function __construct($actor, $target)
{
$this->actor = $actor;
$this->target = $target;
}
/** /**
* Execute the job. * Create a new job instance.
* *
* @return void * @return void
*/ */
public function handle() public function __construct($actor, $target)
{ {
$actor = $this->actor; $this->actor = $actor;
$target = $this->target; $this->target = $target;
}
$actorProfile = Profile::find($actor); /**
if(!$actorProfile) { * Execute the job.
return; *
} * @return void
$targetProfile = Profile::find($target); */
if(!$targetProfile) { public function handle()
return; {
} $actor = $this->actor;
$target = $this->target;
FeedUnfollowPipeline::dispatch($actor, $target)->onQueue('follow'); $actorProfile = Profile::find($actor);
if (! $actorProfile) {
return;
}
$targetProfile = Profile::find($target);
if (! $targetProfile) {
return;
}
FollowerService::remove($actor, $target); FeedUnfollowPipeline::dispatch($actor, $target)->onQueue('follow');
$actorProfileSync = Cache::get(FollowerService::FOLLOWING_SYNC_KEY . $actor); FollowerService::remove($actor, $target);
if(!$actorProfileSync) {
FollowServiceWarmCache::dispatch($actor)->onQueue('low');
} else {
if($actorProfile->following_count) {
$actorProfile->decrement('following_count');
} else {
$count = Follower::whereProfileId($actor)->count();
$actorProfile->following_count = $count;
$actorProfile->save();
}
Cache::put(FollowerService::FOLLOWING_SYNC_KEY . $actor, 1, 604800);
AccountService::del($actor);
}
$targetProfileSync = Cache::get(FollowerService::FOLLOWERS_SYNC_KEY . $target); $actorProfileSync = Cache::get(FollowerService::FOLLOWING_SYNC_KEY.$actor);
if(!$targetProfileSync) { if (! $actorProfileSync) {
FollowServiceWarmCache::dispatch($target)->onQueue('low'); FollowServiceWarmCache::dispatch($actor)->onQueue('low');
} else { } else {
if($targetProfile->followers_count) { if ($actorProfile->following_count) {
$targetProfile->decrement('followers_count'); $actorProfile->decrement('following_count');
} else { } else {
$count = Follower::whereFollowingId($target)->count(); $count = Follower::whereProfileId($actor)->count();
$targetProfile->followers_count = $count; $actorProfile->following_count = $count;
$targetProfile->save(); $actorProfile->save();
} }
Cache::put(FollowerService::FOLLOWERS_SYNC_KEY . $target, 1, 604800); Cache::put(FollowerService::FOLLOWING_SYNC_KEY.$actor, 1, 604800);
AccountService::del($target); AccountService::del($actor);
} }
if($targetProfile->domain == null) { $targetProfileSync = Cache::get(FollowerService::FOLLOWERS_SYNC_KEY.$target);
Notification::withTrashed() if (! $targetProfileSync) {
->whereProfileId($target) FollowServiceWarmCache::dispatch($target)->onQueue('low');
->whereAction('follow') } else {
->whereActorId($actor) if ($targetProfile->followers_count) {
->whereItemId($target) $targetProfile->decrement('followers_count');
->whereItemType('App\Profile') } else {
->get() $count = Follower::whereFollowingId($target)->count();
->each(function($n) { $targetProfile->followers_count = $count;
NotificationService::del($n->profile_id, $n->id); $targetProfile->save();
$n->forceDelete(); }
}); Cache::put(FollowerService::FOLLOWERS_SYNC_KEY.$target, 1, 604800);
} AccountService::del($target);
}
if($actorProfile->domain == null && config('instance.timeline.home.cached')) { if ($targetProfile->domain == null) {
Cache::forget('pf:timelines:home:' . $actor); Notification::withTrashed()
} ->whereProfileId($target)
->whereAction('follow')
->whereActorId($actor)
->whereItemId($target)
->whereItemType('App\Profile')
->get()
->each(function ($n) {
NotificationService::del($n->profile_id, $n->id);
$n->forceDelete();
});
}
FollowRequest::whereFollowingId($target) if ($actorProfile->domain == null && config('instance.timeline.home.cached')) {
->whereFollowerId($actor) Cache::forget('pf:timelines:home:'.$actor);
->delete(); }
return; FollowRequest::whereFollowingId($target)
} ->whereFollowerId($actor)
->delete();
AccountService::del($target);
AccountService::del($actor);
}
} }