From 8df77ff38059cf6fa01164078b4f30b03ce6192b Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Sat, 8 Dec 2018 11:41:56 -0100 Subject: [PATCH] adding route and content to the Notification Stream --- appinfo/routes.php | 1 + lib/Controller/LocalController.php | 25 +++++++++++++++++++ lib/Db/NotesRequest.php | 32 +++++++++++++++++++++++++ lib/Service/ActivityPub/NoteService.php | 12 ++++++++++ 4 files changed, 70 insertions(+) diff --git a/appinfo/routes.php b/appinfo/routes.php index 6a595a4e..506a89aa 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -65,6 +65,7 @@ return [ ['name' => 'SocialPub#displayPost', 'url' => '/@{username}/{postId}', 'verb' => 'GET'], ['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#streamFederated', 'url' => '/api/v1/stream/federated', 'verb' => 'GET'], ['name' => 'Local#streamDirect', 'url' => '/api/v1/stream/direct', 'verb' => 'GET'], diff --git a/lib/Controller/LocalController.php b/lib/Controller/LocalController.php index a99782d9..7ee27ebc 100644 --- a/lib/Controller/LocalController.php +++ b/lib/Controller/LocalController.php @@ -217,6 +217,31 @@ class LocalController extends Controller { } + + + /** + * @NoCSRFRequired + * @NoAdminRequired + * @NoSubAdminRequired + * + * @param int $since + * @param int $limit + * + * @return DataResponse + */ + public function streamNotifications($since = 0, int $limit = 5): DataResponse { + try { + $this->initViewer(true); + $posts = $this->noteService->getStreamNotifications($this->viewer, $since, $limit); + + return $this->success($posts); + } catch (Exception $e) { + return $this->fail($e); + } + } + + + /** * // TODO: Delete the NoCSRF check * diff --git a/lib/Db/NotesRequest.php b/lib/Db/NotesRequest.php index 9e687e25..74fa6faa 100644 --- a/lib/Db/NotesRequest.php +++ b/lib/Db/NotesRequest.php @@ -185,6 +185,38 @@ class NotesRequest extends NotesRequestBuilder { } + /** + * Should returns: + * - Public/Unlisted/Followers-only post where current $actor is tagged, + * - Events: + * - people liking or re-posting your posts + * - someone wants to follow you + * - someone is following you + * + * @param Person $actor + * @param int $since + * @param int $limit + * + * @return array + */ + public function getStreamNotifications(Person $actor, int $since = 0, int $limit = 5): array { + $qb = $this->getNotesSelectSql(); + + $this->limitPaginate($qb, $since, $limit); + $this->limitToRecipient($qb, $actor->getId(), false); + $this->leftJoinCacheActors($qb, 'attributed_to'); + + $notes = []; + $cursor = $qb->execute(); + while ($data = $cursor->fetch()) { + $notes[] = $this->parseNotesSelectSql($data); + } + $cursor->closeCursor(); + + return $notes; + } + + /** * Should returns: * * public message from actorId. diff --git a/lib/Service/ActivityPub/NoteService.php b/lib/Service/ActivityPub/NoteService.php index af0257f9..e721c24c 100644 --- a/lib/Service/ActivityPub/NoteService.php +++ b/lib/Service/ActivityPub/NoteService.php @@ -361,6 +361,18 @@ class NoteService implements ICoreService { } + /** + * @param Person $actor + * @param int $since + * @param int $limit + * + * @return Note[] + */ + public function getStreamNotifications(Person $actor, int $since = 0, int $limit = 5): array { + return $this->notesRequest->getStreamNotifications($actor, $since, $limit); + } + + /** * @param string $actorId * @param int $since