Compare commits
	
		
			7 Commits
		
	
	
		
			add-websoc
			...
			1a151b197b
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 1a151b197b | |||
| 70180c5d5f | |||
| dac037809c | |||
| 6088a2cbd3 | |||
| ed8d148d0a | |||
| 379099dc7a | |||
| c0ed38ac1a | 
							
								
								
									
										10
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										10
									
								
								README.md
									
									
									
									
									
								
							| @ -1,15 +1,15 @@ | ||||
| ## Pleroma -> Ollama Bot Setup | ||||
|  | ||||
| 1. Clone project | ||||
| 2. Install npm 22.11.0 if you don't have it already | ||||
| 2. Install Node `v22.11.0` if you don't have it already | ||||
|     * If using `nvm`, just `nvm install 22.11.0` and then `nvm use 22.11.0` if necessary | ||||
| 3. `cd` into the project directory | ||||
| 4. Run `npm install` | ||||
| 6. Run `npx prisma migrate dev --name init` | ||||
| 7. To run the software on a cronjob, use `npm run once` | ||||
| 8. To run continuously, use `npm run ws` | ||||
| 7. To start, run `npm run start` | ||||
|  | ||||
| I recommend using `screen` to run this in the background until a `systemd` service can be created. I just haven't bothered to do it yet. | ||||
|  | ||||
| ### Database Migrations | ||||
|  | ||||
| If you add stuff to the schema, follow the [Prisma development workflow](https://www.prisma.io/docs/orm/prisma-migrate/workflows/development-and-production). This will apply the new schema to the database and generate a new Prisma client with type safety. | ||||
|  | ||||
| Setting as a system service will come at some point, or someone could contribute if they wanted.  | ||||
							
								
								
									
										90
									
								
								src/main.ts
									
									
									
									
									
								
							
							
						
						
									
										90
									
								
								src/main.ts
									
									
									
									
									
								
							| @ -8,31 +8,10 @@ import { | ||||
| import striptags from "striptags"; | ||||
| import { PrismaClient } from "../generated/prisma/client.js"; | ||||
| import { createWebsocket } from "./websocket.js"; | ||||
| import { WebSocket } from "ws"; | ||||
|  | ||||
| const prisma = new PrismaClient(); | ||||
|  | ||||
| // const getNotifications = async () => { | ||||
| //   try { | ||||
| //     const request = await fetch( | ||||
| //       `${process.env.PLEROMA_INSTANCE_URL}/api/v1/notifications?types[]=mention`, | ||||
| //       { | ||||
| //         method: "GET", | ||||
| //         headers: { | ||||
| //           Authorization: `Bearer ${process.env.INSTANCE_BEARER_TOKEN}`, | ||||
| //         }, | ||||
| //       } | ||||
| //     ); | ||||
|  | ||||
| //     const notifications: Notification[] = await request.json(); | ||||
|  | ||||
| //     return notifications; | ||||
| //   } catch (error: any) { | ||||
| //     throw new Error(error.message); | ||||
| //   } | ||||
| // }; | ||||
|  | ||||
| // const notifications = await getNotifications(); | ||||
|  | ||||
| const storeUserData = async (notification: Notification): Promise<void> => { | ||||
|   try { | ||||
|     await prisma.user.upsert({ | ||||
| @ -173,7 +152,24 @@ const postReplyToStatus = async ( | ||||
|   } | ||||
| }; | ||||
|  | ||||
| const ws = createWebsocket(); | ||||
| let ws = createWebsocket(); | ||||
|  | ||||
| const reconnect = (ws: WebSocket) => { | ||||
|   if (ws) { | ||||
|     ws.close(); | ||||
|   } | ||||
|   return createWebsocket(); | ||||
| }; | ||||
|  | ||||
| ws.on("close", () => { | ||||
|   for (let i = 0; i < 5; i++) { | ||||
|     if (ws.readyState !== WebSocket.OPEN) { | ||||
|       setTimeout(() => { | ||||
|         ws = reconnect(ws); | ||||
|       }, 5000); | ||||
|     } | ||||
|   } | ||||
| }); | ||||
|  | ||||
| ws.on("upgrade", () => { | ||||
|   console.log( | ||||
| @ -181,27 +177,31 @@ ws.on("upgrade", () => { | ||||
|   ); | ||||
| }); | ||||
|  | ||||
| ws.on("message", async (data) => { | ||||
|   const message: WSEvent = JSON.parse(data.toString("utf-8")); | ||||
|   if (message.event !== "notification") { | ||||
|     // only watch for notification events | ||||
|     return; | ||||
|   } | ||||
|   console.log("Websocket message received."); | ||||
|   const payload = JSON.parse(message.payload) as Notification; | ||||
|   const ollamaResponse = await generateOllamaRequest(payload); | ||||
|   if (ollamaResponse) { | ||||
|     await postReplyToStatus(payload, ollamaResponse); | ||||
|   } | ||||
| ws.on("open", () => { | ||||
|   setInterval(() => { | ||||
|     ws.send(JSON.stringify({ type: "ping" })); | ||||
|     console.log("Sending ping to keep session alive..."); | ||||
|   }, 20000); | ||||
| }); | ||||
|  | ||||
| // if (notifications) { | ||||
| //   await Promise.all( | ||||
| //     notifications.map(async (notification) => { | ||||
| //       const ollamaResponse = await generateOllamaRequest(notification); | ||||
| //       if (ollamaResponse) { | ||||
| //         postReplyToStatus(notification, ollamaResponse); | ||||
| //       } | ||||
| //     }) | ||||
| //   ); | ||||
| // } | ||||
| ws.on("pong", (data) => { | ||||
|   console.log(`Pong received: ${JSON.stringify(data.toString("utf-8"))}`); | ||||
| }); | ||||
|  | ||||
| ws.on("message", async (data) => { | ||||
|   try { | ||||
|     const message: WSEvent = JSON.parse(data.toString("utf-8")); | ||||
|     if (message.event !== "notification") { | ||||
|       // only watch for notification events | ||||
|       return; | ||||
|     } | ||||
|     console.log("Websocket message received."); | ||||
|     const payload = JSON.parse(message.payload) as Notification; | ||||
|     const ollamaResponse = await generateOllamaRequest(payload); | ||||
|     if (ollamaResponse) { | ||||
|       await postReplyToStatus(payload, ollamaResponse); | ||||
|     } | ||||
|   } catch (error: any) { | ||||
|     console.error(error.message); | ||||
|   } | ||||
| }); | ||||
|  | ||||
		Reference in New Issue
	
	Block a user