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