From ac6ba79a3652d4f47ea3a49aafb575ba5c806a2b Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Thu, 28 Sep 2023 19:23:16 +0200 Subject: [PATCH] [mastodon-client] GET /notifications/:id --- .../api/mastodon/converters/notification.ts | 4 +-- .../api/mastodon/endpoints/notifications.ts | 32 +++++++++---------- .../api/mastodon/helpers/notification.ts | 4 +++ 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/converters/notification.ts b/packages/backend/src/server/api/mastodon/converters/notification.ts index 458c694d7..98e7eba52 100644 --- a/packages/backend/src/server/api/mastodon/converters/notification.ts +++ b/packages/backend/src/server/api/mastodon/converters/notification.ts @@ -12,8 +12,8 @@ import { getNote } from "@/server/api/common/getters.js"; type NotificationType = typeof notificationTypes[number]; export class NotificationConverter { - public static async encode(notification: Notification, localUser: ILocalUser, cache: AccountCache = UserHelpers.getFreshAccountCache()): Promise { - if (notification.notifieeId !== localUser.id) return null; + public static async encode(notification: Notification, localUser: ILocalUser, cache: AccountCache = UserHelpers.getFreshAccountCache()): Promise { + if (notification.notifieeId !== localUser.id) throw new Error('User is not recipient of notification'); //TODO: Test this (poll ended etc) const account = notification.notifierId diff --git a/packages/backend/src/server/api/mastodon/endpoints/notifications.ts b/packages/backend/src/server/api/mastodon/endpoints/notifications.ts index 9a03bcfa5..ad0851dc8 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/notifications.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/notifications.ts @@ -15,7 +15,6 @@ function toLimitToInt(q: any) { export function apiNotificationsMastodon(router: Router): void { router.get("/v1/notifications", async (ctx) => { - const body: any = ctx.request.body; try { const auth = await authenticate(ctx.headers.authorization, null); const user = auth[0] ?? null; @@ -39,24 +38,23 @@ export function apiNotificationsMastodon(router: Router): void { } }); - router.get("/v1/notification/:id", async (ctx) => { - const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`; - const accessTokens = ctx.request.headers.authorization; - const client = getClient(BASE_URL, accessTokens); - const body: any = ctx.request.body; + router.get("/v1/notifications/:id", async (ctx) => { try { - const dataRaw = await client.getNotification( - convertId(ctx.params.id, IdType.IceshrimpId), - ); - const data = convertNotification(dataRaw.data); - ctx.body = data; - if ( - data.type !== "follow" && - data.type !== "follow_request" && - data.type === "reaction" - ) { - data.type = "favourite"; + const auth = await authenticate(ctx.headers.authorization, null); + const user = auth[0] ?? null; + + if (!user) { + ctx.status = 401; + return; } + + const notification = await NotificationHelpers.getNotification(convertId(ctx.params.id, IdType.IceshrimpId), user); + if (notification === null) { + ctx.status = 404; + return; + } + + ctx.body = convertNotification(await NotificationConverter.encode(notification, user)); } catch (e: any) { console.error(e); ctx.status = 401; diff --git a/packages/backend/src/server/api/mastodon/helpers/notification.ts b/packages/backend/src/server/api/mastodon/helpers/notification.ts index f1766eef3..d72652a0a 100644 --- a/packages/backend/src/server/api/mastodon/helpers/notification.ts +++ b/packages/backend/src/server/api/mastodon/helpers/notification.ts @@ -33,6 +33,10 @@ export class NotificationHelpers { return PaginationHelpers.execQuery(query, limit, minId !== undefined); } + public static async getNotification(id: string, user: ILocalUser): Promise { + return Notifications.findOneBy({id: id, notifieeId: user.id}); + } + private static decodeTypes(types: string[]) { const result: string[] = []; if (types.includes('follow')) result.push('follow');