diff --git a/bun.lockb b/bun.lockb index dcce92a..0816a0d 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/src/channel/Channel.ts b/src/channel/Channel.ts index b5136d8..eebf125 100644 --- a/src/channel/Channel.ts +++ b/src/channel/Channel.ts @@ -1,4 +1,4 @@ -import EventEmitter, { on } from "events"; +import EventEmitter from "events"; import { Logger } from "../util/Logger"; import { ChannelSettingValue, @@ -14,6 +14,7 @@ import { findSocketByPartID, socketsBySocketID } from "../ws/Socket"; import Crown from "./Crown"; import { ChannelList } from "./ChannelList"; import { config } from "./config"; +import { saveChatHistory, getChatHistory } from "../data/history"; interface CachedKickban { userId: string; @@ -24,9 +25,12 @@ interface CachedKickban { export class Channel extends EventEmitter { private settings: Partial = config.defaultSettings; private ppl = new Array(); + public chatHistory = new Array(); + private async loadChatHistory() { + this.chatHistory = await getChatHistory(this.getID()); + } public logger: Logger; - public chatHistory = new Array(); public bans = new Array(); public crown?: Crown; @@ -82,6 +86,8 @@ export class Channel extends EventEmitter { private bindEventListeners() { if (this.alreadyBound) return; this.alreadyBound = true; + this.loadChatHistory(); + this.logger.info("Loaded Chat History."); this.on("update", () => { // Send updated info @@ -101,7 +107,7 @@ export class Channel extends EventEmitter { } }); - this.on("message", (msg: ServerEvents["a"], socket: Socket) => { + this.on("message", async (msg: ServerEvents["a"], socket: Socket) => { if (!msg.message) return; const userFlags = socket.getUserFlags(); @@ -125,6 +131,7 @@ export class Channel extends EventEmitter { this.sendArray([outgoing]); this.chatHistory.push(outgoing); + await saveChatHistory(this.getID(), this.chatHistory); try { if (msg.message.startsWith("/")) { diff --git a/src/data/history.ts b/src/data/history.ts index 2e9297c..bab7aad 100644 --- a/src/data/history.ts +++ b/src/data/history.ts @@ -1 +1,25 @@ -// TODO Chat history +import { prisma } from "./prisma"; + +export async function saveChatHistory(_id: string, chatHistory: object) { + await prisma.chatHistory.upsert({ + where: { + id: _id + }, + update: { + messages: JSON.stringify(chatHistory) + }, + create: { + id: _id, + messages: JSON.stringify(chatHistory) + } + }); +} + +export async function getChatHistory(_id: string) { + const history = await prisma.chatHistory.findFirst({ where: { id: _id } }); + if (!history) { + return []; + } else { + return JSON.parse(history.messages); + } +} diff --git a/src/data/user.ts b/src/data/user.ts index c932664..5e523c1 100644 --- a/src/data/user.ts +++ b/src/data/user.ts @@ -13,6 +13,13 @@ export async function createUser( }); } +export async function getUsers() { + return await { + users: await prisma.user.findMany(), + count: await prisma.user.count() + } +} + export async function deleteUser(_id: string) { return await prisma.user.delete({ where: { id: _id } diff --git a/src/util/readline/commands.ts b/src/util/readline/commands.ts index 7433dae..19375dc 100644 --- a/src/util/readline/commands.ts +++ b/src/util/readline/commands.ts @@ -1,5 +1,5 @@ import { ChannelList } from "../../channel/ChannelList"; -import { deleteUser } from "../../data/user"; +import { deleteUser, getUsers } from "../../data/user"; import Command from "./Command"; Command.addCommand( @@ -50,12 +50,30 @@ Command.addCommand( ); Command.addCommand( - new Command(["list", "ls"], "list", async msg => { - return ( - "Channels:\n- " + - ChannelList.getList() - .map(ch => ch.getID()) - .join("\n- ") - ); + new Command(["list", "ls"], "list ", async msg => { + if(msg.args.length > 1) { + if(msg.args[1] == "channels") { + return ( + "Channels:\n- " + + ChannelList.getList() + .map(ch => ch.getID()) + .join("\n- ") + ); + } else if (msg.args[1] == "users") { + var user = getUsers(); + var users = ""; + ((await user).users).forEach(u => { + users += `\n- [${u.id}]: ${u.name}` + }) + + return ( + "Users: "+await (await user).count + users + ) + } else { + return "list "; + } + } else { + return "list "; + } }) );