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

123 lines
3 KiB
PHP
Raw Normal View History

2018-06-01 03:14:46 +00:00
<?php
namespace App\Jobs\StatusPipeline;
2018-07-23 18:53:33 +00:00
use DB, Cache;
2018-06-01 03:14:46 +00:00
use App\{
Hashtag,
Media,
2018-06-14 01:47:08 +00:00
Mention,
Profile,
2018-06-01 03:14:46 +00:00
Status,
StatusHashtag
};
use App\Util\Lexer\Hashtag as HashtagLexer;
2018-06-14 01:47:08 +00:00
use App\Util\Lexer\{Autolink, Extractor};
2018-06-01 03:14:46 +00:00
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
2018-06-14 01:47:08 +00:00
use App\Jobs\MentionPipeline\MentionPipeline;
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;
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()
{
$status = $this->status;
2018-06-14 01:47:08 +00:00
$this->parseEntities();
}
public function parseEntities()
{
$this->extractEntities();
}
public function extractEntities()
{
$this->entities = Extractor::create()->extract($this->status->caption);
$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;
$status->rendered = $this->autolink;
$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
foreach($tags as $tag) {
2018-07-23 18:53:33 +00:00
DB::transaction(function () use ($status, $tag) {
$slug = str_slug($tag);
$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;
foreach($mentions as $mention) {
2018-07-23 18:53:33 +00:00
$mentioned = Profile::whereUsername($mention)->firstOrFail();
2018-06-14 01:47:08 +00:00
if(empty($mentioned) || !isset($mentioned->id)) {
continue;
}
2018-08-10 02:22:32 +00:00
2018-07-23 18:53:33 +00:00
DB::transaction(function () use ($status, $mentioned) {
$m = new Mention;
$m->status_id = $status->id;
$m->profile_id = $mentioned->id;
$m->save();
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
}
2018-06-14 01:47:08 +00:00
2018-06-01 03:14:46 +00:00
}