Sfoglia il codice sorgente

Update StatusTagsPipeline, process federated hashtags and mentions

Daniel Supernault 3 anni fa
parent
commit
a84b173678
1 ha cambiato i file con 56 aggiunte e 5 eliminazioni
  1. 56 5
      app/Jobs/StatusPipeline/StatusTagsPipeline.php

+ 56 - 5
app/Jobs/StatusPipeline/StatusTagsPipeline.php

@@ -10,6 +10,11 @@ use Illuminate\Queue\InteractsWithQueue;
 use Illuminate\Queue\SerializesModels;
 use App\Services\CustomEmojiService;
 use App\Services\StatusService;
+use App\Jobs\MentionPipeline\MentionPipeline;
+use App\Mention;
+use App\Services\AccountService;
+use App\Hashtag;
+use App\StatusHashtag;
 
 class StatusTagsPipeline implements ShouldQueue
 {
@@ -37,15 +42,61 @@ class StatusTagsPipeline implements ShouldQueue
 	public function handle()
 	{
 		$res = $this->activity;
+		$status = $this->status;
+		$tags = collect($res['tag']);
 
-		collect($res['tag'])
-		->filter(function($tag) {
-			// todo: finish hashtag + mention import
-			// return in_array($tag['type'], ['Emoji', 'Hashtag', 'Mention']);
-			return $tag && $tag['type'] == 'Emoji';
+		// Emoji
+		$tags->filter(function($tag) {
+			return $tag && $tag['type'] == 'Emoji' && isset($tag['id'], $tag['icon'], $tag['name']);
 		})
 		->map(function($tag) {
 			CustomEmojiService::import($tag['id'], $this->status->id);
 		});
+
+		// Hashtags
+		$tags->filter(function($tag) {
+			return $tag && $tag['type'] == 'Hashtag' && isset($tag['href'], $tag['name']);
+		})
+		->map(function($tag) use($status) {
+			$name = substr($tag['name'], 0, 1) == '#' ?
+				substr($tag['name'], 1) : $tag['name'];
+
+			$hashtag = Hashtag::firstOrCreate([
+				'slug' => str_slug($name)
+			], [
+				'name' => $name
+			]);
+
+			StatusHashtag::firstOrCreate([
+				'status_id' => $status->id,
+				'hashtag_id' => $hashtag->id,
+				'profile_id' => $status->profile_id,
+				'status_visibility' => $status->scope
+			]);
+		});
+
+		// Mentions
+		$tags->filter(function($tag) {
+			return $tag &&
+				$tag['type'] == 'Mention' &&
+				isset($tag['href']) &&
+				substr($tag['href'], 0, 8) === 'https://' &&
+				parse_url($tag['href'], PHP_URL_HOST) == config('pixelfed.domain.app');
+		})
+		->map(function($tag) use($status) {
+			$parts = explode('/', $status['href']);
+			if(!$parts) {
+				return;
+			}
+			$pid = AccountService::usernameToId(end($parts));
+			if(!$pid) {
+				return;
+			}
+			$mention = new Mention;
+			$mention->status_id = $status->id;
+			$mention->profile_id = $pid;
+			$mention->save();
+			MentionPipeline::dispatch($status, $mention);
+		});
 	}
 }