1
0

SharePipeline.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Jobs\SharePipeline;
  3. use App\Status;
  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 SharePipeline implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. protected $status;
  17. /**
  18. * Delete the job if its models no longer exist.
  19. *
  20. * @var bool
  21. */
  22. public $deleteWhenMissingModels = true;
  23. /**
  24. * Create a new job instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct(Status $status)
  29. {
  30. $this->status = $status;
  31. }
  32. /**
  33. * Execute the job.
  34. *
  35. * @return void
  36. */
  37. public function handle()
  38. {
  39. $status = $this->status;
  40. $actor = $status->profile;
  41. $target = $status->parent()->profile;
  42. if ($status->uri !== null) {
  43. // Ignore notifications to remote statuses
  44. return;
  45. }
  46. $exists = Notification::whereProfileId($target->id)
  47. ->whereActorId($status->profile_id)
  48. ->whereAction('share')
  49. ->whereItemId($status->reblog_of_id)
  50. ->whereItemType('App\Status')
  51. ->count();
  52. if ($target->id === $status->profile_id || $exists !== 0) {
  53. return true;
  54. }
  55. try {
  56. $notification = new Notification;
  57. $notification->profile_id = $target->id;
  58. $notification->actor_id = $actor->id;
  59. $notification->action = 'share';
  60. $notification->message = $status->shareToText();
  61. $notification->rendered = $status->shareToHtml();
  62. $notification->item_id = $status->id;
  63. $notification->item_type = "App\Status";
  64. $notification->save();
  65. Cache::forever('notification.'.$notification->id, $notification);
  66. $redis = Redis::connection();
  67. $key = config('cache.prefix').':user.'.$status->profile_id.'.notifications';
  68. $redis->lpush($key, $notification->id);
  69. } catch (Exception $e) {
  70. Log::error($e);
  71. }
  72. }
  73. }