add domain whitelist functionality
This commit is contained in:
		
							
								
								
									
										31
									
								
								src/main.ts
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								src/main.ts
									
									
									
									
									
								
							| @ -13,7 +13,10 @@ const prisma = new PrismaClient(); | ||||
| const envConfig = { | ||||
|   pleromaInstanceUrl: process.env.PLEROMA_INSTANCE_URL || "", | ||||
|   pleromaInstanceDomain: process.env.PLEROMA_INSTANCE_DOMAIN || "", | ||||
|   onlyLocalReplies: process.env.ONLY_LOCAL_REPLIES === "true" ? true : false, | ||||
|   whitelistOnly: process.env.ONLY_WHITELIST === "true" ? true : false || "true", | ||||
|   whitelistedDomains: process.env.WHITELISTED_DOMAINS | ||||
|     ? process.env.WHITELISTED_DOMAINS.split(",") | ||||
|     : [process.env.PLEROMA_INSTANCE_DOMAIN], | ||||
|   ollamaUrl: process.env.OLLAMA_URL || "", | ||||
|   ollamaSystemPrompt: | ||||
|     process.env.OLLAMA_SYSTEM_PROMPT || | ||||
| @ -123,16 +126,24 @@ const recordPendingResponse = async (notification: Notification) => { | ||||
|   } | ||||
| }; | ||||
|  | ||||
| const isFromWhitelistedDomain = (fqn: string): boolean => { | ||||
|   try { | ||||
|     const domain = fqn.split("@")[1]; | ||||
|     if (envConfig.whitelistedDomains.includes(domain)) { | ||||
|       return true; | ||||
|     } | ||||
|     return false; | ||||
|   } catch (error: any) { | ||||
|     console.error(`Error with domain check: ${error.message}`); | ||||
|     return false; | ||||
|   } | ||||
| }; | ||||
|  | ||||
| const generateOllamaRequest = async ( | ||||
|   notification: Notification | ||||
| ): Promise<OllamaResponse | undefined> => { | ||||
|   const { | ||||
|     onlyLocalReplies, | ||||
|     pleromaInstanceDomain, | ||||
|     ollamaModel, | ||||
|     ollamaSystemPrompt, | ||||
|     ollamaUrl, | ||||
|   } = envConfig; | ||||
|   const { whitelistOnly, ollamaModel, ollamaSystemPrompt, ollamaUrl } = | ||||
|     envConfig; | ||||
|   try { | ||||
|     if ( | ||||
|       striptags(notification.status.content).includes("!prompt") && | ||||
| @ -140,8 +151,8 @@ const generateOllamaRequest = async ( | ||||
|       notification.type === "mention" | ||||
|     ) { | ||||
|       if ( | ||||
|         onlyLocalReplies && | ||||
|         !notification.status.account.fqn.includes(`@${pleromaInstanceDomain}`) | ||||
|         whitelistOnly && | ||||
|         !isFromWhitelistedDomain(notification.status.account.fqn) | ||||
|       ) { | ||||
|         return; | ||||
|       } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user