[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) => {
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;

View File

@ -37,6 +37,14 @@ export class NotificationHelpers {
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[]) {
const result: string[] = [];
if (types.includes('follow')) result.push('follow');