From bc29073ab1734439bfb196bbac9edb089c8f5df5 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sat, 24 Dec 2022 01:07:44 -0700 Subject: [PATCH 1/2] Fix CustomEmoji, properly handle shortcode updates and delete old copy in case the extension changes --- app/Models/CustomEmoji.php | 2 ++ app/Services/CustomEmojiService.php | 39 ++++++++++++++++++++++------- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/app/Models/CustomEmoji.php b/app/Models/CustomEmoji.php index 45e26121..1ff026a1 100644 --- a/app/Models/CustomEmoji.php +++ b/app/Models/CustomEmoji.php @@ -14,6 +14,8 @@ class CustomEmoji extends Model const SCAN_RE = "/(?<=[^[:alnum:]:]|\n|^):([a-zA-Z0-9_]{2,}):(?=[^[:alnum:]:]|$)/x"; const CACHE_KEY = "pf:custom_emoji:"; + protected $guarded = []; + public static function scan($text, $activitypub = false) { if(config('federation.custom_emoji.enabled') == false) { diff --git a/app/Services/CustomEmojiService.php b/app/Services/CustomEmojiService.php index 74957e28..a95c93a2 100644 --- a/app/Services/CustomEmojiService.php +++ b/app/Services/CustomEmojiService.php @@ -6,6 +6,8 @@ use App\Models\CustomEmoji; use App\Util\ActivityPub\Helpers; use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Storage; +use Illuminate\Http\Client\RequestException; class CustomEmojiService { @@ -33,7 +35,13 @@ class CustomEmojiService return; } - $res = Http::acceptJson()->get($url); + try { + $res = Http::acceptJson()->get($url); + } catch (RequestException $e) { + return; + } catch (\Exception $e) { + return; + } if($res->successful()) { $json = $res->json(); @@ -57,16 +65,23 @@ class CustomEmojiService return; } - $emoji = new CustomEmoji; - $emoji->shortcode = $json['name']; - $emoji->uri = $json['id']; - $emoji->domain = parse_url($json['id'], PHP_URL_HOST); - $emoji->image_remote_url = $json['icon']['url']; - $emoji->save(); + $emoji = CustomEmoji::firstOrCreate([ + 'shortcode' => $json['name'], + 'domain' => parse_url($json['id'], PHP_URL_HOST) + ], [ + 'uri' => $json['id'], + 'image_remote_url' => $json['icon']['url'] + ]); + + if($emoji->wasRecentlyCreated == false) { + if(Storage::exists('public/' . $emoji->media_path)) { + Storage::delete('public/' . $emoji->media_path); + } + } $ext = '.' . last(explode('/', $json['icon']['mediaType'])); $dest = storage_path('app/public/emoji/') . $emoji->id . $ext; - copy($emoji->image_remote_url, $dest); + copy($json['icon']['url'], $dest); $emoji->media_path = 'emoji/' . $emoji->id . $ext; $emoji->save(); @@ -84,7 +99,13 @@ class CustomEmojiService public static function headCheck($url) { - $res = Http::head($url); + try { + $res = Http::head($url); + } catch (RequestException $e) { + return false; + } catch (\Exception $e) { + return false; + } if(!$res->successful()) { return false; From e10985e6312b0618b6fc47cc54ff7d19aecb736d Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sat, 24 Dec 2022 01:08:06 -0700 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b526d5ad..8f63baeb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -65,6 +65,7 @@ - Update CommentPipeline, remove expensive reply count re-calculation query ([b457a446](https://github.com/pixelfed/pixelfed/commit/b457a446)) - Update FederationController, improve inbox/sharedInbox delete handling ([2180a2de](https://github.com/pixelfed/pixelfed/commit/2180a2de)) - Update HashtagController, improve trending hashtag endpoint ([4873c7dd](https://github.com/pixelfed/pixelfed/commit/4873c7dd)) +- Fix CustomEmoji, properly handle shortcode updates and delete old copy in case the extension changes ([bc29073a](https://github.com/pixelfed/pixelfed/commit/bc29073a)) - ([](https://github.com/pixelfed/pixelfed/commit/)) ## [v0.11.4 (2022-10-04)](https://github.com/pixelfed/pixelfed/compare/v0.11.3...v0.11.4)