its called we do a little abstraction
This commit is contained in:
75
src/api.ts
Normal file
75
src/api.ts
Normal file
@ -0,0 +1,75 @@
|
||||
import { envConfig, prisma } from "./main.js";
|
||||
import { PleromaEmoji, Notification } from "../types.js";
|
||||
|
||||
const getNotifications = async () => {
|
||||
const { bearerToken, pleromaInstanceUrl } = envConfig;
|
||||
try {
|
||||
const request = await fetch(
|
||||
`${pleromaInstanceUrl}/api/v1/notifications?types[]=mention`,
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${bearerToken}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
const notifications: Notification[] = await request.json();
|
||||
|
||||
return notifications;
|
||||
} catch (error: any) {
|
||||
throw new Error(error.message);
|
||||
}
|
||||
};
|
||||
|
||||
const getInstanceEmojis = async () => {
|
||||
const { bearerToken, pleromaInstanceUrl } = envConfig;
|
||||
try {
|
||||
const request = await fetch(`${pleromaInstanceUrl}/api/v1/pleroma/emoji`, {
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${bearerToken}`,
|
||||
},
|
||||
});
|
||||
if (!request.ok) {
|
||||
console.error(`Emoji GET failed: ${request.status}`);
|
||||
return;
|
||||
}
|
||||
const emojis: PleromaEmoji[] = await request.json();
|
||||
return Object.keys(emojis);
|
||||
} catch (error: any) {
|
||||
console.error(`Could not fetch emojis: ${error.message}`);
|
||||
}
|
||||
};
|
||||
|
||||
const deleteNotification = async (notification: Notification) => {
|
||||
const { pleromaInstanceUrl, bearerToken } = envConfig;
|
||||
try {
|
||||
if (!notification.id) {
|
||||
return;
|
||||
}
|
||||
await prisma.response.updateMany({
|
||||
// this is probably not the best way to do this, but since we may have duplicate notifications, we have to update all of them - probably won't scale (lmao)
|
||||
where: { pleromaNotificationId: notification.id },
|
||||
data: { isProcessing: false },
|
||||
});
|
||||
const response = await fetch(
|
||||
`${pleromaInstanceUrl}/api/v1/notifications/${notification.id}/dismiss`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Bearer ${bearerToken}`,
|
||||
},
|
||||
}
|
||||
);
|
||||
if (!response.ok) {
|
||||
console.error(
|
||||
`Could not delete notification ID: ${notification.id}\nReason: ${response.status} - ${response.statusText}`
|
||||
);
|
||||
}
|
||||
} catch (error: any) {
|
||||
throw new Error(error.message);
|
||||
}
|
||||
};
|
||||
|
||||
export { deleteNotification, getInstanceEmojis, getNotifications };
|
Reference in New Issue
Block a user