From 85e4be8172612de344ec73ddb0e6be2a28493e3c Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Mon, 3 Jan 2022 00:53:15 -0700 Subject: [PATCH 1/7] Update AccountService, add getMastodon method for mastoapi compatibility --- app/Services/AccountService.php | 29 +++++++++++++++++++ app/Transformer/Api/AccountTransformer.php | 4 +-- .../Api/Mastodon/v1/AccountTransformer.php | 4 +-- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/app/Services/AccountService.php b/app/Services/AccountService.php index 2c3f97a7b..0410f8f64 100644 --- a/app/Services/AccountService.php +++ b/app/Services/AccountService.php @@ -33,6 +33,35 @@ class AccountService }); } + public static function getMastodon($id, $softFail = false) + { + $account = self::get($id, $softFail); + if(!$account) { + return null; + } + + unset( + $account['header_bg'], + $account['is_admin'], + $account['last_fetched_at'], + $account['local'], + $account['location'], + $account['note_text'], + $account['pronouns'], + $account['website'] + ); + + $account['avatar_static'] = $account['avatar']; + $account['bot'] = false; + $account['emojis'] = []; + $account['fields'] = []; + $account['header'] = url('/storage/headers/missing.png'); + $account['header_static'] = url('/storage/headers/missing.png'); + $account['last_status_at'] = null; + + return $account; + } + public static function del($id) { return Cache::forget(self::CACHE_KEY . $id); diff --git a/app/Transformer/Api/AccountTransformer.php b/app/Transformer/Api/AccountTransformer.php index 8beb5b668..6e6d9ea16 100644 --- a/app/Transformer/Api/AccountTransformer.php +++ b/app/Transformer/Api/AccountTransformer.php @@ -25,8 +25,8 @@ class AccountTransformer extends Fractal\TransformerAbstract 'acct' => $acct, 'display_name' => $profile->name, 'locked' => (bool) $profile->is_private, - 'followers_count' => $profile->followerCount(), - 'following_count' => $profile->followingCount(), + 'followers_count' => (int) $profile->followerCount(), + 'following_count' => (int) $profile->followingCount(), 'statuses_count' => (int) $profile->statusCount(), 'note' => $profile->bio ?? '', 'note_text' => strip_tags($profile->bio), diff --git a/app/Transformer/Api/Mastodon/v1/AccountTransformer.php b/app/Transformer/Api/Mastodon/v1/AccountTransformer.php index 25f482303..5f1e58214 100644 --- a/app/Transformer/Api/Mastodon/v1/AccountTransformer.php +++ b/app/Transformer/Api/Mastodon/v1/AccountTransformer.php @@ -24,8 +24,8 @@ class AccountTransformer extends Fractal\TransformerAbstract 'url' => $profile->url(), 'avatar' => $profile->avatarUrl(), 'avatar_static' => $profile->avatarUrl(), - 'header' => '', - 'header_static' => '', + 'header' => url('/storage/headers/missing.png'), + 'header_static' => url('/storage/headers/missing.png'), 'followers_count' => (int) $profile->followerCount(), 'following_count' => (int) $profile->followingCount(), 'statuses_count' => (int) $profile->statusCount(), From c32fde22e7b3e64814bf7c9afe4ee19bdfed884a Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Mon, 3 Jan 2022 00:55:13 -0700 Subject: [PATCH 2/7] Add default header --- storage/app/public/.gitignore | 3 ++- storage/app/public/headers/.gitignore | 3 +++ storage/app/public/headers/missing.png | Bin 0 -> 81 bytes 3 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 storage/app/public/headers/.gitignore create mode 100644 storage/app/public/headers/missing.png diff --git a/storage/app/public/.gitignore b/storage/app/public/.gitignore index 0e4505c04..e368b9e0e 100755 --- a/storage/app/public/.gitignore +++ b/storage/app/public/.gitignore @@ -2,4 +2,5 @@ !.gitignore !no-preview.png !m/ -!textimg/ \ No newline at end of file +!textimg/ +!headers/ diff --git a/storage/app/public/headers/.gitignore b/storage/app/public/headers/.gitignore new file mode 100644 index 000000000..ba3444e98 --- /dev/null +++ b/storage/app/public/headers/.gitignore @@ -0,0 +1,3 @@ +* +!.gitignore +!missing.png diff --git a/storage/app/public/headers/missing.png b/storage/app/public/headers/missing.png new file mode 100644 index 0000000000000000000000000000000000000000..26b59e75a08de1c5d3d7b0705e9fc19d317dc4cb GIT binary patch literal 81 zcmeAS@N?(olHy`uVBq!ia0vp^j35jm7|ip2ssJgbk|4ie28Oc9XDxs{E>9Q75RU7~ Y2_P8;#z%^KjDai$Pgg&ebxsLQ04O{U&j0`b literal 0 HcmV?d00001 From 85fc9dd0a8b62689f1b9b502c64d7d5ccba517da Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Mon, 3 Jan 2022 00:59:39 -0700 Subject: [PATCH 3/7] Update PublicApiController, fix accountStatuses pagination operator --- app/Http/Controllers/PublicApiController.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/Http/Controllers/PublicApiController.php b/app/Http/Controllers/PublicApiController.php index c1e71b09f..b678694b4 100644 --- a/app/Http/Controllers/PublicApiController.php +++ b/app/Http/Controllers/PublicApiController.php @@ -738,6 +738,10 @@ class PublicApiController extends Controller $min_id = $request->min_id; $scope = ['photo', 'photo:album', 'video', 'video:album']; + if(!$min_id && !$max_id) { + $min_id = 1; + } + if($profile['locked']) { if(!$user) { return response()->json([]); From 454b4e21dd509c42b84f2e0577307f37d91690b3 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Mon, 3 Jan 2022 02:31:03 -0700 Subject: [PATCH 4/7] Update Services, fix mastoapi compat --- app/Services/MediaService.php | 52 ++++++++++++++++++++++++---------- app/Services/StatusService.php | 3 ++ 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/app/Services/MediaService.php b/app/Services/MediaService.php index 42f188eba..6960af5f1 100644 --- a/app/Services/MediaService.php +++ b/app/Services/MediaService.php @@ -18,26 +18,48 @@ class MediaService public static function get($statusId) { - $status = Status::find($statusId); - if(!$status) { - return []; - } - $ttl = $status->created_at->lt(now()->subMinutes(30)) ? 129600 : 30; - return Cache::remember(self::CACHE_KEY.$statusId, $ttl, function() use($status) { - if(!$status) { + return Cache::remember(self::CACHE_KEY.$statusId, 86400, function() use($statusId) { + $media = Media::whereStatusId($statusId)->orderBy('order')->get(); + if(!$media) { return []; } - if(in_array($status->type, ['group:post', 'photo', 'video', 'video:album', 'photo:album', 'loop', 'photo:video:album'])) { - $media = Media::whereStatusId($status->id)->orderBy('order')->get(); - $fractal = new Fractal\Manager(); - $fractal->setSerializer(new ArraySerializer()); - $resource = new Fractal\Resource\Collection($media, new MediaTransformer()); - return $fractal->createData($resource)->toArray(); - } - return []; + $fractal = new Fractal\Manager(); + $fractal->setSerializer(new ArraySerializer()); + $resource = new Fractal\Resource\Collection($media, new MediaTransformer()); + return $fractal->createData($resource)->toArray(); }); } + public static function getMastodon($id) + { + $media = self::get($id); + if(!$media) { + return []; + } + $medias = collect($media) + ->map(function($media) { + $mime = $media['mime'] ? explode('/', $media['mime']) : false; + unset( + $media['optimized_url'], + $media['license'], + $media['is_nsfw'], + $media['orientation'], + $media['filter_name'], + $media['filter_class'], + $media['mime'] + ); + + $media['type'] = $mime ? strtolower($mime[0]) : 'unknown'; + return $media; + }) + ->filter(function($m) { + return $m && isset($m['url']); + }) + ->values(); + + return $medias->toArray(); + } + public static function del($statusId) { return Cache::forget(self::CACHE_KEY . $statusId); diff --git a/app/Services/StatusService.php b/app/Services/StatusService.php index d272e478a..3532c8733 100644 --- a/app/Services/StatusService.php +++ b/app/Services/StatusService.php @@ -71,6 +71,7 @@ class StatusService $status['account']['note_text'], $status['account']['pronouns'], $status['account']['website'], + $status['media_attachments'], ); $status['account']['avatar_static'] = $status['account']['avatar']; $status['account']['bot'] = false; @@ -80,6 +81,8 @@ class StatusService $status['account']['header_static'] = url('/storage/headers/missing.png'); $status['account']['last_status_at'] = null; + $status['media_attachments'] = array_values(MediaService::getMastodon($status['id'])); + return $status; } From ddf41dc347a5e8083df8066633c18c2ac4a8a270 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Wed, 5 Jan 2022 18:17:05 -0700 Subject: [PATCH 5/7] Update StoryController, add postgres bug fix --- app/Http/Controllers/StoryController.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/Http/Controllers/StoryController.php b/app/Http/Controllers/StoryController.php index 1c8890d87..216fed8b4 100644 --- a/app/Http/Controllers/StoryController.php +++ b/app/Http/Controllers/StoryController.php @@ -46,6 +46,8 @@ class StoryController extends StoryComposeController $r = new \StdClass; $r->id = $s->id; $r->profile_id = $s->profile_id; + $r->type = $s->type; + $r->path = $s->path; return $r; }) ->unique('profile_id'); From fcabc9be02ebb9789747ea0f110d3427bf41176d Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Wed, 5 Jan 2022 18:30:58 -0700 Subject: [PATCH 6/7] Fix Direct Message conversations endpoint on postgres instances --- app/Http/Controllers/Api/ApiV1Controller.php | 33 ++++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/Api/ApiV1Controller.php b/app/Http/Controllers/Api/ApiV1Controller.php index c4adc5d33..b4cc51e93 100644 --- a/app/Http/Controllers/Api/ApiV1Controller.php +++ b/app/Http/Controllers/Api/ApiV1Controller.php @@ -1701,16 +1701,29 @@ class ApiV1Controller extends Controller $scope = $request->input('scope', 'inbox'); $pid = $request->user()->profile_id; - $dms = DirectMessage::when($scope === 'inbox', function($q, $scope) use($pid) { - return $q->whereIsHidden(false)->whereToId($pid)->orWhere('from_id', $pid)->groupBy('to_id'); - }) - ->when($scope === 'sent', function($q, $scope) use($pid) { - return $q->whereFromId($pid)->groupBy('to_id'); - }) - ->when($scope === 'requests', function($q, $scope) use($pid) { - return $q->whereToId($pid)->whereIsHidden(true); - }) - ->latest() + if(config('database.default') == 'pgsql') { + $dms = DirectMessage::when($scope === 'inbox', function($q, $scope) use($pid) { + return $q->whereIsHidden(false)->whereToId($pid)->orWhere('from_id', $pid); + }) + ->when($scope === 'sent', function($q, $scope) use($pid) { + return $q->whereFromId($pid); + }) + ->when($scope === 'requests', function($q, $scope) use($pid) { + return $q->whereToId($pid)->whereIsHidden(true); + }); + } else { + $dms = DirectMessage::when($scope === 'inbox', function($q, $scope) use($pid) { + return $q->whereIsHidden(false)->whereToId($pid)->orWhere('from_id', $pid)->groupBy('to_id'); + }) + ->when($scope === 'sent', function($q, $scope) use($pid) { + return $q->whereFromId($pid)->groupBy('to_id'); + }) + ->when($scope === 'requests', function($q, $scope) use($pid) { + return $q->whereToId($pid)->whereIsHidden(true); + }); + } + + $dms = $dms->latest() ->simplePaginate($limit) ->map(function($dm) use($pid) { $from = $pid == $dm->to_id ? $dm->from_id : $dm->to_id; From fac8f2c66076f85a44525d19da96a70b85861c51 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Wed, 5 Jan 2022 18:32:57 -0700 Subject: [PATCH 7/7] Update changelog --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2cf4bbe21..d8dbec9fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,10 @@ ### Configuration - Enable network timeline by default ([b95aec12](https://github.com/pixelfed/pixelfed/commit/b95aec12)) +### Postgres Compatibility +- Fix Story recent endpoint on postgres instances ([ddf41dc3](https://github.com/pixelfed/pixelfed/commit/ddf41dc3)) +- Fix Direct Message conversations endpoint on postgres instances ([fcabc9be](https://github.com/pixelfed/pixelfed/commit/fcabc9be)) + ### Added - Manual email verification requests. ([bc659387](https://github.com/pixelfed/pixelfed/commit/bc659387)) - Added StatusMentionService, fixes #3026. ([e5387d67](https://github.com/pixelfed/pixelfed/commit/e5387d67)) @@ -80,6 +84,7 @@ - Updated AccountService, fix json casting. ([e5f8f344](https://github.com/pixelfed/pixelfed/commit/e5f8f344)) - Updated ApiV1Controller, fix illegal operator bug by setting default min_id. ([415826f2](https://github.com/pixelfed/pixelfed/commit/415826f2)) - Updated StatusService, add getMastodon method for mastoapi compatibility. ([36a129fe](https://github.com/pixelfed/pixelfed/commit/36a129fe)) +- Updated PublicApiController, fix accountStatuses pagination operator. ([85fc9dd0](https://github.com/pixelfed/pixelfed/commit/85fc9dd0)) - ([](https://github.com/pixelfed/pixelfed/commit/)) ## [v0.11.1 (2021-09-07)](https://github.com/pixelfed/pixelfed/compare/v0.11.0...v0.11.1)