From fa274227409f06254f350c0d2f6ae50adb36742c Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Tue, 17 Jan 2023 08:22:37 -0100 Subject: [PATCH] get status api Signed-off-by: Maxence Lange --- appinfo/routes.php | 3 ++- lib/Controller/ApiController.php | 22 ++++++++++++++++++++++ lib/Db/StreamRequest.php | 23 ++++++++++++++++++++--- lib/Db/StreamRequestBuilder.php | 2 +- lib/Service/StreamService.php | 12 ++++++++++++ lib/Tools/Db/ExtendedQueryBuilder.php | 5 +++++ 6 files changed, 62 insertions(+), 5 deletions(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index a05daff8..787f0d16 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -79,11 +79,12 @@ return [ ['name' => 'Api#instance', 'url' => '/api/v1/instance/', 'verb' => 'GET'], ['name' => 'Api#customEmojis', 'url' => '/api/v1/custom_emojis', 'verb' => 'GET'], ['name' => 'Api#savedSearches', 'url' => '/api/saved_searches/list.json', 'verb' => 'GET'], + ['name' => 'Api#timelines', 'url' => '/api/v1/timelines/{timeline}/', 'verb' => 'GET'], ['name' => 'Api#favourites', 'url' => '/api/v1/favourites/', 'verb' => 'GET'], ['name' => 'Api#notifications', 'url' => '/api/v1/notifications', 'verb' => 'GET'], ['name' => 'Api#tag', 'url' => '/api/v1/timelines/tag/{hashtag}', 'verb' => 'GET'], ['name' => 'Api#statusNew', 'url' => '/api/v1/statuses', 'verb' => 'POST'], - ['name' => 'Api#timelines', 'url' => '/api/v1/timelines/{timeline}/', 'verb' => 'GET'], + ['name' => 'Api#statusGet', 'url' => '/api/v1/statuses/{nid}', 'verb' => 'GET'], ['name' => 'Api#accountStatuses', 'url' => '/api/v1/accounts/{account}/statuses', 'verb' => 'GET'], // Api for local front-end diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index a31894bd..71d0cde2 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -281,6 +281,28 @@ class ApiController extends Controller { } + /** + * @NoCSRFRequired + * @PublicPage + * + * @param int $nid + * + * @return DataResponse + */ + public function statusGet(int $nid): DataResponse { + try { + $this->initViewer(true); + + $item = $this->streamService->getStreamByNid($nid); + + return new DataResponse($item, Http::STATUS_OK); + } catch (Exception $e) { + return $this->error($e->getMessage()); + } + } + + + /** * @NoCSRFRequired * @PublicPage diff --git a/lib/Db/StreamRequest.php b/lib/Db/StreamRequest.php index 88488a19..9e0c18ff 100644 --- a/lib/Db/StreamRequest.php +++ b/lib/Db/StreamRequest.php @@ -227,6 +227,26 @@ class StreamRequest extends StreamRequestBuilder { } + /** + * @param string $id + * @param bool $asViewer + * @param int $format + * + * @return Stream + * @throws StreamNotFoundException + */ + public function getStreamByNid(int $nid): Stream { + $qb = $this->getStreamSelectSql(ACore::FORMAT_LOCAL); + $qb->limitToNid($nid); + $qb->linkToCacheActors('ca', 's.attributed_to_prim'); + + $qb->limitToViewer('sd', 'f', true, true); + $qb->leftJoinStreamAction('sa'); + + return $this->getStreamFromRequest($qb); + } + + /** * @param string $idPrim * @@ -446,8 +466,6 @@ class StreamRequest extends StreamRequestBuilder { } - - /** * Should returns: * - public message from actorId. @@ -482,7 +500,6 @@ class StreamRequest extends StreamRequestBuilder { } - /** * @param TimelineOptions $options * diff --git a/lib/Db/StreamRequestBuilder.php b/lib/Db/StreamRequestBuilder.php index 634d2db1..e2b421d1 100644 --- a/lib/Db/StreamRequestBuilder.php +++ b/lib/Db/StreamRequestBuilder.php @@ -172,7 +172,7 @@ class StreamRequestBuilder extends CoreRequestBuilder { try { $result = $qb->getRow([$this, 'parseStreamSelectSql']); } catch (RowNotFoundException $e) { - throw new StreamNotFoundException($e->getMessage()); + throw new StreamNotFoundException('stream not found'); } return $result; diff --git a/lib/Service/StreamService.php b/lib/Service/StreamService.php index 501c6e4a..0b72fc40 100644 --- a/lib/Service/StreamService.php +++ b/lib/Service/StreamService.php @@ -358,6 +358,18 @@ class StreamService { } + /** + * @param string $id + * @param bool $asViewer + * + * @return Stream + * @throws StreamNotFoundException + */ + public function getStreamByNid(int $nid): Stream { + return $this->streamRequest->getStreamByNid($nid); + } + + /** * @param string $id * @param int $since diff --git a/lib/Tools/Db/ExtendedQueryBuilder.php b/lib/Tools/Db/ExtendedQueryBuilder.php index 32f5d0a6..cda11f80 100644 --- a/lib/Tools/Db/ExtendedQueryBuilder.php +++ b/lib/Tools/Db/ExtendedQueryBuilder.php @@ -86,6 +86,11 @@ class ExtendedQueryBuilder extends QueryBuilder implements IExtendedQueryBuilder } + public function limitToNid(int $id): void { + $this->limitToDBFieldInt('nid', $id); + } + + /** * @param array $ids *