diff --git a/app/Http/Controllers/Api/ApiV1Controller.php b/app/Http/Controllers/Api/ApiV1Controller.php index 17f3afcdd..3c0dccb4a 100644 --- a/app/Http/Controllers/Api/ApiV1Controller.php +++ b/app/Http/Controllers/Api/ApiV1Controller.php @@ -1589,14 +1589,14 @@ class ApiV1Controller extends Controller $minId = null; if($max) { - $res = NotificationService::getMax($pid, $max, $limit); + $res = NotificationService::getMaxMastodon($pid, $max, $limit); $ids = NotificationService::getRankedMaxId($pid, $max, $limit); if(!empty($ids)) { $maxId = max($ids); $minId = min($ids); } } else { - $res = NotificationService::getMin($pid, $min ?? $since, $limit); + $res = NotificationService::getMinMastodon($pid, $min ?? $since, $limit); $ids = NotificationService::getRankedMinId($pid, $min ?? $since, $limit); if(!empty($ids)) { $maxId = max($ids); @@ -2216,9 +2216,7 @@ class ApiV1Controller extends Controller Cache::forget('profile:embed:' . $status->profile_id); Cache::forget($limitKey); - $resource = new Fractal\Resource\Item($status, new StatusTransformer()); - $res = $this->fractal->createData($resource)->toArray(); - + $res = StatusService::getMastodon($status->id, false); return $this->json($res); } @@ -2318,16 +2316,16 @@ class ApiV1Controller extends Controller ->first(); if(!$reblog) { - $resource = new Fractal\Resource\Item($status, new StatusTransformer()); - $res = $this->fractal->createData($resource)->toArray(); - return response()->json($res); + $res = StatusService::getMastodon($status->id); + $res['reblogged'] = false; + return $this->json($res); } UndoSharePipeline::dispatch($reblog); ReblogService::del($user->profile_id, $status->id); $res = StatusService::getMastodon($status->id); - $res['reblogged'] = true; + $res['reblogged'] = false; return $this->json($res); } @@ -2454,8 +2452,7 @@ class ApiV1Controller extends Controller 'status_id' => $status->id, 'profile_id' => $request->user()->profile_id ]); - $resource = new Fractal\Resource\Item($status, new StatusTransformer()); - $res = $this->fractal->createData($resource)->toArray(); + $res = StatusService::getMastodon($status->id); return $this->json($res); } @@ -2475,17 +2472,11 @@ class ApiV1Controller extends Controller ->whereScope('public') ->findOrFail($id); - Bookmark::firstOrCreate([ - 'status_id' => $status->id, - 'profile_id' => $request->user()->profile_id - ]); $bookmark = Bookmark::whereStatusId($status->id) ->whereProfileId($request->user()->profile_id) ->firstOrFail(); $bookmark->delete(); - - $resource = new Fractal\Resource\Item($status, new StatusTransformer()); - $res = $this->fractal->createData($resource)->toArray(); + $res = StatusService::getMastodon($status->id); return $this->json($res); } diff --git a/app/Services/NotificationService.php b/app/Services/NotificationService.php index e84ea9903..f8d9a62e8 100644 --- a/app/Services/NotificationService.php +++ b/app/Services/NotificationService.php @@ -16,6 +16,15 @@ use League\Fractal\Pagination\IlluminatePaginatorAdapter; class NotificationService { const CACHE_KEY = 'pf:services:notifications:ids:'; + const MASTODON_TYPES = [ + 'follow', + 'follow_request', + 'mention', + 'reblog', + 'favourite', + 'poll', + 'status' + ]; public static function get($id, $start = 0, $stop = 400) { @@ -85,6 +94,63 @@ class NotificationService { return $res->toArray(); } + + public static function getMaxMastodon($id = false, $start = 0, $limit = 10) + { + $ids = self::getRankedMaxId($id, $start, $limit); + + if(empty($ids)) { + return []; + } + + $res = collect([]); + foreach($ids as $id) { + $n = self::getNotification($id); + if($n != null && in_array($n['type'], self::MASTODON_TYPES)) { + $n['account'] = AccountService::getMastodon($n['account']['id']); + + if(isset($n['relationship'])) { + unset($n['relationship']); + } + + if(isset($n['status'])) { + $n['status'] = StatusService::getMastodon($n['status']['id'], false); + } + + $res->push($n); + } + } + return $res->toArray(); + } + + public static function getMinMastodon($id = false, $start = 0, $limit = 10) + { + $ids = self::getRankedMinId($id, $start, $limit); + + if(empty($ids)) { + return []; + } + + $res = collect([]); + foreach($ids as $id) { + $n = self::getNotification($id); + if($n != null && in_array($n['type'], self::MASTODON_TYPES)) { + $n['account'] = AccountService::getMastodon($n['account']['id']); + + if(isset($n['relationship'])) { + unset($n['relationship']); + } + + if(isset($n['status'])) { + $n['status'] = StatusService::getMastodon($n['status']['id'], false); + } + + $res->push($n); + } + } + return $res->toArray(); + } + public static function getRankedMaxId($id = false, $start = null, $limit = 10) { if(!$start || !$id) {