FeedFollowPipeline.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\Services\AccountService;
  12. use App\Services\HomeTimelineService;
  13. use App\Services\SnowflakeService;
  14. use App\Status;
  15. class FeedFollowPipeline implements ShouldQueue, ShouldBeUniqueUntilProcessing
  16. {
  17. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  18. protected $actorId;
  19. protected $followingId;
  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:follows:aid:' . $this->actorId . ':fid:' . $this->followingId;
  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:follows:aid:{$this->actorId}:fid:{$this->followingId}"))->shared()->dontRelease()];
  45. }
  46. /**
  47. * Create a new job instance.
  48. */
  49. public function __construct($actorId, $followingId)
  50. {
  51. $this->actorId = $actorId;
  52. $this->followingId = $followingId;
  53. }
  54. /**
  55. * Execute the job.
  56. */
  57. public function handle(): void
  58. {
  59. $actorId = $this->actorId;
  60. $followingId = $this->followingId;
  61. $minId = SnowflakeService::byDate(now()->subWeeks(6));
  62. $ids = Status::where('id', '>', $minId)
  63. ->where('profile_id', $followingId)
  64. ->whereNull(['in_reply_to_id', 'reblog_of_id'])
  65. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  66. ->whereIn('visibility',['public', 'unlisted', 'private'])
  67. ->orderByDesc('id')
  68. ->limit(HomeTimelineService::FOLLOWER_FEED_POST_LIMIT)
  69. ->pluck('id');
  70. foreach($ids as $id) {
  71. HomeTimelineService::add($actorId, $id);
  72. }
  73. }
  74. }