StatusEntityLexer.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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::firstOrCreate(
  95. ['name' => $tag, 'slug' => $slug]
  96. );
  97. StatusHashtag::firstOrCreate(
  98. [
  99. 'status_id' => $status->id,
  100. 'hashtag_id' => $hashtag->id,
  101. 'profile_id' => $status->profile_id,
  102. 'status_visibility' => $status->visibility,
  103. ]
  104. );
  105. });
  106. }
  107. $this->storeMentions();
  108. }
  109. public function storeMentions()
  110. {
  111. $mentions = array_unique($this->entities['mentions']);
  112. $status = $this->status;
  113. foreach ($mentions as $mention) {
  114. $mentioned = Profile::whereUsername($mention)->first();
  115. if (empty($mentioned) || !isset($mentioned->id)) {
  116. continue;
  117. }
  118. DB::transaction(function () use ($status, $mentioned) {
  119. $m = new Mention();
  120. $m->status_id = $status->id;
  121. $m->profile_id = $mentioned->id;
  122. $m->save();
  123. MentionPipeline::dispatch($status, $m);
  124. });
  125. }
  126. $this->deliver();
  127. }
  128. public function deliver()
  129. {
  130. $status = $this->status;
  131. $types = [
  132. 'photo',
  133. 'photo:album',
  134. 'video',
  135. 'video:album',
  136. 'photo:video:album'
  137. ];
  138. if(config_cache('pixelfed.bouncer.enabled')) {
  139. Bouncer::get($status);
  140. }
  141. if($status->uri == null && $status->scope == 'public' && in_array($status->type, $types)) {
  142. PublicTimelineService::add($status->id);
  143. }
  144. if(config_cache('federation.activitypub.enabled') == true && config('app.env') == 'production') {
  145. StatusActivityPubDeliver::dispatch($status);
  146. }
  147. }
  148. }