From e2ce3971189523a2a3dbacad4bac328c008b0216 Mon Sep 17 00:00:00 2001 From: matty Date: Sun, 6 Jul 2025 14:39:45 +0000 Subject: [PATCH] add instance custom emojis to responses --- src/main.ts | 32 +++++++++++++++++++++++++++++++- types.d.ts | 9 +++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/main.ts b/src/main.ts index 304749c..c87eb92 100644 --- a/src/main.ts +++ b/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 => { 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 ( diff --git a/types.d.ts b/types.d.ts index 9ef1f0d..c59b613 100644 --- a/types.d.ts +++ b/types.d.ts @@ -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) */