UnfollowPipeline.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Jobs\FollowPipeline;
  3. use App\Follower;
  4. use App\FollowRequest;
  5. use App\Jobs\HomeFeedPipeline\FeedUnfollowPipeline;
  6. use App\Notification;
  7. use App\Profile;
  8. use App\Services\AccountService;
  9. use App\Services\FollowerService;
  10. use App\Services\NotificationService;
  11. use Cache;
  12. use Illuminate\Bus\Queueable;
  13. use Illuminate\Contracts\Queue\ShouldQueue;
  14. use Illuminate\Foundation\Bus\Dispatchable;
  15. use Illuminate\Queue\InteractsWithQueue;
  16. use Illuminate\Queue\SerializesModels;
  17. class UnfollowPipeline implements ShouldQueue
  18. {
  19. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  20. protected $actor;
  21. protected $target;
  22. /**
  23. * Create a new job instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct($actor, $target)
  28. {
  29. $this->actor = $actor;
  30. $this->target = $target;
  31. }
  32. /**
  33. * Execute the job.
  34. *
  35. * @return void
  36. */
  37. public function handle()
  38. {
  39. $actor = $this->actor;
  40. $target = $this->target;
  41. $actorProfile = Profile::find($actor);
  42. if (! $actorProfile) {
  43. return;
  44. }
  45. $targetProfile = Profile::find($target);
  46. if (! $targetProfile) {
  47. return;
  48. }
  49. FeedUnfollowPipeline::dispatch($actor, $target)->onQueue('follow');
  50. FollowerService::remove($actor, $target);
  51. $actorProfileSync = Cache::get(FollowerService::FOLLOWING_SYNC_KEY.$actor);
  52. if (! $actorProfileSync) {
  53. FollowServiceWarmCache::dispatch($actor)->onQueue('low');
  54. } else {
  55. if ($actorProfile->following_count) {
  56. $actorProfile->decrement('following_count');
  57. } else {
  58. $count = Follower::whereProfileId($actor)->count();
  59. $actorProfile->following_count = $count;
  60. $actorProfile->save();
  61. }
  62. Cache::put(FollowerService::FOLLOWING_SYNC_KEY.$actor, 1, 604800);
  63. AccountService::del($actor);
  64. }
  65. $targetProfileSync = Cache::get(FollowerService::FOLLOWERS_SYNC_KEY.$target);
  66. if (! $targetProfileSync) {
  67. FollowServiceWarmCache::dispatch($target)->onQueue('low');
  68. } else {
  69. if ($targetProfile->followers_count) {
  70. $targetProfile->decrement('followers_count');
  71. } else {
  72. $count = Follower::whereFollowingId($target)->count();
  73. $targetProfile->followers_count = $count;
  74. $targetProfile->save();
  75. }
  76. Cache::put(FollowerService::FOLLOWERS_SYNC_KEY.$target, 1, 604800);
  77. AccountService::del($target);
  78. }
  79. if ($targetProfile->domain == null) {
  80. Notification::withTrashed()
  81. ->whereProfileId($target)
  82. ->whereAction('follow')
  83. ->whereActorId($actor)
  84. ->whereItemId($target)
  85. ->whereItemType('App\Profile')
  86. ->get()
  87. ->each(function ($n) {
  88. NotificationService::del($n->profile_id, $n->id);
  89. $n->forceDelete();
  90. });
  91. }
  92. if ($actorProfile->domain == null && config('instance.timeline.home.cached')) {
  93. Cache::forget('pf:timelines:home:'.$actor);
  94. }
  95. FollowRequest::whereFollowingId($target)
  96. ->whereFollowerId($actor)
  97. ->delete();
  98. AccountService::del($target);
  99. AccountService::del($actor);
  100. }
  101. }