FollowPipeline.php 1.6 KB

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