StatusEntityLexer.php 4.1 KB

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