diff --git a/packages/backend/src/server/api/mastodon/endpoints/notifications.ts b/packages/backend/src/server/api/mastodon/endpoints/notifications.ts index ad0851dc8..8e57ac6e1 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/notifications.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/notifications.ts @@ -63,13 +63,17 @@ export function apiNotificationsMastodon(router: Router): void { }); router.post("/v1/notifications/clear", 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; try { - const data = await client.dismissNotifications(); - ctx.body = data.data; + const auth = await authenticate(ctx.headers.authorization, null); + const user = auth[0] ?? null; + + if (!user) { + ctx.status = 401; + return; + } + + await NotificationHelpers.clearAllNotifications(user); + ctx.body = {}; } catch (e: any) { console.error(e); ctx.status = 401; @@ -77,16 +81,24 @@ export function apiNotificationsMastodon(router: Router): void { } }); - router.post("/v1/notification/:id/dismiss", 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.post("/v1/notifications/:id/dismiss", async (ctx) => { try { - const data = await client.dismissNotification( - convertId(ctx.params.id, IdType.IceshrimpId), - ); - ctx.body = data.data; + 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; + } + + await NotificationHelpers.dismissNotification(notification.id, user); + ctx.body = {}; } 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 d72652a0a..64e479883 100644 --- a/packages/backend/src/server/api/mastodon/helpers/notification.ts +++ b/packages/backend/src/server/api/mastodon/helpers/notification.ts @@ -37,6 +37,14 @@ export class NotificationHelpers { return Notifications.findOneBy({id: id, notifieeId: user.id}); } + public static async dismissNotification(id: string, user: ILocalUser): Promise { + const result = await Notifications.update({id: id, notifieeId: user.id}, {isRead: true}); + } + + public static async clearAllNotifications(user: ILocalUser): Promise { + await Notifications.update({notifieeId: user.id}, {isRead: true}); + } + private static decodeTypes(types: string[]) { const result: string[] = []; if (types.includes('follow')) result.push('follow');