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";
|
import { SnowflakeGenerator } from "./snowflake.ts";
|
||||||
|
|
||||||
function getContentType(path: string): string {
|
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(".css")) return "text/css";
|
||||||
if (path.endsWith(".js")) return "application/javascript";
|
if (path.endsWith(".js")) return "application/javascript";
|
||||||
if (path.endsWith(".json")) return "application/json";
|
if (path.endsWith(".json")) return "application/json";
|
||||||
@@ -32,19 +32,19 @@ enum OpcodesServerbound {
|
|||||||
}
|
}
|
||||||
|
|
||||||
interface user {
|
interface user {
|
||||||
name: string;
|
name?: string;
|
||||||
username: string;
|
username: string;
|
||||||
pfp: string;
|
pfp?: string;
|
||||||
id: number;
|
id: string;
|
||||||
token: string;
|
token?: string;
|
||||||
password: string;
|
password?: string;
|
||||||
dev: boolean;
|
dev?: boolean;
|
||||||
bot: boolean;
|
bot?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface message {
|
interface message {
|
||||||
id: string;
|
id: string;
|
||||||
author: user;
|
author: string | user; // server sends the user object but stores only the id for easier user.json modification
|
||||||
content: string;
|
content: string;
|
||||||
timestamp: number;
|
timestamp: number;
|
||||||
editedTimestamp?: number;
|
editedTimestamp?: number;
|
||||||
@@ -121,10 +121,15 @@ const server = Bun.serve({
|
|||||||
data: user[0],
|
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(
|
ws.send(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
op: OpcodesClientbound.initMessages,
|
op: OpcodesClientbound.initMessages,
|
||||||
data: messages,
|
data: msgs,
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
@@ -144,6 +149,7 @@ const server = Bun.serve({
|
|||||||
obj.data.author.dev = user[0].dev;
|
obj.data.author.dev = user[0].dev;
|
||||||
ws.publish("messages", JSON.stringify(obj));
|
ws.publish("messages", JSON.stringify(obj));
|
||||||
ws.send(JSON.stringify(obj));
|
ws.send(JSON.stringify(obj));
|
||||||
|
obj.data.author = obj.data.author.id;
|
||||||
messages.push(obj.data);
|
messages.push(obj.data);
|
||||||
Bun.write("./db/messages.json", JSON.stringify(messages));
|
Bun.write("./db/messages.json", JSON.stringify(messages));
|
||||||
} else {
|
} else {
|
||||||
@@ -187,7 +193,14 @@ const server = Bun.serve({
|
|||||||
path = "/index.html";
|
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, {
|
const upgraded = server.upgrade(req, {
|
||||||
// @ts-expect-error guhh whqt
|
// @ts-expect-error guhh whqt
|
||||||
data: {
|
data: {
|
||||||
|
|||||||
Reference in New Issue
Block a user