From b299da9311926baeffaf958ac36deaf0d111c1c1 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Mon, 1 Feb 2021 00:47:54 -0700 Subject: [PATCH 1/4] Update AccountService, cache object and observe changes --- app/Observers/AvatarObserver.php | 4 +- app/Observers/ProfileObserver.php | 64 ++++++++++++++++++++++++++++ app/Providers/AppServiceProvider.php | 3 ++ app/Services/AccountService.php | 29 ++++++++----- 4 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 app/Observers/ProfileObserver.php diff --git a/app/Observers/AvatarObserver.php b/app/Observers/AvatarObserver.php index 3d62845b4..31ac45ebe 100644 --- a/app/Observers/AvatarObserver.php +++ b/app/Observers/AvatarObserver.php @@ -5,6 +5,7 @@ namespace App\Observers; use App\Avatar; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; +use App\Services\AccountService; class AvatarObserver { @@ -27,7 +28,7 @@ class AvatarObserver */ public function updated(Avatar $avatar) { - // + AccountService::del($avatar->profile_id); } /** @@ -64,6 +65,7 @@ class AvatarObserver $disk->delete($avatar->media_path); } } + AccountService::del($avatar->profile_id); } /** diff --git a/app/Observers/ProfileObserver.php b/app/Observers/ProfileObserver.php new file mode 100644 index 000000000..8cd5caf96 --- /dev/null +++ b/app/Observers/ProfileObserver.php @@ -0,0 +1,64 @@ +id); + } + + /** + * Handle the Profile "deleted" event. + * + * @param \App\Profile $profile + * @return void + */ + public function deleted(Profile $profile) + { + AccountService::del($profile->id); + } + + /** + * Handle the Profile "restored" event. + * + * @param \App\Profile $profile + * @return void + */ + public function restored(Profile $profile) + { + // + } + + /** + * Handle the Profile "force deleted" event. + * + * @param \App\Profile $profile + * @return void + */ + public function forceDeleted(Profile $profile) + { + // + } +} diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index df72e6448..7364745a8 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -6,6 +6,7 @@ use App\Observers\{ AvatarObserver, NotificationObserver, ModLogObserver, + ProfileObserver, StatusHashtagObserver, UserObserver, UserFilterObserver, @@ -14,6 +15,7 @@ use App\{ Avatar, Notification, ModLog, + Profile, StatusHashtag, User, UserFilter @@ -41,6 +43,7 @@ class AppServiceProvider extends ServiceProvider Avatar::observe(AvatarObserver::class); Notification::observe(NotificationObserver::class); ModLog::observe(ModLogObserver::class); + Profile::observe(ProfileObserver::class); StatusHashtag::observe(StatusHashtagObserver::class); User::observe(UserObserver::class); UserFilter::observe(UserFilterObserver::class); diff --git a/app/Services/AccountService.php b/app/Services/AccountService.php index 2ef8a6215..38e29169d 100644 --- a/app/Services/AccountService.php +++ b/app/Services/AccountService.php @@ -14,16 +14,25 @@ class AccountService { public static function get($id) { - // $key = self::CACHE_KEY . ':' . $id; - // $ttl = now()->addSeconds(10); - // return Cache::remember($key, $ttl, function() use($id) { - // }); - - $fractal = new Fractal\Manager(); - $fractal->setSerializer(new ArraySerializer()); - $profile = Profile::whereNull('status')->findOrFail($id); - $resource = new Fractal\Resource\Item($profile, new AccountTransformer()); - return $fractal->createData($resource)->toArray(); + if($id > PHP_INT_MAX || $id < 1) { + return []; + } + + $key = self::CACHE_KEY . $id; + $ttl = now()->addMinutes(15); + + return Cache::remember($key, $ttl, function() use($id) { + $fractal = new Fractal\Manager(); + $fractal->setSerializer(new ArraySerializer()); + $profile = Profile::whereNull('status')->findOrFail($id); + $resource = new Fractal\Resource\Item($profile, new AccountTransformer()); + return $fractal->createData($resource)->toArray(); + }); + } + + public static function del($id) + { + return Cache::forget(self::CACHE_KEY . $id); } } \ No newline at end of file From aad07e2c83c1656c08d9175b10309ce2dc125bf7 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Mon, 1 Feb 2021 00:48:36 -0700 Subject: [PATCH 2/4] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3635338ab..74f657043 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ - Updated DiscoverComponent, add blurhash and like/comment counts. ([a8ebdd2e](https://github.com/pixelfed/pixelfed/commit/a8ebdd2e)) - Updated DiscoverComponent, add spinner loaders and remove deprecated sections. ([34869247](https://github.com/pixelfed/pixelfed/commit/34869247)) - Updated AccountController, add mutes and blocks endpoint to pixelfed api. ([1fb7e2b2](https://github.com/pixelfed/pixelfed/commit/1fb7e2b2)) +- Updated AccountService, cache object and observe changes. ([b299da93](https://github.com/pixelfed/pixelfed/commit/b299da93)) - ([](https://github.com/pixelfed/pixelfed/commit/)) ## [v0.10.10 (2021-01-28)](https://github.com/pixelfed/pixelfed/compare/v0.10.9...v0.10.10) From 2d11317cebc36eda469167dc50befa6467e1ee6f Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Wed, 3 Feb 2021 20:55:49 -0700 Subject: [PATCH 3/4] Update webfinger util, fail on invalid webfinger url --- app/Http/Controllers/FederationController.php | 2 +- app/Util/Lexer/Nickname.php | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/FederationController.php b/app/Http/Controllers/FederationController.php index a2f5d5832..f6d3df31f 100644 --- a/app/Http/Controllers/FederationController.php +++ b/app/Http/Controllers/FederationController.php @@ -63,7 +63,7 @@ class FederationController extends Controller } $webfinger = (new Webfinger($profile))->generate(); - return response()->json($webfinger, 200, [], JSON_PRETTY_PRINT) + return response()->json($webfinger, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES) ->header('Access-Control-Allow-Origin','*'); } diff --git a/app/Util/Lexer/Nickname.php b/app/Util/Lexer/Nickname.php index 00a9b23d2..a8a1d0192 100644 --- a/app/Util/Lexer/Nickname.php +++ b/app/Util/Lexer/Nickname.php @@ -2,22 +2,35 @@ namespace App\Util\Lexer; +use Illuminate\Support\Str; + class Nickname { public static function normalizeProfileUrl($url) { - if (starts_with($url, 'acct:')) { + if(!Str::of($url)->contains('@')) { + return; + } + + if(Str::startsWith($url, 'acct:')) { $url = str_replace('acct:', '', $url); } - if(starts_with($url, '@')) { + if(Str::startsWith($url, '@')) { $url = substr($url, 1); + + if(!Str::of($url)->contains('@')) { + return; + } } $parts = explode('@', $url); $username = $parts[0]; $domain = $parts[1]; - return ['domain' => $domain, 'username' => $username]; + return [ + 'domain' => $domain, + 'username' => $username + ]; } } From 0cef5aec26b26db3673dc1e55aeea00ae5616625 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Wed, 3 Feb 2021 20:57:31 -0700 Subject: [PATCH 4/4] Update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 74f657043..1c8aa74bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ - Updated DiscoverComponent, add spinner loaders and remove deprecated sections. ([34869247](https://github.com/pixelfed/pixelfed/commit/34869247)) - Updated AccountController, add mutes and blocks endpoint to pixelfed api. ([1fb7e2b2](https://github.com/pixelfed/pixelfed/commit/1fb7e2b2)) - Updated AccountService, cache object and observe changes. ([b299da93](https://github.com/pixelfed/pixelfed/commit/b299da93)) +- Updated webfinger util, fail on invalid webfinger url. Fixes ([#2613](https://github.com/pixelfed/pixelfed/issues/2613)) ([2d11317c](https://github.com/pixelfed/pixelfed/commit/2d11317c)) - ([](https://github.com/pixelfed/pixelfed/commit/)) ## [v0.10.10 (2021-01-28)](https://github.com/pixelfed/pixelfed/compare/v0.10.9...v0.10.10)