StatusEntityLexer.php 4.7 KB

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