FollowPipeline.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. try {
  42. $notification = new Notification();
  43. $notification->profile_id = $target->id;
  44. $notification->actor_id = $actor->id;
  45. $notification->action = 'follow';
  46. $notification->message = $follower->toText();
  47. $notification->rendered = $follower->toHtml();
  48. $notification->item_id = $target->id;
  49. $notification->item_type = "App\Profile";
  50. $notification->save();
  51. Cache::forever('notification.'.$notification->id, $notification);
  52. Cache::forget('feature:discover:people:'.$actor->id);
  53. $redis = Redis::connection();
  54. $nkey = config('cache.prefix').':user.'.$target->id.'.notifications';
  55. $redis->lpush($nkey, $notification->id);
  56. } catch (Exception $e) {
  57. Log::error($e);
  58. }
  59. }
  60. }