Chat History added.

This commit is contained in:
Daniel 2024-02-03 22:04:53 -03:00
parent c257ffbd2a
commit fadb92e544
2 changed files with 42 additions and 14 deletions

View File

@ -14,6 +14,7 @@ import { findSocketByPartID, socketsBySocketID } from "../ws/Socket";
import Crown from "./Crown"; import Crown from "./Crown";
import { ChannelList } from "./ChannelList"; import { ChannelList } from "./ChannelList";
import { config } from "./config"; import { config } from "./config";
import { saveChatHistory, getChatHistory } from "../data/history";
import { prisma } from "../data/prisma"; import { prisma } from "../data/prisma";
interface CachedKickban { interface CachedKickban {
@ -25,9 +26,13 @@ interface CachedKickban {
export class Channel extends EventEmitter { export class Channel extends EventEmitter {
private settings: Partial<IChannelSettings> = config.defaultSettings; private settings: Partial<IChannelSettings> = config.defaultSettings;
private ppl = new Array<Participant>(); private ppl = new Array<Participant>();
public chatHistory = new Array<ClientEvents["a"]>();
private async loadChatHistory() {
this.chatHistory = await getChatHistory(this.getID());
}
public logger: Logger; public logger: Logger;
public chatHistory = new Array<ClientEvents["a"]>();
public bans = new Array<CachedKickban>(); public bans = new Array<CachedKickban>();
public crown?: Crown; public crown?: Crown;
@ -41,6 +46,7 @@ export class Channel extends EventEmitter {
) { ) {
super(); super();
this.logger = new Logger("Channel - " + _id); this.logger = new Logger("Channel - " + _id);
// Validate settings in set // Validate settings in set
@ -83,6 +89,8 @@ export class Channel extends EventEmitter {
private bindEventListeners() { private bindEventListeners() {
if (this.alreadyBound) return; if (this.alreadyBound) return;
this.alreadyBound = true; this.alreadyBound = true;
this.loadChatHistory()
this.logger.info("Loaded Chat History.");
this.on("update", () => { this.on("update", () => {
// Send updated info // Send updated info
@ -102,7 +110,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; if (!msg.message) return;
const userFlags = socket.getUserFlags(); const userFlags = socket.getUserFlags();
@ -126,19 +134,8 @@ export class Channel extends EventEmitter {
this.sendArray([outgoing]); this.sendArray([outgoing]);
this.chatHistory.push(outgoing); this.chatHistory.push(outgoing);
await saveChatHistory(this.getID(), this.chatHistory)
prisma.chatHistory.upsert({
where:{
id: this._id
},
update:{
messages: JSON.stringify(this.chatHistory)
},
create: {
id:this._id,
messages: JSON.stringify(this.chatHistory)
}
})
try { try {
if (msg.message.startsWith("/")) { if (msg.message.startsWith("/")) {
this.emit("command", msg, socket); this.emit("command", msg, socket);

View File

@ -1 +1,32 @@
// TODO Chat history // 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(await history.messages);
}
}