StatusEntityLexer.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Jobs\StatusPipeline;
  3. use App\Hashtag;
  4. use App\Jobs\MentionPipeline\MentionPipeline;
  5. use App\Mention;
  6. use App\Profile;
  7. use App\Status;
  8. use App\StatusHashtag;
  9. use App\Util\Lexer\Autolink;
  10. use App\Util\Lexer\Extractor;
  11. use DB;
  12. use Illuminate\Bus\Queueable;
  13. use Illuminate\Contracts\Queue\ShouldQueue;
  14. use Illuminate\Foundation\Bus\Dispatchable;
  15. use Illuminate\Queue\InteractsWithQueue;
  16. use Illuminate\Queue\SerializesModels;
  17. class StatusEntityLexer implements ShouldQueue
  18. {
  19. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  20. protected $status;
  21. protected $entities;
  22. protected $autolink;
  23. /**
  24. * Delete the job if its models no longer exist.
  25. *
  26. * @var bool
  27. */
  28. public $deleteWhenMissingModels = true;
  29. /**
  30. * Create a new job instance.
  31. *
  32. * @return void
  33. */
  34. public function __construct(Status $status)
  35. {
  36. $this->status = $status;
  37. }
  38. /**
  39. * Execute the job.
  40. *
  41. * @return void
  42. */
  43. public function handle()
  44. {
  45. $profile = $this->status->profile;
  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->storeEntities();
  63. }
  64. public function storeEntities()
  65. {
  66. $this->storeHashtags();
  67. DB::transaction(function () {
  68. $status = $this->status;
  69. $status->rendered = nl2br($this->autolink);
  70. $status->entities = json_encode($this->entities);
  71. $status->save();
  72. });
  73. }
  74. public function storeHashtags()
  75. {
  76. $tags = array_unique($this->entities['hashtags']);
  77. $status = $this->status;
  78. foreach ($tags as $tag) {
  79. DB::transaction(function () use ($status, $tag) {
  80. $slug = str_slug($tag, '-', false);
  81. $hashtag = Hashtag::firstOrCreate(
  82. ['name' => $tag, 'slug' => $slug]
  83. );
  84. StatusHashtag::firstOrCreate(
  85. [
  86. 'status_id' => $status->id,
  87. 'hashtag_id' => $hashtag->id,
  88. 'profile_id' => $status->profile_id
  89. ]
  90. );
  91. });
  92. }
  93. $this->storeMentions();
  94. }
  95. public function storeMentions()
  96. {
  97. $mentions = array_unique($this->entities['mentions']);
  98. $status = $this->status;
  99. foreach ($mentions as $mention) {
  100. $mentioned = Profile::whereNull('domain')->whereUsername($mention)->firstOrFail();
  101. if (empty($mentioned) || !isset($mentioned->id)) {
  102. continue;
  103. }
  104. DB::transaction(function () use ($status, $mentioned) {
  105. $m = new Mention();
  106. $m->status_id = $status->id;
  107. $m->profile_id = $mentioned->id;
  108. $m->save();
  109. MentionPipeline::dispatch($status, $m);
  110. });
  111. }
  112. $this->deliver();
  113. }
  114. public function deliver()
  115. {
  116. if(config('pixelfed.activitypub_enabled') == true) {
  117. StatusActivityPubDeliver::dispatch($this->status);
  118. }
  119. }
  120. }