smaller msgdb size
This commit is contained in:
35
serve.ts
35
serve.ts
@@ -2,7 +2,7 @@ import { SHA512_256, type ServerWebSocket } from "bun";
|
||||
import { SnowflakeGenerator } from "./snowflake.ts";
|
||||
|
||||
function getContentType(path: string): string {
|
||||
if (path.endsWith(".html")) return "text/html";
|
||||
if (path.endsWith(".html") || path.endsWith(".htm")) return "text/html";
|
||||
if (path.endsWith(".css")) return "text/css";
|
||||
if (path.endsWith(".js")) return "application/javascript";
|
||||
if (path.endsWith(".json")) return "application/json";
|
||||
@@ -32,19 +32,19 @@ enum OpcodesServerbound {
|
||||
}
|
||||
|
||||
interface user {
|
||||
name: string;
|
||||
name?: string;
|
||||
username: string;
|
||||
pfp: string;
|
||||
id: number;
|
||||
token: string;
|
||||
password: string;
|
||||
dev: boolean;
|
||||
bot: boolean;
|
||||
pfp?: string;
|
||||
id: string;
|
||||
token?: string;
|
||||
password?: string;
|
||||
dev?: boolean;
|
||||
bot?: boolean;
|
||||
}
|
||||
|
||||
interface message {
|
||||
id: string;
|
||||
author: user;
|
||||
author: string | user; // server sends the user object but stores only the id for easier user.json modification
|
||||
content: string;
|
||||
timestamp: number;
|
||||
editedTimestamp?: number;
|
||||
@@ -121,10 +121,15 @@ const server = Bun.serve({
|
||||
data: user[0],
|
||||
})
|
||||
);
|
||||
let msgs = JSON.parse(JSON.stringify(messages));
|
||||
for (const ms of msgs) {
|
||||
const user = users.find(user => user.id === ms.author);
|
||||
ms.author = user || { username: "unknown-user", id: "0" };
|
||||
}
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
op: OpcodesClientbound.initMessages,
|
||||
data: messages,
|
||||
data: msgs,
|
||||
})
|
||||
);
|
||||
} else {
|
||||
@@ -144,6 +149,7 @@ const server = Bun.serve({
|
||||
obj.data.author.dev = user[0].dev;
|
||||
ws.publish("messages", JSON.stringify(obj));
|
||||
ws.send(JSON.stringify(obj));
|
||||
obj.data.author = obj.data.author.id;
|
||||
messages.push(obj.data);
|
||||
Bun.write("./db/messages.json", JSON.stringify(messages));
|
||||
} else {
|
||||
@@ -187,7 +193,14 @@ const server = Bun.serve({
|
||||
path = "/index.html";
|
||||
}
|
||||
|
||||
if (url.pathname === "/ws") {
|
||||
if (path === "/pfps/" || path === "/pfps") {
|
||||
return new Response(
|
||||
"<center><h1>403 Forbidden</h1><hr><p>Bun</p></center>",
|
||||
{ status: 403, headers: { "Content-Type": "text/html" } }
|
||||
);
|
||||
}
|
||||
|
||||
if (path === "/ws") {
|
||||
const upgraded = server.upgrade(req, {
|
||||
// @ts-expect-error guhh whqt
|
||||
data: {
|
||||
|
||||
Reference in New Issue
Block a user