LikePipeline.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Jobs\LikePipeline;
  3. use App\Like;
  4. use App\Notification;
  5. use Cache;
  6. use Illuminate\Bus\Queueable;
  7. use Illuminate\Contracts\Queue\ShouldQueue;
  8. use Illuminate\Foundation\Bus\Dispatchable;
  9. use Illuminate\Queue\InteractsWithQueue;
  10. use Illuminate\Queue\SerializesModels;
  11. use Log;
  12. use Redis;
  13. class LikePipeline implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. protected $like;
  17. /**
  18. * Create a new job instance.
  19. *
  20. * @return void
  21. */
  22. public function __construct(Like $like)
  23. {
  24. $this->like = $like;
  25. }
  26. /**
  27. * Execute the job.
  28. *
  29. * @return void
  30. */
  31. public function handle()
  32. {
  33. $like = $this->like;
  34. $status = $this->like->status;
  35. $actor = $this->like->actor;
  36. if (!$status || $status->url !== null) {
  37. // Ignore notifications to remote statuses, or deleted statuses
  38. return;
  39. }
  40. $exists = Notification::whereProfileId($status->profile_id)
  41. ->whereActorId($actor->id)
  42. ->whereAction('like')
  43. ->whereItemId($status->id)
  44. ->whereItemType('App\Status')
  45. ->count();
  46. if ($actor->id === $status->profile_id || $exists !== 0) {
  47. return true;
  48. }
  49. try {
  50. $notification = new Notification();
  51. $notification->profile_id = $status->profile_id;
  52. $notification->actor_id = $actor->id;
  53. $notification->action = 'like';
  54. $notification->message = $like->toText();
  55. $notification->rendered = $like->toHtml();
  56. $notification->item_id = $status->id;
  57. $notification->item_type = "App\Status";
  58. $notification->save();
  59. Cache::forever('notification.'.$notification->id, $notification);
  60. $redis = Redis::connection();
  61. $key = config('cache.prefix').':user.'.$status->profile_id.'.notifications';
  62. $redis->lpush($key, $notification->id);
  63. } catch (Exception $e) {
  64. Log::error($e);
  65. }
  66. }
  67. }