Browse Source

Add StatusPipeline

Daniel Supernault 7 years ago
parent
commit
ec2bff95b6

+ 52 - 0
app/Jobs/StatusPipeline/NewStatusPipeline.php

@@ -0,0 +1,52 @@
+<?php
+
+namespace App\Jobs\StatusPipeline;
+
+use Cache, Redis;
+use App\{Media, Status};
+use App\Jobs\ImageOptimizePipeline\ImageOptimize;
+use Illuminate\Bus\Queueable;
+use Illuminate\Queue\SerializesModels;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+
+class NewStatusPipeline implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+    protected $status;
+    protected $media;
+
+    /**
+     * Create a new job instance.
+     *
+     * @return void
+     */
+    public function __construct(Status $status, $media = false)
+    {
+        $this->status = $status;
+        $this->media = $media;
+    }
+
+    /**
+     * Execute the job.
+     *
+     * @return void
+     */
+    public function handle()
+    {
+        $status = $this->status;
+        $media = $this->media;
+
+        StatusEntityLexer::dispatch($status);
+        StatusActivityPubDeliver::dispatch($status);
+        if($media) {
+            ImageOptimize::dispatch($media);
+        }
+        Cache::forever('post.' . $status->id, $status);
+        
+        $redis = Redis::connection();
+        $redis->lpush(config('cache.prefix').':user.' . $status->profile_id . '.posts', $status->id);
+    }
+}

+ 38 - 0
app/Jobs/StatusPipeline/StatusActivityPubDeliver.php

@@ -0,0 +1,38 @@
+<?php
+
+namespace App\Jobs\StatusPipeline;
+
+use App\{Media, Status};
+use Illuminate\Bus\Queueable;
+use Illuminate\Queue\SerializesModels;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+
+class StatusActivityPubDeliver implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+    protected $status;
+    /**
+     * 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;
+
+        // todo: fanout on write
+    }
+}

+ 74 - 0
app/Jobs/StatusPipeline/StatusEntityLexer.php

@@ -0,0 +1,74 @@
+<?php
+
+namespace App\Jobs\StatusPipeline;
+
+use Cache;
+use App\{
+    Hashtag,
+    Media,
+    Status,
+    StatusHashtag
+};
+use App\Util\Lexer\Hashtag as HashtagLexer;
+use Illuminate\Bus\Queueable;
+use Illuminate\Queue\SerializesModels;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+
+class StatusEntityLexer implements ShouldQueue
+{
+    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+    protected $status;
+    /**
+     * 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;
+        $this->parseHashtags();
+    }
+
+    public function parseHashtags()
+    {
+        $status = $this->status;
+        $text = $status->caption;
+        $tags = HashtagLexer::getHashtags($text);
+        $rendered = $text;
+        if(count($tags) > 0) {
+            $rendered = HashtagLexer::replaceHashtagsWithLinks($text);
+        }
+        $status->rendered = $rendered;
+        $status->save();
+        
+        Cache::forever('post.' . $status->id, $status);
+
+        foreach($tags as $tag) {
+            $slug = str_slug($tag);
+            
+            $htag = Hashtag::firstOrCreate(
+                ['name' => $tag],
+                ['slug' => $slug]
+            );
+
+            $stag = new StatusHashtag;
+            $stag->status_id = $status->id;
+            $stag->hashtag_id = $htag->id;
+            $stag->save();
+        }
+
+    }
+}