1
0

FeedInsertPipeline.php 3.0 KB

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