NewCommentPipeline.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace App\Jobs\GroupsPipeline;
  3. use App\Util\Media\Image;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use App\Models\Group;
  10. use App\Models\GroupComment;
  11. use App\Models\GroupPost;
  12. use App\Models\GroupHashtag;
  13. use App\Models\GroupPostHashtag;
  14. use App\Util\Lexer\Autolink;
  15. use App\Util\Lexer\Extractor;
  16. use DB;
  17. class NewCommentPipeline implements ShouldQueue
  18. {
  19. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  20. protected $status;
  21. protected $parent;
  22. protected $entities;
  23. protected $autolink;
  24. /**
  25. * Delete the job if its models no longer exist.
  26. *
  27. * @var bool
  28. */
  29. public $deleteWhenMissingModels = true;
  30. /**
  31. * Create a new job instance.
  32. *
  33. * @return void
  34. */
  35. public function __construct($parent, GroupComment $status)
  36. {
  37. $this->parent = $parent;
  38. $this->status = $status;
  39. }
  40. /**
  41. * Execute the job.
  42. *
  43. * @return void
  44. */
  45. public function handle()
  46. {
  47. $profile = $this->status->profile;
  48. $status = $this->status;
  49. $parent = $this->parent;
  50. $parent->reply_count = GroupComment::whereStatusId($parent->id)->count();
  51. $parent->save();
  52. if ($profile->no_autolink == false) {
  53. $this->parseEntities();
  54. }
  55. }
  56. public function parseEntities()
  57. {
  58. $this->extractEntities();
  59. }
  60. public function extractEntities()
  61. {
  62. $this->entities = Extractor::create()->extract($this->status->caption);
  63. $this->autolinkStatus();
  64. }
  65. public function autolinkStatus()
  66. {
  67. $this->autolink = Autolink::create()->autolink($this->status->caption);
  68. $this->storeHashtags();
  69. }
  70. public function storeHashtags()
  71. {
  72. $tags = array_unique($this->entities['hashtags']);
  73. $status = $this->status;
  74. foreach ($tags as $tag) {
  75. if (mb_strlen($tag) > 124) {
  76. continue;
  77. }
  78. DB::transaction(function () use ($status, $tag) {
  79. $hashtag = GroupHashtag::firstOrCreate([
  80. 'name' => $tag,
  81. ]);
  82. GroupPostHashtag::firstOrCreate(
  83. [
  84. 'status_id' => $status->id,
  85. 'group_id' => $status->group_id,
  86. 'hashtag_id' => $hashtag->id,
  87. 'profile_id' => $status->profile_id,
  88. 'status_visibility' => $status->visibility,
  89. ]
  90. );
  91. });
  92. }
  93. $this->storeMentions();
  94. }
  95. public function storeMentions()
  96. {
  97. // todo
  98. }
  99. }