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