sharkey/packages/backend/src/server/api/mastodon/endpoints/notifications.ts

72 lines
2.1 KiB
TypeScript
Raw Normal View History

import { IdConvertType as IdType, convertId, convertNotification } from '../converters.js';
2023-09-25 00:36:13 +09:00
import { convertTimelinesArgsId } from './timeline.js';
import type { MegalodonInterface, Entity } from 'megalodon';
import type { FastifyRequest } from 'fastify';
function toLimitToInt(q: any) {
2023-09-25 00:36:13 +09:00
if (q.limit) if (typeof q.limit === 'string') q.limit = parseInt(q.limit, 10);
return q;
}
2023-09-25 00:36:13 +09:00
export class ApiNotifyMastodon {
private request: FastifyRequest;
private client: MegalodonInterface;
2023-09-25 00:36:13 +09:00
constructor(request: FastifyRequest, client: MegalodonInterface) {
this.request = request;
this.client = client;
}
2023-09-25 00:36:13 +09:00
public async getNotifications() {
try {
const data = await this.client.getNotifications( convertTimelinesArgsId(toLimitToInt(this.request.query)) );
2023-09-25 00:36:13 +09:00
const notifs = data.data;
const processed = notifs.map((n: Entity.Notification) => {
let convertedn = convertNotification(n);
if (convertedn.type !== 'follow' && convertedn.type !== 'follow_request') {
if (convertedn.type === 'reaction') convertedn.type = 'favourite';
return convertedn;
} else {
return convertedn;
}
});
return processed;
} catch (e: any) {
console.error(e);
return e.response.data;
}
2023-09-25 00:36:13 +09:00
}
2023-09-25 00:36:13 +09:00
public async getNotification() {
try {
const data = await this.client.getNotification( convertId((this.request.params as any).id, IdType.SharkeyId) );
2023-09-25 00:36:13 +09:00
const notif = convertNotification(data.data);
if (notif.type !== 'follow' && notif.type !== 'follow_request' && notif.type === 'reaction') notif.type = 'favourite';
return notif;
} catch (e: any) {
console.error(e);
return e.response.data;
}
2023-09-25 00:36:13 +09:00
}
2023-09-25 00:36:13 +09:00
public async rmNotification() {
try {
const data = await this.client.dismissNotification( convertId((this.request.params as any).id, IdType.SharkeyId) );
return data.data;
} catch (e: any) {
console.error(e);
return e.response.data;
}
2023-09-25 00:36:13 +09:00
}
2023-09-25 00:36:13 +09:00
public async rmNotifications() {
try {
const data = await this.client.dismissNotifications();
return data.data;
} catch (e: any) {
console.error(e);
return e.response.data;
}
2023-09-25 00:36:13 +09:00
}
}