1
0

NewStatusPipeline.php 3.2 KB

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