From c7d1da1f53a4fbde722a28ed0b98acf511409f22 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Fri, 4 Jan 2019 18:42:58 -0100 Subject: [PATCH] get stream for hashtag Signed-off-by: Maxence Lange --- appinfo/routes.php | 1 + lib/Controller/LocalController.php | 24 ++++++++++++++++++++ lib/Db/NotesRequest.php | 35 +++++++++++++++++++++++++++++- lib/Service/NoteService.php | 12 ++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) diff --git a/appinfo/routes.php b/appinfo/routes.php index dc309c09..c12fbd30 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -59,6 +59,7 @@ return [ ['name' => 'Local#streamHome', 'url' => '/api/v1/stream/home', 'verb' => 'GET'], ['name' => 'Local#streamNotifications', 'url' => '/api/v1/stream/notifications', 'verb' => 'GET'], ['name' => 'Local#streamTimeline', 'url' => '/api/v1/stream/timeline', 'verb' => 'GET'], + ['name' => 'Local#streamTag', 'url' => '/api/v1/stream/tag/{hashtag}/', 'verb' => 'GET'], ['name' => 'Local#streamFederated', 'url' => '/api/v1/stream/federated', 'verb' => 'GET'], ['name' => 'Local#streamDirect', 'url' => '/api/v1/stream/direct', 'verb' => 'GET'], ['name' => 'Local#streamAccount', 'url' => '/api/v1/account/{username}/stream', 'verb' => 'GET'], diff --git a/lib/Controller/LocalController.php b/lib/Controller/LocalController.php index 9b435490..215fee60 100644 --- a/lib/Controller/LocalController.php +++ b/lib/Controller/LocalController.php @@ -295,6 +295,30 @@ class LocalController extends Controller { } } + + /** + * Get timeline + * + * @NoCSRFRequired + * @NoAdminRequired + * + * @param int $since + * @param int $limit + * + * @return DataResponse + */ + public function streamTag(string $hashtag, int $since = 0, int $limit = 5): DataResponse { + try { + $posts = $this->noteService->getStreamLocalTag($hashtag, $since, $limit); + + return $this->success($posts); + } catch (Exception $e) { + return $this->fail($e); + } + } + + + /** * Get timeline * diff --git a/lib/Db/NotesRequest.php b/lib/Db/NotesRequest.php index af5bd63f..68634565 100644 --- a/lib/Db/NotesRequest.php +++ b/lib/Db/NotesRequest.php @@ -87,7 +87,7 @@ class NotesRequest extends NotesRequestBuilder { ) ->setValue('content', $qb->createNamedParameter($note->getContent())) ->setValue('summary', $qb->createNamedParameter($note->getSummary())) - ->setValue('hashtags', $qb->createNamedParameter(json_encode($note->getHashtags()))) + ->setValue('hashtags', $qb->createNamedParameter(json_encode($note->getHashtags()))) ->setValue('published', $qb->createNamedParameter($note->getPublished())) ->setValue( 'published_time', $qb->createNamedParameter($dTime, IQueryBuilder::PARAM_DATE) @@ -307,6 +307,39 @@ class NotesRequest extends NotesRequestBuilder { } + /** + * Should returns: + * - All public post related to a tag (not yet) + * - direct message related to a tag (not yet) + * - message to followers related to a tag (not yet) + * + * @param string $hashtag + * @param int $since + * @param int $limit + * + * @return array + */ + public function getStreamTag(string $hashtag, int $since = 0, int $limit = 5): array { + $qb = $this->getNotesSelectSql(); + +// // TODO: LIMIT TO NOTE RELATED TO THE VIEWER+PUBLIC + + $this->limitPaginate($qb, $since, $limit); + $qb->andWhere($this->exprValueWithinJsonFormat($qb, 'hashtags', '#' . $hashtag)); + + $this->leftJoinCacheActors($qb, 'attributed_to'); + + $notes = []; + $cursor = $qb->execute(); + while ($data = $cursor->fetch()) { + $notes[] = $this->parseNotesSelectSql($data); + } + $cursor->closeCursor(); + + return $notes; + } + + /** * @param string $id */ diff --git a/lib/Service/NoteService.php b/lib/Service/NoteService.php index af3860f5..5518874d 100644 --- a/lib/Service/NoteService.php +++ b/lib/Service/NoteService.php @@ -383,6 +383,18 @@ class NoteService { } + /** + * @param string $hashtag + * @param int $since + * @param int $limit + * + * @return Note[] + */ + public function getStreamLocalTag(string $hashtag, int $since = 0, int $limit = 5): array { + return $this->notesRequest->getStreamTag($hashtag, $since, $limit); + } + + /** * @param int $since * @param int $limit