[mastodon-client] POST /notifications/:id/dismiss; POST /notifications/clear

This commit is contained in:
Laura Hausmann 2023-09-28 20:01:25 +02:00
parent ac6ba79a36
commit 72619198b9
No known key found for this signature in database
GPG Key ID: D044E84C5BE01605
2 changed files with 35 additions and 15 deletions

View File

@ -63,13 +63,17 @@ export function apiNotificationsMastodon(router: Router): void {
}); });
router.post("/v1/notifications/clear", async (ctx) => { 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 { try {
const data = await client.dismissNotifications(); const auth = await authenticate(ctx.headers.authorization, null);
ctx.body = data.data; const user = auth[0] ?? null;
if (!user) {
ctx.status = 401;
return;
}
await NotificationHelpers.clearAllNotifications(user);
ctx.body = {};
} catch (e: any) { } catch (e: any) {
console.error(e); console.error(e);
ctx.status = 401; ctx.status = 401;
@ -77,16 +81,24 @@ export function apiNotificationsMastodon(router: Router): void {
} }
}); });
router.post("/v1/notification/:id/dismiss", async (ctx) => { router.post("/v1/notifications/: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;
try { try {
const data = await client.dismissNotification( const auth = await authenticate(ctx.headers.authorization, null);
convertId(ctx.params.id, IdType.IceshrimpId), const user = auth[0] ?? null;
);
ctx.body = data.data; 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) { } catch (e: any) {
console.error(e); console.error(e);
ctx.status = 401; ctx.status = 401;

View File

@ -37,6 +37,14 @@ export class NotificationHelpers {
return Notifications.findOneBy({id: id, notifieeId: user.id}); return Notifications.findOneBy({id: id, notifieeId: user.id});
} }
public static async dismissNotification(id: string, user: ILocalUser): Promise<void> {
const result = await Notifications.update({id: id, notifieeId: user.id}, {isRead: true});
}
public static async clearAllNotifications(user: ILocalUser): Promise<void> {
await Notifications.update({notifieeId: user.id}, {isRead: true});
}
private static decodeTypes(types: string[]) { private static decodeTypes(types: string[]) {
const result: string[] = []; const result: string[] = [];
if (types.includes('follow')) result.push('follow'); if (types.includes('follow')) result.push('follow');