Merge branch 'daniel176-master'

This commit is contained in:
Hri7566 2024-02-04 08:40:05 -05:00
commit 3dd5201750
5 changed files with 68 additions and 12 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -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<IChannelSettings> = config.defaultSettings;
private ppl = new Array<Participant>();
public chatHistory = new Array<ClientEvents["a"]>();
private async loadChatHistory() {
this.chatHistory = await getChatHistory(this.getID());
}
public logger: Logger;
public chatHistory = new Array<ClientEvents["a"]>();
public bans = new Array<CachedKickban>();
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("/")) {

View File

@ -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);
}
}

View File

@ -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 }

View File

@ -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 => {
new Command(["list", "ls"], "list <channels, users>", 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 <channels, users>";
}
} else {
return "list <channels, users>";
}
})
);