109 lines
2.8 KiB
TypeScript
109 lines
2.8 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);
|
|
|
|
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 shouldContinue = (notification: Notification) => {
|
|
// wow this is bad
|
|
try {
|
|
const { botAccountId } = envConfig;
|
|
const statusContent = trimInputData(notification.status.content);
|
|
if (
|
|
// notification.status.visibility !== "private" &&
|
|
!notification.account.bot &&
|
|
notification.type === "mention"
|
|
) {
|
|
if (notification.status.in_reply_to_account_id === botAccountId) {
|
|
return true;
|
|
} else if (
|
|
notification.status.in_reply_to_account_id !== botAccountId &&
|
|
statusContent.includes("Lexi")
|
|
) {
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
} catch (error: unknown) {
|
|
if (error instanceof Error) {
|
|
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,
|
|
shouldContinue,
|
|
};
|