UnfollowPipeline.php 2.8 KB

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