Update StatusEntityLexer

This commit is contained in:
Daniel Supernault 2018-06-13 19:47:08 -06:00
parent 231884c770
commit 596f335dc6

View file

@ -6,21 +6,28 @@ use Cache;
use App\{ use App\{
Hashtag, Hashtag,
Media, Media,
Mention,
Profile,
Status, Status,
StatusHashtag StatusHashtag
}; };
use App\Util\Lexer\Hashtag as HashtagLexer; use App\Util\Lexer\Hashtag as HashtagLexer;
use App\Util\Lexer\{Autolink, Extractor};
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use App\Jobs\MentionPipeline\MentionPipeline;
class StatusEntityLexer implements ShouldQueue class StatusEntityLexer implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $status; protected $status;
protected $entities;
protected $autolink;
/** /**
* Create a new job instance. * Create a new job instance.
* *
@ -39,22 +46,40 @@ class StatusEntityLexer implements ShouldQueue
public function handle() public function handle()
{ {
$status = $this->status; $status = $this->status;
$this->parseHashtags(); $this->parseEntities();
} }
public function parseHashtags() 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()
{ {
$status = $this->status; $status = $this->status;
$text = e($status->caption); $this->storeHashtags();
$tags = HashtagLexer::getHashtags($text); $this->storeMentions();
$rendered = $text; $status->rendered = $this->autolink;
if(count($tags) > 0) { $status->entities = json_encode($this->entities);
$rendered = HashtagLexer::replaceHashtagsWithLinks($text);
}
$status->rendered = $rendered;
$status->save(); $status->save();
}
Cache::forever('post.' . $status->id, $status); public function storeHashtags()
{
$tags = array_unique($this->entities['hashtags']);
$status = $this->status;
foreach($tags as $tag) { foreach($tags as $tag) {
$slug = str_slug($tag); $slug = str_slug($tag);
@ -64,11 +89,32 @@ class StatusEntityLexer implements ShouldQueue
['slug' => $slug] ['slug' => $slug]
); );
$stag = new StatusHashtag; StatusHashtag::firstOrCreate(
$stag->status_id = $status->id; ['status_id' => $status->id],
$stag->hashtag_id = $htag->id; ['hashtag_id' => $htag->id]
$stag->save(); );
} }
} }
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;
}
$m = new Mention;
$m->status_id = $status->id;
$m->profile_id = $mentioned->id;
$m->save();
MentionPipeline::dispatch($status, $m);
}
}
} }