StatusEntityLexer.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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\Services\PublicTimelineService;
  10. use App\Util\Lexer\Autolink;
  11. use App\Util\Lexer\Extractor;
  12. use App\Util\Sentiment\Bouncer;
  13. use DB;
  14. use Illuminate\Bus\Queueable;
  15. use Illuminate\Contracts\Queue\ShouldQueue;
  16. use Illuminate\Foundation\Bus\Dispatchable;
  17. use Illuminate\Queue\InteractsWithQueue;
  18. use Illuminate\Queue\SerializesModels;
  19. class StatusEntityLexer implements ShouldQueue
  20. {
  21. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  22. protected $status;
  23. protected $entities;
  24. protected $autolink;
  25. /**
  26. * Delete the job if its models no longer exist.
  27. *
  28. * @var bool
  29. */
  30. public $deleteWhenMissingModels = true;
  31. /**
  32. * Create a new job instance.
  33. *
  34. * @return void
  35. */
  36. public function __construct(Status $status)
  37. {
  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. if(in_array($status->type, ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])) {
  50. $profile->status_count = $profile->status_count + 1;
  51. $profile->save();
  52. }
  53. if($profile->no_autolink == false) {
  54. $this->parseEntities();
  55. }
  56. }
  57. public function parseEntities()
  58. {
  59. $this->extractEntities();
  60. }
  61. public function extractEntities()
  62. {
  63. $this->entities = Extractor::create()->extract($this->status->caption);
  64. $this->autolinkStatus();
  65. }
  66. public function autolinkStatus()
  67. {
  68. $this->autolink = Autolink::create()->autolink($this->status->caption);
  69. $this->storeEntities();
  70. }
  71. public function storeEntities()
  72. {
  73. $this->storeHashtags();
  74. DB::transaction(function () {
  75. $status = $this->status;
  76. $status->rendered = nl2br($this->autolink);
  77. $status->entities = json_encode($this->entities);
  78. $status->save();
  79. });
  80. }
  81. public function storeHashtags()
  82. {
  83. $tags = array_unique($this->entities['hashtags']);
  84. $status = $this->status;
  85. foreach ($tags as $tag) {
  86. if(mb_strlen($tag) > 124) {
  87. continue;
  88. }
  89. DB::transaction(function () use ($status, $tag) {
  90. $slug = str_slug($tag, '-', false);
  91. $hashtag = Hashtag::where('slug', $slug)->first();
  92. if (!$hashtag) {
  93. $hashtag = Hashtag::create(
  94. ['name' => $tag, 'slug' => $slug]
  95. );
  96. }
  97. StatusHashtag::firstOrCreate(
  98. [
  99. 'status_id' => $status->id,
  100. 'hashtag_id' => $hashtag->id,
  101. 'profile_id' => $status->profile_id,
  102. 'status_visibility' => $status->visibility,
  103. ]
  104. );
  105. });
  106. }
  107. $this->storeMentions();
  108. }
  109. public function storeMentions()
  110. {
  111. $mentions = array_unique($this->entities['mentions']);
  112. $status = $this->status;
  113. foreach ($mentions as $mention) {
  114. $mentioned = Profile::whereUsername($mention)->first();
  115. if (empty($mentioned) || !isset($mentioned->id)) {
  116. continue;
  117. }
  118. DB::transaction(function () use ($status, $mentioned) {
  119. $m = new Mention();
  120. $m->status_id = $status->id;
  121. $m->profile_id = $mentioned->id;
  122. $m->save();
  123. MentionPipeline::dispatch($status, $m);
  124. });
  125. }
  126. $this->deliver();
  127. }
  128. public function deliver()
  129. {
  130. $status = $this->status;
  131. $types = [
  132. 'photo',
  133. 'photo:album',
  134. 'video',
  135. 'video:album',
  136. 'photo:video:album'
  137. ];
  138. if(config_cache('pixelfed.bouncer.enabled')) {
  139. Bouncer::get($status);
  140. }
  141. if( $status->uri == null &&
  142. $status->scope == 'public' &&
  143. in_array($status->type, $types) &&
  144. $status->in_reply_to_id === null &&
  145. $status->reblog_of_id === null
  146. ) {
  147. PublicTimelineService::add($status->id);
  148. }
  149. if(config_cache('federation.activitypub.enabled') == true && config('app.env') == 'production') {
  150. StatusActivityPubDeliver::dispatch($status);
  151. }
  152. }
  153. }