123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace App\Jobs\StatusPipeline;
- use App\Hashtag;
- use App\Jobs\MentionPipeline\MentionPipeline;
- use App\Mention;
- use App\Profile;
- use App\Status;
- use App\StatusHashtag;
- use App\Services\PublicTimelineService;
- use App\Util\Lexer\Autolink;
- use App\Util\Lexer\Extractor;
- use App\Util\Sentiment\Bouncer;
- use DB;
- use Illuminate\Bus\Queueable;
- use Illuminate\Contracts\Queue\ShouldQueue;
- use Illuminate\Foundation\Bus\Dispatchable;
- use Illuminate\Queue\InteractsWithQueue;
- use Illuminate\Queue\SerializesModels;
- class StatusEntityLexer implements ShouldQueue
- {
- use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
- protected $status;
- protected $entities;
- protected $autolink;
- /**
- * Delete the job if its models no longer exist.
- *
- * @var bool
- */
- public $deleteWhenMissingModels = true;
- /**
- * Create a new job instance.
- *
- * @return void
- */
- public function __construct(Status $status)
- {
- $this->status = $status;
- }
- /**
- * Execute the job.
- *
- * @return void
- */
- public function handle()
- {
- $profile = $this->status->profile;
- $count = $profile->statuses()
- ->getQuery()
- ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
- ->whereNull('in_reply_to_id')
- ->whereNull('reblog_of_id')
- ->count();
- $profile->status_count = $count;
- $profile->save();
- if($profile->no_autolink == false) {
- $this->parseEntities();
- }
- }
- public function parseEntities()
- {
- $this->extractEntities();
- }
- public function extractEntities()
- {
- $this->entities = Extractor::create()->extract($this->status->caption);
- $this->autolinkStatus();
- }
- public function autolinkStatus()
- {
- $this->autolink = Autolink::create()->autolink($this->status->caption);
- $this->storeEntities();
- }
- public function storeEntities()
- {
- $this->storeHashtags();
- DB::transaction(function () {
- $status = $this->status;
- $status->rendered = nl2br($this->autolink);
- $status->entities = json_encode($this->entities);
- $status->save();
- });
- }
- public function storeHashtags()
- {
- $tags = array_unique($this->entities['hashtags']);
- $status = $this->status;
- foreach ($tags as $tag) {
- if(mb_strlen($tag) > 124) {
- continue;
- }
- DB::transaction(function () use ($status, $tag) {
- $slug = str_slug($tag, '-', false);
- $hashtag = Hashtag::firstOrCreate(
- ['name' => $tag, 'slug' => $slug]
- );
- StatusHashtag::firstOrCreate(
- [
- 'status_id' => $status->id,
- 'hashtag_id' => $hashtag->id,
- 'profile_id' => $status->profile_id,
- 'status_visibility' => $status->visibility,
- ]
- );
- });
- }
- $this->storeMentions();
- }
- public function storeMentions()
- {
- $mentions = array_unique($this->entities['mentions']);
- $status = $this->status;
- foreach ($mentions as $mention) {
- $mentioned = Profile::whereUsername($mention)->first();
- if (empty($mentioned) || !isset($mentioned->id)) {
- continue;
- }
- DB::transaction(function () use ($status, $mentioned) {
- $m = new Mention();
- $m->status_id = $status->id;
- $m->profile_id = $mentioned->id;
- $m->save();
- MentionPipeline::dispatch($status, $m);
- });
- }
- $this->deliver();
- }
- public function deliver()
- {
- $status = $this->status;
- $types = [
- 'photo',
- 'photo:album',
- 'video',
- 'video:album',
- 'photo:video:album'
- ];
- if(config_cache('pixelfed.bouncer.enabled')) {
- Bouncer::get($status);
- }
- if($status->uri == null && $status->scope == 'public' && in_array($status->type, $types)) {
- PublicTimelineService::add($status->id);
- }
- if(config_cache('federation.activitypub.enabled') == true && config('app.env') == 'production') {
- StatusActivityPubDeliver::dispatch($status);
- }
- }
- }
|