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, |   NewStatusBody, | ||||||
|   Notification, |   Notification, | ||||||
|   OllamaConfigOptions, |   OllamaConfigOptions, | ||||||
|  |   PleromaEmoji, | ||||||
| } from "../types.js"; | } from "../types.js"; | ||||||
| import striptags from "striptags"; | import striptags from "striptags"; | ||||||
| import { PrismaClient } from "../generated/prisma/client.js"; | 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> => { | const storeUserData = async (notification: Notification): Promise<void> => { | ||||||
|   try { |   try { | ||||||
|     await prisma.user.upsert({ |     await prisma.user.upsert({ | ||||||
| @ -192,11 +217,16 @@ const postReplyToStatus = async ( | |||||||
|   ollamaResponseBody: OllamaResponse |   ollamaResponseBody: OllamaResponse | ||||||
| ) => { | ) => { | ||||||
|   const { pleromaInstanceUrl, bearerToken } = envConfig; |   const { pleromaInstanceUrl, bearerToken } = envConfig; | ||||||
|  |   const emojiList = await getInstanceEmojis(); | ||||||
|  |   let randomEmoji; | ||||||
|  |   if (emojiList) { | ||||||
|  |     randomEmoji = selectRandomEmoji(emojiList); | ||||||
|  |   } | ||||||
|   try { |   try { | ||||||
|     let mentions: string[]; |     let mentions: string[]; | ||||||
|     const statusBody: NewStatusBody = { |     const statusBody: NewStatusBody = { | ||||||
|       content_type: "text/markdown", |       content_type: "text/markdown", | ||||||
|       status: ollamaResponseBody.response, |       status: `${ollamaResponseBody.response} :${randomEmoji}:`, | ||||||
|       in_reply_to_id: notification.status.id, |       in_reply_to_id: notification.status.id, | ||||||
|     }; |     }; | ||||||
|     if ( |     if ( | ||||||
|  | |||||||
							
								
								
									
										9
									
								
								types.d.ts
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										9
									
								
								types.d.ts
									
									
									
									
										vendored
									
									
								
							| @ -73,6 +73,15 @@ export interface Mention { | |||||||
|   username: string; |   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) |  * 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