Merge branch 'daniel176-master'
This commit is contained in:
commit
3dd5201750
|
@ -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("/")) {
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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 }
|
||||
|
|
|
@ -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 <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>";
|
||||
}
|
||||
})
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue