FollowPipeline.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace App\Jobs\FollowPipeline;
  3. use App\Notification;
  4. use Cache;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Foundation\Bus\Dispatchable;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Queue\SerializesModels;
  10. use Log;
  11. use Redis;
  12. class FollowPipeline implements ShouldQueue
  13. {
  14. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  15. protected $follower;
  16. /**
  17. * Delete the job if its models no longer exist.
  18. *
  19. * @var bool
  20. */
  21. public $deleteWhenMissingModels = true;
  22. /**
  23. * Create a new job instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct($follower)
  28. {
  29. $this->follower = $follower;
  30. }
  31. /**
  32. * Execute the job.
  33. *
  34. * @return void
  35. */
  36. public function handle()
  37. {
  38. $follower = $this->follower;
  39. $actor = $follower->actor;
  40. $target = $follower->target;
  41. if($target->domain || !$target->private_key) {
  42. return;
  43. }
  44. try {
  45. $notification = new Notification();
  46. $notification->profile_id = $target->id;
  47. $notification->actor_id = $actor->id;
  48. $notification->action = 'follow';
  49. $notification->message = $follower->toText();
  50. $notification->rendered = $follower->toHtml();
  51. $notification->item_id = $target->id;
  52. $notification->item_type = "App\Profile";
  53. $notification->save();
  54. $redis = Redis::connection();
  55. $nkey = config('cache.prefix').':user.'.$target->id.'.notifications';
  56. $redis->lpush($nkey, $notification->id);
  57. } catch (Exception $e) {
  58. Log::error($e);
  59. }
  60. }
  61. }