1
0
Fork 1
mirror of https://github.com/pixelfed/pixelfed.git synced 2024-12-26 17:56:48 +00:00
pixelfed/app/Jobs/StatusPipeline/StatusEntityLexer.php

119 lines
2.9 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;
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);
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) {
$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;
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
}
}