StatusEntityLexer.php 4.2 KB

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