StatusEntityLexer.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. $count = $profile->statuses()
  49. ->getQuery()
  50. ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
  51. ->whereNull('in_reply_to_id')
  52. ->whereNull('reblog_of_id')
  53. ->count();
  54. $profile->status_count = $count;
  55. $profile->save();
  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->entities = json_encode($this->entities);
  81. $status->save();
  82. });
  83. }
  84. public function storeHashtags()
  85. {
  86. $tags = array_unique($this->entities['hashtags']);
  87. $status = $this->status;
  88. foreach ($tags as $tag) {
  89. if(mb_strlen($tag) > 124) {
  90. continue;
  91. }
  92. DB::transaction(function () use ($status, $tag) {
  93. $slug = str_slug($tag, '-', false);
  94. $hashtag = Hashtag::where('slug', $slug)->first();
  95. if (!$hashtag) {
  96. $hashtag = Hashtag::create(
  97. ['name' => $tag, 'slug' => $slug]
  98. );
  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. DB::transaction(function () use ($status, $mentioned) {
  122. $m = new Mention();
  123. $m->status_id = $status->id;
  124. $m->profile_id = $mentioned->id;
  125. $m->save();
  126. MentionPipeline::dispatch($status, $m);
  127. });
  128. }
  129. $this->deliver();
  130. }
  131. public function deliver()
  132. {
  133. $status = $this->status;
  134. $types = [
  135. 'photo',
  136. 'photo:album',
  137. 'video',
  138. 'video:album',
  139. 'photo:video:album'
  140. ];
  141. if(config_cache('pixelfed.bouncer.enabled')) {
  142. Bouncer::get($status);
  143. }
  144. if( $status->uri == null &&
  145. $status->scope == 'public' &&
  146. in_array($status->type, $types) &&
  147. $status->in_reply_to_id === null &&
  148. $status->reblog_of_id === null
  149. ) {
  150. PublicTimelineService::add($status->id);
  151. }
  152. if(config_cache('federation.activitypub.enabled') == true && config('app.env') == 'production') {
  153. StatusActivityPubDeliver::dispatch($status);
  154. }
  155. }
  156. }