1
0
Fork 0
forked from mirror/pixelfed
pixelfed/app/Jobs/StatusPipeline/StatusEntityLexer.php

128 lines
3.1 KiB
PHP
Raw Normal View History

2018-06-01 03:14:46 +00:00
<?php
namespace App\Jobs\StatusPipeline;
2018-08-28 03:07:36 +00:00
use App\Hashtag;
use App\Jobs\MentionPipeline\MentionPipeline;
use App\Mention;
use App\Profile;
use App\Status;
use App\StatusHashtag;
use App\Util\Lexer\Autolink;
use App\Util\Lexer\Extractor;
use DB;
2018-06-01 03:14:46 +00:00
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
2018-08-28 03:07:36 +00:00
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
2018-06-01 03:14:46 +00:00
class StatusEntityLexer implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $status;
2018-06-14 01:47:08 +00:00
protected $entities;
protected $autolink;
2019-01-12 22:19:57 +00:00
/**
* Delete the job if its models no longer exist.
*
* @var bool
*/
public $deleteWhenMissingModels = true;
2018-06-01 03:14:46 +00:00
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Status $status)
{
$this->status = $status;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
2019-01-11 03:44:22 +00:00
$profile = $this->status->profile;
if($profile->no_autolink == false) {
$this->parseEntities();
}
2018-06-14 01:47:08 +00:00
}
public function parseEntities()
{
$this->extractEntities();
}
public function extractEntities()
{
$this->entities = Extractor::create()->extract($this->status->caption);
2018-08-28 03:07:36 +00:00
$this->autolinkStatus();
2018-06-01 03:14:46 +00:00
}
2018-06-14 01:47:08 +00:00
public function autolinkStatus()
{
$this->autolink = Autolink::create()->autolink($this->status->caption);
$this->storeEntities();
}
public function storeEntities()
2018-06-01 03:14:46 +00:00
{
2018-06-14 01:47:08 +00:00
$this->storeHashtags();
$this->storeMentions();
2018-07-23 18:53:33 +00:00
DB::transaction(function () {
$status = $this->status;
2018-11-17 20:34:17 +00:00
$status->rendered = nl2br($this->autolink);
2018-07-23 18:53:33 +00:00
$status->entities = json_encode($this->entities);
$status->save();
});
2018-06-14 01:47:08 +00:00
}
public function storeHashtags()
{
$tags = array_unique($this->entities['hashtags']);
$status = $this->status;
2018-06-01 03:14:46 +00:00
2018-08-28 03:07:36 +00:00
foreach ($tags as $tag) {
2018-07-23 18:53:33 +00:00
DB::transaction(function () use ($status, $tag) {
2018-11-27 03:26:27 +00:00
$slug = str_slug($tag, '-', false);
2018-07-23 18:53:33 +00:00
$hashtag = Hashtag::firstOrCreate(
['name' => $tag, 'slug' => $slug]
);
StatusHashtag::firstOrCreate(
['status_id' => $status->id, 'hashtag_id' => $hashtag->id]
);
});
2018-06-01 03:14:46 +00:00
}
2018-06-14 01:47:08 +00:00
}
public function storeMentions()
{
$mentions = array_unique($this->entities['mentions']);
$status = $this->status;
2018-08-28 03:07:36 +00:00
foreach ($mentions as $mention) {
2018-07-23 18:53:33 +00:00
$mentioned = Profile::whereUsername($mention)->firstOrFail();
2018-08-28 03:07:36 +00:00
if (empty($mentioned) || !isset($mentioned->id)) {
2018-06-14 01:47:08 +00:00
continue;
}
2018-08-10 02:22:32 +00:00
2018-07-23 18:53:33 +00:00
DB::transaction(function () use ($status, $mentioned) {
2018-08-28 03:07:36 +00:00
$m = new Mention();
2018-07-23 18:53:33 +00:00
$m->status_id = $status->id;
$m->profile_id = $mentioned->id;
$m->save();
2018-08-28 03:07:36 +00:00
2018-08-10 02:22:32 +00:00
MentionPipeline::dispatch($status, $m);
2018-07-23 18:53:33 +00:00
});
2018-06-14 01:47:08 +00:00
}
2018-06-01 03:14:46 +00:00
}
}