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 { Logger } from "../util/Logger";
|
||||||
import {
|
import {
|
||||||
ChannelSettingValue,
|
ChannelSettingValue,
|
||||||
|
@ -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";
|
||||||
|
|
||||||
interface CachedKickban {
|
interface CachedKickban {
|
||||||
userId: string;
|
userId: string;
|
||||||
|
@ -24,9 +25,12 @@ 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;
|
||||||
|
@ -82,6 +86,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
|
||||||
|
@ -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;
|
if (!msg.message) return;
|
||||||
|
|
||||||
const userFlags = socket.getUserFlags();
|
const userFlags = socket.getUserFlags();
|
||||||
|
@ -125,6 +131,7 @@ 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);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (msg.message.startsWith("/")) {
|
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) {
|
export async function deleteUser(_id: string) {
|
||||||
return await prisma.user.delete({
|
return await prisma.user.delete({
|
||||||
where: { id: _id }
|
where: { id: _id }
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { ChannelList } from "../../channel/ChannelList";
|
import { ChannelList } from "../../channel/ChannelList";
|
||||||
import { deleteUser } from "../../data/user";
|
import { deleteUser, getUsers } from "../../data/user";
|
||||||
import Command from "./Command";
|
import Command from "./Command";
|
||||||
|
|
||||||
Command.addCommand(
|
Command.addCommand(
|
||||||
|
@ -50,12 +50,30 @@ Command.addCommand(
|
||||||
);
|
);
|
||||||
|
|
||||||
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 (
|
return (
|
||||||
"Channels:\n- " +
|
"Channels:\n- " +
|
||||||
ChannelList.getList()
|
ChannelList.getList()
|
||||||
.map(ch => ch.getID())
|
.map(ch => ch.getID())
|
||||||
.join("\n- ")
|
.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