HashtagInsertFanoutPipeline.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Jobs\HomeFeedPipeline;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldBeUnique;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use App\Hashtag;
  10. use App\StatusHashtag;
  11. use App\UserFilter;
  12. use App\Services\HashtagFollowService;
  13. use App\Services\HomeTimelineService;
  14. use App\Services\StatusService;
  15. use Illuminate\Queue\Middleware\WithoutOverlapping;
  16. use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
  17. class HashtagInsertFanoutPipeline implements ShouldQueue, ShouldBeUniqueUntilProcessing
  18. {
  19. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  20. protected $hashtag;
  21. public $timeout = 900;
  22. public $tries = 3;
  23. public $maxExceptions = 1;
  24. public $failOnTimeout = true;
  25. /**
  26. * Delete the job if its models no longer exist.
  27. *
  28. * @var bool
  29. */
  30. public $deleteWhenMissingModels = true;
  31. /**
  32. * The number of seconds after which the job's unique lock will be released.
  33. *
  34. * @var int
  35. */
  36. public $uniqueFor = 3600;
  37. /**
  38. * Get the unique ID for the job.
  39. */
  40. public function uniqueId(): string
  41. {
  42. return 'hfp:hashtag:fanout:insert:' . $this->hashtag->id;
  43. }
  44. /**
  45. * Get the middleware the job should pass through.
  46. *
  47. * @return array<int, object>
  48. */
  49. public function middleware(): array
  50. {
  51. return [(new WithoutOverlapping("hfp:hashtag:fanout:insert:{$this->hashtag->id}"))->shared()->dontRelease()];
  52. }
  53. /**
  54. * Create a new job instance.
  55. */
  56. public function __construct(StatusHashtag $hashtag)
  57. {
  58. $this->hashtag = $hashtag;
  59. }
  60. /**
  61. * Execute the job.
  62. */
  63. public function handle(): void
  64. {
  65. $hashtag = $this->hashtag;
  66. $sid = $hashtag->status_id;
  67. $status = StatusService::get($sid, false);
  68. if(!$status) {
  69. return;
  70. }
  71. if(!in_array($status['pf_type'], ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])) {
  72. return;
  73. }
  74. $skipIds = UserFilter::whereFilterableType('App\Profile')->whereFilterableId($status['account']['id'])->whereIn('filter_type', ['mute', 'block'])->pluck('user_id')->toArray();
  75. $ids = HashtagFollowService::getPidByHid($hashtag->hashtag_id);
  76. if(!$ids || !count($ids)) {
  77. return;
  78. }
  79. foreach($ids as $id) {
  80. if(!in_array($id, $skipIds)) {
  81. HomeTimelineService::add($id, $hashtag->status_id);
  82. }
  83. }
  84. }
  85. }