NewStatusPipeline.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. namespace App\Jobs\GroupPipeline;
  3. use App\Notification;
  4. use App\Hashtag;
  5. use App\Mention;
  6. use App\Profile;
  7. use App\Status;
  8. use App\StatusHashtag;
  9. use App\Models\GroupPostHashtag;
  10. use App\Models\GroupPost;
  11. use Cache;
  12. use DB;
  13. use Illuminate\Bus\Queueable;
  14. use Illuminate\Contracts\Queue\ShouldQueue;
  15. use Illuminate\Foundation\Bus\Dispatchable;
  16. use Illuminate\Queue\InteractsWithQueue;
  17. use Illuminate\Queue\SerializesModels;
  18. use Illuminate\Support\Facades\Redis;
  19. use App\Services\MediaStorageService;
  20. use App\Services\NotificationService;
  21. use App\Services\StatusService;
  22. use App\Util\Lexer\Autolink;
  23. use App\Util\Lexer\Extractor;
  24. class NewStatusPipeline implements ShouldQueue
  25. {
  26. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  27. protected $status;
  28. protected $gp;
  29. protected $tags;
  30. protected $mentions;
  31. public function __construct(Status $status, GroupPost $gp)
  32. {
  33. $this->status = $status;
  34. $this->gp = $gp;
  35. }
  36. public function handle()
  37. {
  38. $status = $this->status;
  39. $autolink = Autolink::create()
  40. ->setAutolinkActiveUsersOnly(true)
  41. ->setBaseHashPath("/groups/{$status->group_id}/topics/")
  42. ->setBaseUserPath("/groups/{$status->group_id}/username/")
  43. ->autolink($status->caption);
  44. $entities = Extractor::create()->extract($status->caption);
  45. $autolink = str_replace('/discover/tags/', '/groups/' . $status->group_id . '/topics/', $autolink);
  46. $status->rendered = nl2br($autolink);
  47. $status->entities = null;
  48. $status->save();
  49. $this->tags = array_unique($entities['hashtags']);
  50. $this->mentions = array_unique($entities['mentions']);
  51. if(count($this->tags)) {
  52. $this->storeHashtags();
  53. }
  54. if(count($this->mentions)) {
  55. $this->storeMentions($this->mentions);
  56. }
  57. }
  58. protected function storeHashtags()
  59. {
  60. $tags = $this->tags;
  61. $status = $this->status;
  62. $gp = $this->gp;
  63. foreach ($tags as $tag) {
  64. if(mb_strlen($tag) > 124) {
  65. continue;
  66. }
  67. DB::transaction(function () use ($status, $tag, $gp) {
  68. $slug = str_slug($tag, '-', false);
  69. $hashtag = Hashtag::firstOrCreate(
  70. ['name' => $tag, 'slug' => $slug]
  71. );
  72. GroupPostHashtag::firstOrCreate(
  73. [
  74. 'group_id' => $status->group_id,
  75. 'group_post_id' => $gp->id,
  76. 'status_id' => $status->id,
  77. 'hashtag_id' => $hashtag->id,
  78. 'profile_id' => $status->profile_id,
  79. ]
  80. );
  81. });
  82. }
  83. if(count($this->mentions)) {
  84. $this->storeMentions();
  85. }
  86. StatusService::del($status->id);
  87. }
  88. protected function storeMentions()
  89. {
  90. $mentions = $this->mentions;
  91. $status = $this->status;
  92. foreach ($mentions as $mention) {
  93. $mentioned = Profile::whereUsername($mention)->first();
  94. if (empty($mentioned) || !isset($mentioned->id)) {
  95. continue;
  96. }
  97. DB::transaction(function () use ($status, $mentioned) {
  98. $m = new Mention();
  99. $m->status_id = $status->id;
  100. $m->profile_id = $mentioned->id;
  101. $m->save();
  102. MentionPipeline::dispatch($status, $m);
  103. });
  104. }
  105. StatusService::del($status->id);
  106. }
  107. }