HashtagInsertFanoutPipeline.php 3.2 KB

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