[mastodon-client] GET /notifications/:id

This commit is contained in:
Laura Hausmann 2023-09-28 19:23:16 +02:00
parent 58dcbe68b7
commit ac6ba79a36
No known key found for this signature in database
GPG Key ID: D044E84C5BE01605
3 changed files with 21 additions and 19 deletions

View File

@ -12,8 +12,8 @@ import { getNote } from "@/server/api/common/getters.js";
type NotificationType = typeof notificationTypes[number]; type NotificationType = typeof notificationTypes[number];
export class NotificationConverter { export class NotificationConverter {
public static async encode(notification: Notification, localUser: ILocalUser, cache: AccountCache = UserHelpers.getFreshAccountCache()): Promise<MastodonEntity.Notification | null> { public static async encode(notification: Notification, localUser: ILocalUser, cache: AccountCache = UserHelpers.getFreshAccountCache()): Promise<MastodonEntity.Notification> {
if (notification.notifieeId !== localUser.id) return null; if (notification.notifieeId !== localUser.id) throw new Error('User is not recipient of notification');
//TODO: Test this (poll ended etc) //TODO: Test this (poll ended etc)
const account = notification.notifierId const account = notification.notifierId

View File

@ -15,7 +15,6 @@ function toLimitToInt(q: any) {
export function apiNotificationsMastodon(router: Router): void { export function apiNotificationsMastodon(router: Router): void {
router.get("/v1/notifications", async (ctx) => { router.get("/v1/notifications", async (ctx) => {
const body: any = ctx.request.body;
try { try {
const auth = await authenticate(ctx.headers.authorization, null); const auth = await authenticate(ctx.headers.authorization, null);
const user = auth[0] ?? null; const user = auth[0] ?? null;
@ -39,24 +38,23 @@ export function apiNotificationsMastodon(router: Router): void {
} }
}); });
router.get("/v1/notification/:id", async (ctx) => { router.get("/v1/notifications/: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;
try { try {
const dataRaw = await client.getNotification( const auth = await authenticate(ctx.headers.authorization, null);
convertId(ctx.params.id, IdType.IceshrimpId), const user = auth[0] ?? null;
);
const data = convertNotification(dataRaw.data); if (!user) {
ctx.body = data; ctx.status = 401;
if ( return;
data.type !== "follow" &&
data.type !== "follow_request" &&
data.type === "reaction"
) {
data.type = "favourite";
} }
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) { } catch (e: any) {
console.error(e); console.error(e);
ctx.status = 401; ctx.status = 401;

View File

@ -33,6 +33,10 @@ export class NotificationHelpers {
return PaginationHelpers.execQuery(query, limit, minId !== undefined); return PaginationHelpers.execQuery(query, limit, minId !== undefined);
} }
public static async getNotification(id: string, user: ILocalUser): Promise<Notification | null> {
return Notifications.findOneBy({id: id, notifieeId: user.id});
}
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');