mirror of
https://iceshrimp.dev/crimekillz/trashposs
synced 2024-11-22 08:53:48 +01:00
[mastodon-client] GET /notifications/:id
This commit is contained in:
parent
58dcbe68b7
commit
ac6ba79a36
@ -12,8 +12,8 @@ import { getNote } from "@/server/api/common/getters.js";
|
||||
type NotificationType = typeof notificationTypes[number];
|
||||
|
||||
export class NotificationConverter {
|
||||
public static async encode(notification: Notification, localUser: ILocalUser, cache: AccountCache = UserHelpers.getFreshAccountCache()): Promise<MastodonEntity.Notification | null> {
|
||||
if (notification.notifieeId !== localUser.id) return null;
|
||||
public static async encode(notification: Notification, localUser: ILocalUser, cache: AccountCache = UserHelpers.getFreshAccountCache()): Promise<MastodonEntity.Notification> {
|
||||
if (notification.notifieeId !== localUser.id) throw new Error('User is not recipient of notification');
|
||||
|
||||
//TODO: Test this (poll ended etc)
|
||||
const account = notification.notifierId
|
||||
|
@ -15,7 +15,6 @@ function toLimitToInt(q: any) {
|
||||
|
||||
export function apiNotificationsMastodon(router: Router): void {
|
||||
router.get("/v1/notifications", async (ctx) => {
|
||||
const body: any = ctx.request.body;
|
||||
try {
|
||||
const auth = await authenticate(ctx.headers.authorization, null);
|
||||
const user = auth[0] ?? null;
|
||||
@ -39,24 +38,23 @@ export function apiNotificationsMastodon(router: Router): void {
|
||||
}
|
||||
});
|
||||
|
||||
router.get("/v1/notification/: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;
|
||||
router.get("/v1/notifications/:id", async (ctx) => {
|
||||
try {
|
||||
const dataRaw = await client.getNotification(
|
||||
convertId(ctx.params.id, IdType.IceshrimpId),
|
||||
);
|
||||
const data = convertNotification(dataRaw.data);
|
||||
ctx.body = data;
|
||||
if (
|
||||
data.type !== "follow" &&
|
||||
data.type !== "follow_request" &&
|
||||
data.type === "reaction"
|
||||
) {
|
||||
data.type = "favourite";
|
||||
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;
|
||||
}
|
||||
|
||||
ctx.body = convertNotification(await NotificationConverter.encode(notification, user));
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
ctx.status = 401;
|
||||
|
@ -33,6 +33,10 @@ export class NotificationHelpers {
|
||||
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[]) {
|
||||
const result: string[] = [];
|
||||
if (types.includes('follow')) result.push('follow');
|
||||
|
Loading…
Reference in New Issue
Block a user