Files
pleroma-ollama-bot/src/util.ts

80 lines
2.1 KiB
TypeScript

import striptags from "striptags";
import { prisma } from "./main.js";
import { envConfig } from "./main.js";
import { Notification } from "../types.js";
const trimInputData = (input: string): string => {
const strippedInput = striptags(input, [], "\n");
const split = strippedInput.split(" ");
const promptStringIndex = split.indexOf("!prompt");
const botFqnIndex = split.indexOf("@nice-ai");
const botFqnIndexFull = split.indexOf("@nice-ai@nicecrew.digital");
if (botFqnIndex !== -1) {
split[botFqnIndex] = "Lexi";
}
if (botFqnIndexFull !== -1) {
split[botFqnIndexFull] = "Lexi";
}
split.splice(promptStringIndex, 1);
return split.join(" "); // returns everything after the !prompt
};
const recordPendingResponse = async (notification: Notification) => {
try {
await prisma.response.create({
data: {
pleromaNotificationId: notification.id,
isProcessing: true,
isComplete: false,
},
});
} catch (error: any) {
throw new Error(error.message);
}
};
const isFromWhitelistedDomain = (notification: Notification): boolean => {
try {
const domain = notification.status.account.fqn.split("@")[1];
if (envConfig.whitelistedDomains.includes(domain)) {
return true;
}
console.log(
`Rejecting prompt request from non-whitelisted domain: ${domain}`
);
return false;
} catch (error: any) {
console.error(`Error with domain check: ${error.message}`);
return false;
}
};
const alreadyRespondedTo = async (
notification: Notification
): Promise<boolean> => {
try {
const duplicate = await prisma.response.findFirst({
where: { pleromaNotificationId: notification.id },
});
if (duplicate?.isProcessing || duplicate?.isComplete) {
return true;
}
return false;
} catch (error: any) {
throw new Error(error.message);
}
};
const selectRandomEmoji = (emojiList: string[]) => {
return emojiList[Math.floor(Math.random() * emojiList.length)];
};
export {
alreadyRespondedTo,
selectRandomEmoji,
trimInputData,
recordPendingResponse,
isFromWhitelistedDomain,
};