diff --git a/app/lib/ffmpeg.ts b/app/lib/ffmpeg.ts
index 3887a02..35c9df7 100644
--- a/app/lib/ffmpeg.ts
+++ b/app/lib/ffmpeg.ts
@@ -7,7 +7,7 @@ import which from "which";
import fs from "fs";
-import { wss } from "./ws";
+//import { wss } from "./ws";
/**
* Enum to represent different types of video encoding methods.
diff --git a/app/lib/lib.ts b/app/lib/lib.ts
index 7279357..47ec592 100644
--- a/app/lib/lib.ts
+++ b/app/lib/lib.ts
@@ -4,8 +4,8 @@ declare global {
interface User {
id?: number | string;
username: string;
- hashed_password?: any;
- salt?: any;
+ hashed_password?: Buffer;
+ salt?: Buffer;
}
}
}
@@ -21,8 +21,11 @@ export function extension(str: string) {
export interface User {
id?: number | string;
username: string;
- hashed_password?: any;
- salt?: any;
+}
+
+export interface UserRow extends User {
+ salt: Buffer;
+ hashed_password: Buffer;
}
export interface oembedObj {
diff --git a/app/lib/ws.ts b/app/lib/ws.ts
index c672e24..ed28975 100644
--- a/app/lib/ws.ts
+++ b/app/lib/ws.ts
@@ -1,7 +1,19 @@
+import { EventEmitter } from "events";
+
import WebSocket from "ws";
+const eventEmitter = new EventEmitter();
+
const wsPort = normalizePort(process.env.EBWSPORT || "3001");
+const clients: WebSocket[] = [];
+
+/**
+ * Normalizes a port number to ensure it is a valid integer.
+ *
+ * @param {string} val - The port number as a string.
+ * @returns {number} The normalized port number.
+ */
function normalizePort(val: string) {
const port = parseInt(val, 10);
@@ -13,8 +25,91 @@ function normalizePort(val: string) {
return port;
}
}
-
+/**
+ * The WebSocket server instance.
+ */
const wss = new WebSocket.Server({port: wsPort});
-export { wss };
+wss.on("connection", (ws) => {
+ clients.push(ws);
+
+ ws.on("message", handleMessage);
+
+ ws.on("close", handleMessage);
+
+ ws.on("error", handleMessage);
+
+ ws.on("close", () => {
+ const index = clients.indexOf(ws);
+ if (index !== -1) {
+ clients.splice(index, 1);
+ }
+ });
+});
+
+/**
+ * Handles incoming messages from clients.
+ *
+ * @param {string} message - The incoming message.
+ */
+function handleMessage(message: string) {
+ try {
+ const data = JSON.parse(message);
+
+ switch (data.type) {
+ case "message":
+ eventEmitter.emit("message", data.message);
+ break;
+ case "close":
+ eventEmitter.emit("close", data.userId);
+ break;
+ case "error":
+ eventEmitter.emit("error", data.error);
+ break;
+ default:
+ console.log(`Unknown message type: ${data.type}`);
+ }
+ } catch (error) {
+ console.log(`Error parsing message: ${error}`);
+ }
+}
+
+/**
+ * Broadcasts a message to all connected clients.
+ *
+ * @param {string} message - The message to broadcast.
+ */
+function broadcast(message: string) {
+ wss.clients.forEach((client) => {
+ if (client.readyState === WebSocket.OPEN) {
+ client.send(message);
+ }
+ });
+}
+
+/**
+ * Returns an array of all connected clients.
+ *
+ * @returns {WebSocket[]} An array of connected clients.
+ */
+function getClients() {
+ return clients;
+}
+
+/**
+ * Sends a message to a specific client.
+ *
+ * @param {string} clientId - The ID of the client to send the message to.
+ * @param {string} message - The message to send.
+ */
+/*function sendMessageToClient(clientId: string, message: string) {
+ const client = clients.find((client) => client.id === clientId);
+ if (client) {
+ client.send(message);
+ }
+}*/
+
+
+
+//export { wss, eventEmitter, broadcast, getClients, sendMessageToClient };
diff --git a/app/routes/auth.ts b/app/routes/auth.ts
index 04fe16b..c787435 100644
--- a/app/routes/auth.ts
+++ b/app/routes/auth.ts
@@ -46,12 +46,9 @@ passport.use(
}),
);
-passport.serializeUser(function (user: User, cb) {
- process.nextTick(function () {
- cb(null, {
- id: user.id,
- username: user.username,
- });
+passport.serializeUser((user: User, cb: (err: Error | null, id?: User) => void) => {
+ process.nextTick(() => {
+ cb(null, user); // No need to reconstruct the user object
});
});
diff --git a/app/routes/index.ts b/app/routes/index.ts
index 084d48f..a8dff93 100644
--- a/app/routes/index.ts
+++ b/app/routes/index.ts
@@ -124,7 +124,7 @@ router.get("/oembed/:file",
try {
const oembedData: oembedObj = {
- type: (videoExtensions.includes(fileExtension) ? "photo" : "video"),
+ type: (videoExtensions.includes(fileExtension) ? "video" : "photo"),
version: "1.0",
provider_name: "embedder",
provider_url: "https://github.com/WaveringAna/embedder",
@@ -134,16 +134,20 @@ router.get("/oembed/:file",
url: `${req.protocol}://${req.get("host")}/uploads/${filename}`
};
- if (videoExtensions.includes(fileExtension) || fileExtension === '.gif') {
+ if (videoExtensions.includes(fileExtension) || fileExtension === ".gif") {
const ffprobeData = await ffProbe(`uploads/${filename}`, filename, fileExtension);
oembedData.width = ffprobeData.streams[0].width;
oembedData.height = ffprobeData.streams[0].height;
-
- // Consider generating a thumbnail_url if it's a video
+
+ oembedData.html = ``;
} else {
const imageData = await imageProbe(fs.createReadStream(`uploads/${filename}`));
oembedData.width = imageData.width;
oembedData.height = imageData.height;
+
+ oembedData.html = `
+
+ `;
}
res.json(oembedData);