HashtagInsertFanoutPipeline.php 2.4 KB

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