NewPostPipeline.php 2.5 KB

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