mirror of
https://git.joinsharkey.org/Sharkey/Sharkey.git
synced 2024-12-23 22:03:08 +02:00
add: notification endpoints to masto api
This commit is contained in:
parent
bb2d4b0e09
commit
168c041373
2 changed files with 130 additions and 0 deletions
|
@ -14,6 +14,7 @@ import multer from 'fastify-multer';
|
|||
import { apiAuthMastodon } from './endpoints/auth.js';
|
||||
import { apiAccountMastodon } from './endpoints/account.js';
|
||||
import { apiSearchMastodon } from './endpoints/search.js';
|
||||
import { apiNotifyMastodon } from './endpoints/notifications.js';
|
||||
|
||||
const staticAssets = fileURLToPath(new URL('../../../../assets/', import.meta.url));
|
||||
|
||||
|
@ -608,6 +609,64 @@ export class MastodonApiServerService {
|
|||
reply.code(401).send(e.response.data);
|
||||
}
|
||||
});
|
||||
//#endregion
|
||||
|
||||
//#region Notifications
|
||||
fastify.get("/v1/notifications", async (_request, reply) => {
|
||||
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
try {
|
||||
const notify = new apiNotifyMastodon(_request, client);
|
||||
reply.send(await notify.getNotifications());
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
console.error(e.response.data);
|
||||
reply.code(401).send(e.response.data);
|
||||
}
|
||||
});
|
||||
|
||||
fastify.get<{ Params: { id: string } }>("/v1/notification/:id", async (_request, reply) => {
|
||||
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
try {
|
||||
const notify = new apiNotifyMastodon(_request, client);
|
||||
reply.send(await notify.getNotification());
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
console.error(e.response.data);
|
||||
reply.code(401).send(e.response.data);
|
||||
}
|
||||
});
|
||||
|
||||
fastify.post<{ Params: { id: string } }>("/v1/notification/:id/dismiss", async (_request, reply) => {
|
||||
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
try {
|
||||
const notify = new apiNotifyMastodon(_request, client);
|
||||
reply.send(await notify.rmNotification());
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
console.error(e.response.data);
|
||||
reply.code(401).send(e.response.data);
|
||||
}
|
||||
});
|
||||
|
||||
fastify.post("/v1/notifications/clear", async (_request, reply) => {
|
||||
const BASE_URL = `${_request.protocol}://${_request.hostname}`;
|
||||
const accessTokens = _request.headers.authorization;
|
||||
const client = getClient(BASE_URL, accessTokens);
|
||||
try {
|
||||
const notify = new apiNotifyMastodon(_request, client);
|
||||
reply.send(await notify.rmNotifications());
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
console.error(e.response.data);
|
||||
reply.code(401).send(e.response.data);
|
||||
}
|
||||
});
|
||||
//#endregion
|
||||
done();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,71 @@
|
|||
import type { MegalodonInterface } from "megalodon";
|
||||
import type { FastifyRequest } from 'fastify';
|
||||
import { convertTimelinesArgsId } from "./timeline.js";
|
||||
import { IdConvertType as IdType, convertId, convertNotification } from '../converters.js';
|
||||
|
||||
function toLimitToInt(q: any) {
|
||||
if (q.limit) if (typeof q.limit === "string") q.limit = parseInt(q.limit, 10);
|
||||
return q;
|
||||
}
|
||||
|
||||
export class apiNotifyMastodon {
|
||||
private request: FastifyRequest;
|
||||
private client: MegalodonInterface;
|
||||
|
||||
constructor(request: FastifyRequest, client: MegalodonInterface) {
|
||||
this.request = request;
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
public async getNotifications() {
|
||||
try {
|
||||
const data = await this.client.getNotifications( convertTimelinesArgsId(toLimitToInt(this.request.query)) );
|
||||
const notifs = data.data;
|
||||
const processed = notifs.map((n) => {
|
||||
n = convertNotification(n);
|
||||
if (n.type !== "follow" && n.type !== "follow_request") {
|
||||
if (n.type === "reaction") n.type = "favourite";
|
||||
return n;
|
||||
} else {
|
||||
return n;
|
||||
}
|
||||
});
|
||||
return processed;
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
return e.response.data;
|
||||
}
|
||||
}
|
||||
|
||||
public async getNotification() {
|
||||
try {
|
||||
const data = await this.client.getNotification( convertId((this.request.params as any).id, IdType.SharkeyId) );
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public async rmNotifications() {
|
||||
try {
|
||||
const data = await this.client.dismissNotifications();
|
||||
return data.data;
|
||||
} catch (e: any) {
|
||||
console.error(e);
|
||||
return e.response.data;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue