ProfilePurgeFollowersByDomain.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\Jobs\ProfilePipeline;
  3. use Illuminate\Bus\Batchable;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldBeUnique;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. use Illuminate\Queue\Middleware\WithoutOverlapping;
  11. use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
  12. use App\Follower;
  13. use App\Profile;
  14. use App\Notification;
  15. use DB;
  16. use App\Services\AccountService;
  17. use App\Services\FollowerService;
  18. use App\Services\NotificationService;
  19. class ProfilePurgeFollowersByDomain implements ShouldQueue, ShouldBeUniqueUntilProcessing
  20. {
  21. use Batchable, Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  22. protected $pid;
  23. protected $domain;
  24. public $timeout = 900;
  25. public $tries = 3;
  26. public $maxExceptions = 1;
  27. public $failOnTimeout = true;
  28. /**
  29. * The number of seconds after which the job's unique lock will be released.
  30. *
  31. * @var int
  32. */
  33. public $uniqueFor = 3600;
  34. /**
  35. * Get the unique ID for the job.
  36. */
  37. public function uniqueId(): string
  38. {
  39. return 'followers:v1:purge-by-domain:' . $this->pid . ':d-' . $this->domain;
  40. }
  41. /**
  42. * Get the middleware the job should pass through.
  43. *
  44. * @return array<int, object>
  45. */
  46. public function middleware(): array
  47. {
  48. return [(new WithoutOverlapping("followers:v1:purge-by-domain:{$this->pid}:d-{$this->domain}"))->shared()->dontRelease()];
  49. }
  50. /**
  51. * Create a new job instance.
  52. */
  53. public function __construct($pid, $domain)
  54. {
  55. $this->pid = $pid;
  56. $this->domain = $domain;
  57. }
  58. /**
  59. * Execute the job.
  60. */
  61. public function handle(): void
  62. {
  63. if ($this->batch()->cancelled()) {
  64. return;
  65. }
  66. $pid = $this->pid;
  67. $domain = $this->domain;
  68. $query = 'SELECT f.*
  69. FROM followers f
  70. JOIN profiles p ON p.id = f.profile_id OR p.id = f.following_id
  71. WHERE (f.profile_id = ? OR f.following_id = ?)
  72. AND p.domain = ?;';
  73. $params = [$pid, $pid, $domain];
  74. foreach(DB::cursor($query, $params) as $n) {
  75. if(!$n || !$n->id) {
  76. continue;
  77. }
  78. $follower = Follower::find($n->id);
  79. if($follower->following_id == $pid && $follower->profile_id) {
  80. FollowerService::remove($follower->profile_id, $pid, true);
  81. $follower->delete();
  82. } else if ($follower->profile_id == $pid && $follower->following_id) {
  83. FollowerService::remove($follower->following_id, $pid, true);
  84. $follower->delete();
  85. }
  86. }
  87. $profile = Profile::find($pid);
  88. $followerCount = DB::table('profiles')
  89. ->join('followers', 'profiles.id', '=', 'followers.following_id')
  90. ->where('followers.following_id', $pid)
  91. ->count();
  92. $followingCount = DB::table('profiles')
  93. ->join('followers', 'profiles.id', '=', 'followers.following_id')
  94. ->where('followers.profile_id', $pid)
  95. ->count();
  96. $profile->followers_count = $followerCount;
  97. $profile->following_count = $followingCount;
  98. $profile->save();
  99. AccountService::del($profile->id);
  100. }
  101. }