1
0

StatusEntityLexer.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. class StatusEntityLexer implements ShouldQueue
  22. {
  23. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  24. protected $status;
  25. protected $entities;
  26. protected $autolink;
  27. /**
  28. * Delete the job if its models no longer exist.
  29. *
  30. * @var bool
  31. */
  32. public $deleteWhenMissingModels = true;
  33. /**
  34. * Create a new job instance.
  35. *
  36. * @return void
  37. */
  38. public function __construct(Status $status)
  39. {
  40. $this->status = $status;
  41. }
  42. /**
  43. * Execute the job.
  44. *
  45. * @return void
  46. */
  47. public function handle()
  48. {
  49. $profile = $this->status->profile;
  50. $status = $this->status;
  51. if(in_array($status->type, ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])) {
  52. $profile->status_count = $profile->status_count + 1;
  53. $profile->save();
  54. }
  55. if($profile->no_autolink == false) {
  56. $this->parseEntities();
  57. }
  58. }
  59. public function parseEntities()
  60. {
  61. $this->extractEntities();
  62. }
  63. public function extractEntities()
  64. {
  65. $this->entities = Extractor::create()->extract($this->status->caption);
  66. $this->autolinkStatus();
  67. }
  68. public function autolinkStatus()
  69. {
  70. $this->autolink = Autolink::create()->autolink($this->status->caption);
  71. $this->storeEntities();
  72. }
  73. public function storeEntities()
  74. {
  75. $this->storeHashtags();
  76. DB::transaction(function () {
  77. $status = $this->status;
  78. $status->rendered = nl2br($this->autolink);
  79. $status->entities = json_encode($this->entities);
  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. PublicTimelineService::add($status->id);
  157. }
  158. if(config_cache('federation.activitypub.enabled') == true && config('app.env') == 'production') {
  159. StatusActivityPubDeliver::dispatch($status);
  160. }
  161. }
  162. }