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

121 lines
2.7 KiB
PHP
Raw Normal View History

2018-06-01 03:14:46 +00:00
<?php
namespace App\Jobs\StatusPipeline;
use Cache;
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
{
$status = $this->status;
2018-06-14 01:47:08 +00:00
$this->storeHashtags();
$this->storeMentions();
$status->rendered = $this->autolink;
$status->entities = json_encode($this->entities);
2018-06-01 03:14:46 +00:00
$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) {
$slug = str_slug($tag);
$htag = Hashtag::firstOrCreate(
['name' => $tag],
['slug' => $slug]
);
2018-06-14 01:47:08 +00:00
StatusHashtag::firstOrCreate(
['status_id' => $status->id],
['hashtag_id' => $htag->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) {
$mentioned = Profile::whereUsername($mention)->first();
if(empty($mentioned) || !isset($mentioned->id)) {
continue;
}
2018-06-01 03:14:46 +00:00
2018-06-14 01:47:08 +00:00
$m = new Mention;
$m->status_id = $status->id;
$m->profile_id = $mentioned->id;
$m->save();
MentionPipeline::dispatch($status, $m);
}
2018-06-01 03:14:46 +00:00
}
2018-06-14 01:47:08 +00:00
2018-06-01 03:14:46 +00:00
}