From dc17c9fc2715d14d642f9856d5f766bb0bdc00d0 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Wed, 19 Jan 2022 00:46:30 -0700 Subject: [PATCH] Improve emoji import --- .../StatusPipeline/StatusTagsPipeline.php | 56 +++++++++++++++++++ app/Models/CustomEmoji.php | 4 ++ app/Services/CustomEmojiService.php | 18 +++++- app/Util/ActivityPub/Helpers.php | 18 ++---- ...01_19_025041_create_custom_emoji_table.php | 2 +- 5 files changed, 81 insertions(+), 17 deletions(-) create mode 100644 app/Jobs/StatusPipeline/StatusTagsPipeline.php diff --git a/app/Jobs/StatusPipeline/StatusTagsPipeline.php b/app/Jobs/StatusPipeline/StatusTagsPipeline.php new file mode 100644 index 000000000..b09623b38 --- /dev/null +++ b/app/Jobs/StatusPipeline/StatusTagsPipeline.php @@ -0,0 +1,56 @@ +activity = $activity; + $this->status = $status; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + $res = $this->activity; + + collect($res['tag']) + ->filter(function($tag) { + // todo: finish hashtag + mention import + // return in_array($tag['type'], ['Emoji', 'Hashtag', 'Mention']); + return $tag && $tag['type'] == 'Emoji'; + }) + ->values() + ->map(function($tag) { + CustomEmojiService::import($tag['id']); + }); + + // sleep(15); + + StatusService::del($this->status->id); + } +} diff --git a/app/Models/CustomEmoji.php b/app/Models/CustomEmoji.php index 0903d5375..fdd8ac196 100644 --- a/app/Models/CustomEmoji.php +++ b/app/Models/CustomEmoji.php @@ -16,6 +16,10 @@ class CustomEmoji extends Model public static function scan($text) { + if(config('federation.custom_emoji.enabled') == false) { + return []; + } + return Str::of($text) ->matchAll(self::SCAN_RE) ->map(function($match) { diff --git a/app/Services/CustomEmojiService.php b/app/Services/CustomEmojiService.php index 3311e5117..75b1fb0ce 100644 --- a/app/Services/CustomEmojiService.php +++ b/app/Services/CustomEmojiService.php @@ -2,26 +2,35 @@ namespace App\Services; +use App\Models\CustomEmoji; use App\Util\ActivityPub\Helpers; use Illuminate\Support\Facades\Http; -use App\Models\CustomEmoji; +use Illuminate\Support\Facades\Cache; class CustomEmojiService { public static function get($shortcode) { + if(config('federation.custom_emoji.enabled') == false) { + return; + } + return CustomEmoji::whereShortcode($shortcode)->first(); } public static function import($url) { + if(config('federation.custom_emoji.enabled') == false) { + return; + } + if(Helpers::validateUrl($url) == false) { return; } $emoji = CustomEmoji::whereUri($url)->first(); if($emoji) { - return $emoji; + return; } $res = Http::acceptJson()->get($url); @@ -47,6 +56,7 @@ class CustomEmojiService if(!self::headCheck($json['icon']['url'])) { return; } + $emoji = new CustomEmoji; $emoji->shortcode = $json['name']; $emoji->uri = $json['id']; @@ -60,7 +70,9 @@ class CustomEmojiService $emoji->media_path = 'emoji/' . $emoji->id . $ext; $emoji->save(); - return $emoji; + $name = str_replace(':', '', $json['name']); + Cache::forget('pf:custom_emoji:' . $name); + return; } else { return; } diff --git a/app/Util/ActivityPub/Helpers.php b/app/Util/ActivityPub/Helpers.php index 1413f5106..a18a57ff0 100644 --- a/app/Util/ActivityPub/Helpers.php +++ b/app/Util/ActivityPub/Helpers.php @@ -23,6 +23,7 @@ use App\Jobs\RemoteFollowPipeline\RemoteFollowImportRecent; use App\Jobs\ImageOptimizePipeline\{ImageOptimize,ImageThumbnail}; use App\Jobs\StatusPipeline\NewStatusPipeline; use App\Jobs\StatusPipeline\StatusReplyPipeline; +use App\Jobs\StatusPipeline\StatusTagsPipeline; use App\Util\ActivityPub\HttpSignature; use Illuminate\Support\Str; use App\Services\ActivityPubFetchService; @@ -381,19 +382,6 @@ class Helpers { $scope, $id ) { - if(isset($res['tag']) && is_array($res['tag']) && !empty($res['tag'])) { - collect($res['tag']) - ->filter(function($tag) { - // todo: finish hashtag + mention import - // return in_array($tag['type'], ['Emoji', 'Hashtag', 'Mention']); - return in_array($tag['type'], ['Emoji']); - }) - ->each(function($tag) { - if(isset($tag['id'])) { - CustomEmojiService::import($tag['id']); - } - }); - } if($res['type'] === 'Question') { $status = self::storePoll( @@ -430,6 +418,10 @@ class Helpers { } else { StatusReplyPipeline::dispatch($status); } + + if(isset($res['tag']) && is_array($res['tag']) && !empty($res['tag'])) { + StatusTagsPipeline::dispatch($res, $status); + } return $status; }); }); diff --git a/database/migrations/2022_01_19_025041_create_custom_emoji_table.php b/database/migrations/2022_01_19_025041_create_custom_emoji_table.php index 8c05d9bbb..40ff9b3e9 100644 --- a/database/migrations/2022_01_19_025041_create_custom_emoji_table.php +++ b/database/migrations/2022_01_19_025041_create_custom_emoji_table.php @@ -15,7 +15,7 @@ class CreateCustomEmojiTable extends Migration { Schema::create('custom_emoji', function (Blueprint $table) { $table->id(); - $table->string('shortcode')->unique()->index(); + $table->string('shortcode')->index(); $table->string('media_path')->nullable(); $table->string('domain')->nullable()->index(); $table->boolean('disabled')->default(false)->index();