mirror of
https://iceshrimp.dev/crimekillz/trashposs
synced 2024-11-25 10:19:06 +01:00
[mastodon-client] POST /notifications/:id/dismiss; POST /notifications/clear
This commit is contained in:
parent
ac6ba79a36
commit
72619198b9
@ -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;
|
||||||
|
@ -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');
|
||||||
|
Loading…
Reference in New Issue
Block a user