GroupCommentPipeline.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Jobs\GroupPipeline;
  3. use App\Notification;
  4. use App\Status;
  5. use App\Models\GroupPost;
  6. use Cache;
  7. use DB;
  8. use Illuminate\Bus\Queueable;
  9. use Illuminate\Contracts\Queue\ShouldQueue;
  10. use Illuminate\Foundation\Bus\Dispatchable;
  11. use Illuminate\Queue\InteractsWithQueue;
  12. use Illuminate\Queue\SerializesModels;
  13. use Illuminate\Support\Facades\Redis;
  14. use App\Services\MediaStorageService;
  15. use App\Services\NotificationService;
  16. use App\Services\StatusService;
  17. class GroupCommentPipeline implements ShouldQueue
  18. {
  19. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  20. protected $status;
  21. protected $comment;
  22. protected $groupPost;
  23. public function __construct(Status $status, Status $comment, $groupPost = null)
  24. {
  25. $this->status = $status;
  26. $this->comment = $comment;
  27. $this->groupPost = $groupPost;
  28. }
  29. public function handle()
  30. {
  31. if($this->status->group_id == null || $this->comment->group_id == null) {
  32. return;
  33. }
  34. $this->updateParentReplyCount();
  35. $this->generateNotification();
  36. if($this->groupPost) {
  37. $this->updateChildReplyCount();
  38. }
  39. }
  40. protected function updateParentReplyCount()
  41. {
  42. $parent = $this->status;
  43. $parent->reply_count = Status::whereInReplyToId($parent->id)->count();
  44. $parent->save();
  45. StatusService::del($parent->id);
  46. }
  47. protected function updateChildReplyCount()
  48. {
  49. $gp = $this->groupPost;
  50. if($gp->reply_child_id) {
  51. $parent = GroupPost::whereStatusId($gp->reply_child_id)->first();
  52. if($parent) {
  53. $parent->reply_count++;
  54. $parent->save();
  55. }
  56. }
  57. }
  58. protected function generateNotification()
  59. {
  60. $status = $this->status;
  61. $comment = $this->comment;
  62. $target = $status->profile;
  63. $actor = $comment->profile;
  64. if ($actor->id == $target->id || $status->comments_disabled == true) {
  65. return;
  66. }
  67. $notification = DB::transaction(function() use($target, $actor, $comment) {
  68. $actorName = $actor->username;
  69. $actorUrl = $actor->url();
  70. $text = "{$actorName} commented on your group post.";
  71. $html = "<a href='{$actorUrl}' class='profile-link'>{$actorName}</a> commented on your group post.";
  72. $notification = new Notification();
  73. $notification->profile_id = $target->id;
  74. $notification->actor_id = $actor->id;
  75. $notification->action = 'group:comment';
  76. $notification->item_id = $comment->id;
  77. $notification->item_type = "App\Status";
  78. $notification->save();
  79. return $notification;
  80. });
  81. NotificationService::setNotification($notification);
  82. NotificationService::set($notification->profile_id, $notification->id);
  83. }
  84. }