add instance custom emojis to responses
This commit is contained in:
32
src/main.ts
32
src/main.ts
@ -4,6 +4,7 @@ import {
|
||||
NewStatusBody,
|
||||
Notification,
|
||||
OllamaConfigOptions,
|
||||
PleromaEmoji,
|
||||
} from "../types.js";
|
||||
import striptags from "striptags";
|
||||
import { PrismaClient } from "../generated/prisma/client.js";
|
||||
@ -53,6 +54,30 @@ const getNotifications = async () => {
|
||||
}
|
||||
};
|
||||
|
||||
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 selectRandomEmoji = (emojiList: string[]) => {
|
||||
return emojiList[Math.floor(Math.random() * emojiList.length)];
|
||||
};
|
||||
|
||||
const storeUserData = async (notification: Notification): Promise<void> => {
|
||||
try {
|
||||
await prisma.user.upsert({
|
||||
@ -192,11 +217,16 @@ const postReplyToStatus = async (
|
||||
ollamaResponseBody: OllamaResponse
|
||||
) => {
|
||||
const { pleromaInstanceUrl, bearerToken } = envConfig;
|
||||
const emojiList = await getInstanceEmojis();
|
||||
let randomEmoji;
|
||||
if (emojiList) {
|
||||
randomEmoji = selectRandomEmoji(emojiList);
|
||||
}
|
||||
try {
|
||||
let mentions: string[];
|
||||
const statusBody: NewStatusBody = {
|
||||
content_type: "text/markdown",
|
||||
status: ollamaResponseBody.response,
|
||||
status: `${ollamaResponseBody.response} :${randomEmoji}:`,
|
||||
in_reply_to_id: notification.status.id,
|
||||
};
|
||||
if (
|
||||
|
9
types.d.ts
vendored
9
types.d.ts
vendored
@ -73,6 +73,15 @@ export interface Mention {
|
||||
username: string;
|
||||
}
|
||||
|
||||
export interface PleromaEmoji {
|
||||
[emojiName: string]: PleromaEmojiMetadata;
|
||||
}
|
||||
|
||||
interface PleromaEmojiMetadata {
|
||||
image_url: string;
|
||||
tags: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Experimental settings, I wouldn't recommend messing with these if you don't know how they work (I don't either)
|
||||
*/
|
||||
|
Reference in New Issue
Block a user