Fix channel sending two update messages on join, delete chat history when room is destroyed
This commit is contained in:
parent
b8174bb571
commit
29c3caea0e
|
@ -17,7 +17,7 @@ import Crown from "./Crown";
|
|||
import { ChannelList } from "./ChannelList";
|
||||
import { config } from "./config";
|
||||
import { config as usersConfig } from "../ws/usersConfig";
|
||||
import { saveChatHistory, getChatHistory } from "../data/history";
|
||||
import { saveChatHistory, getChatHistory, deleteChatHistory } from "../data/history";
|
||||
import { mixin, darken } from "../util/helpers";
|
||||
import { User } from "@prisma/client";
|
||||
import { heapStats } from "bun:jsc";
|
||||
|
@ -42,7 +42,18 @@ export class Channel extends EventEmitter {
|
|||
public chatHistory = new Array<ClientEvents["a"]>();
|
||||
|
||||
private async loadChatHistory() {
|
||||
try {
|
||||
this.chatHistory = await getChatHistory(this.getID());
|
||||
|
||||
this.sendArray([{
|
||||
m: "c",
|
||||
c: this.chatHistory
|
||||
}]);
|
||||
} catch (err) { }
|
||||
}
|
||||
|
||||
private async deleteChatHistory() {
|
||||
await deleteChatHistory(this.getID());
|
||||
}
|
||||
|
||||
public logger: Logger;
|
||||
|
@ -97,10 +108,8 @@ export class Channel extends EventEmitter {
|
|||
|
||||
// ...and, possibly, an owner, too
|
||||
if (creator) {
|
||||
// if (this.crown.canBeSetBy(creator)) {
|
||||
const part = creator.getParticipant();
|
||||
if (part) this.giveCrown(part);
|
||||
// }
|
||||
if (part) this.giveCrown(part, true, false);
|
||||
}
|
||||
} else {
|
||||
this.settings = config.lobbySettings;
|
||||
|
@ -129,14 +138,13 @@ export class Channel extends EventEmitter {
|
|||
this.loadChatHistory();
|
||||
this.logger.info("Loaded chat history");
|
||||
|
||||
this.on("update", () => {
|
||||
//this.logger.debug("-------- UPDATE START --------");
|
||||
this.on("update", (self, uuid) => {
|
||||
// Send updated info
|
||||
for (const socket of socketsBySocketID.values()) {
|
||||
for (const p of this.ppl) {
|
||||
//if (socket.getParticipantID() == p.id) {
|
||||
if (p.uuids.includes(socket.getUUID())) {
|
||||
//this.logger.debug("sending to", socket.getUUID())
|
||||
const socketUUID = socket.getUUID();
|
||||
|
||||
if (p.uuids.includes(socketUUID) && socketUUID !== uuid) {
|
||||
socket.sendChannelUpdate(
|
||||
this.getInfo(),
|
||||
this.getParticipantList()
|
||||
|
@ -276,6 +284,7 @@ export class Channel extends EventEmitter {
|
|||
);
|
||||
}
|
||||
|
||||
//this.logger.debug("Update from user data update handler");
|
||||
this.emit("update", this);
|
||||
} catch (err) {
|
||||
this.logger.error(err);
|
||||
|
@ -328,6 +337,7 @@ export class Channel extends EventEmitter {
|
|||
// probably causes jank, but people can just reload their page or whatever
|
||||
// not sure what to do about the URL situation
|
||||
this._id = _id;
|
||||
//this.logger.debug("Update from setID");
|
||||
this.emit("update", this);
|
||||
}
|
||||
|
||||
|
@ -407,6 +417,7 @@ export class Channel extends EventEmitter {
|
|||
}
|
||||
*/
|
||||
|
||||
//this.logger.debug("Update from changeSettings");
|
||||
this.emit("update", this);
|
||||
}
|
||||
|
||||
|
@ -486,7 +497,7 @@ export class Channel extends EventEmitter {
|
|||
p.uuids.push(socket.getUUID())
|
||||
}
|
||||
|
||||
socket.sendChannelUpdate(this.getInfo(), this.getParticipantList());
|
||||
//socket.sendChannelUpdate(this.getInfo(), this.getParticipantList());
|
||||
} else {
|
||||
// Add them to the channel
|
||||
hasChangedChannel = true;
|
||||
|
@ -534,15 +545,13 @@ export class Channel extends EventEmitter {
|
|||
|
||||
if (p) {
|
||||
// Give the crown back
|
||||
this.giveCrown(p, true);
|
||||
this.giveCrown(p, true, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Send our state data back
|
||||
// TODO Does this go above?
|
||||
socket.sendArray([
|
||||
// {
|
||||
// m: "ch",
|
||||
|
@ -578,7 +587,9 @@ export class Channel extends EventEmitter {
|
|||
part.id
|
||||
);
|
||||
|
||||
// Broadcast a channel update so everyone subscribed to the channel list can see us
|
||||
// Broadcast a channel update so everyone subscribed to the channel list can see the new user count
|
||||
//this.emit("update", this, socket.getUUID());
|
||||
//this.logger.debug("Update from join");
|
||||
this.emit("update", this);
|
||||
|
||||
//this.logger.debug("Settings:", this.settings);
|
||||
|
@ -625,6 +636,7 @@ export class Channel extends EventEmitter {
|
|||
}
|
||||
]);
|
||||
|
||||
//this.logger.debug("Update from leave");
|
||||
this.emit("update", this);
|
||||
} else {
|
||||
for (const p of this.ppl) {
|
||||
|
@ -789,6 +801,7 @@ export class Channel extends EventEmitter {
|
|||
}
|
||||
|
||||
ChannelList.remove(this);
|
||||
this.deleteChatHistory();
|
||||
this.logger.info("Destroyed");
|
||||
}
|
||||
|
||||
|
@ -819,7 +832,7 @@ export class Channel extends EventEmitter {
|
|||
* @param part Participant to give crown to
|
||||
* @param force Whether or not to force-create a crown (useful for lobbies)
|
||||
*/
|
||||
public giveCrown(part: Participant, force = false) {
|
||||
public giveCrown(part: Participant, force = false, update = true) {
|
||||
if (force) {
|
||||
if (!this.crown) this.crown = new Crown();
|
||||
}
|
||||
|
@ -828,9 +841,13 @@ export class Channel extends EventEmitter {
|
|||
this.crown.userId = part._id;
|
||||
this.crown.participantId = part.id;
|
||||
this.crown.time = Date.now();
|
||||
|
||||
if (update) {
|
||||
//this.logger.debug("Update from giveCrown");
|
||||
this.emit("update", this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop the crown (reset timer, and, if applicable, remove from user's head)
|
||||
|
@ -865,6 +882,7 @@ export class Channel extends EventEmitter {
|
|||
|
||||
delete this.crown.participantId;
|
||||
|
||||
//this.logger.debug("Update from dropCrown");
|
||||
this.emit("update", this);
|
||||
}
|
||||
}
|
||||
|
@ -951,6 +969,7 @@ export class Channel extends EventEmitter {
|
|||
}
|
||||
|
||||
if (shouldUpdate) {
|
||||
//this.logger.debug("Update from kickban");
|
||||
this.emit("update", this);
|
||||
|
||||
if (typeof banner !== "undefined") {
|
||||
|
@ -1149,7 +1168,7 @@ export class Channel extends EventEmitter {
|
|||
|
||||
public printMemoryInChat() {
|
||||
const mem = heapStats();
|
||||
this.sendChatAdmin(`Size: ${(mem.heapSize / 1000 / 1000).toFixed(2)}M / Capacity: ${(mem.heapCapacity / 1000 / 1000).toFixed(2)}M`);
|
||||
this.sendChatAdmin(`Used: ${(mem.heapSize / 1000 / 1000).toFixed(2)}M / Allocated: ${(mem.heapCapacity / 1000 / 1000).toFixed(2)}M`);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -27,3 +27,7 @@ export async function getChatHistory(_id: string) {
|
|||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteChatHistory(_id: string) {
|
||||
await prisma.chatHistory.delete({ where: { id: _id } });
|
||||
}
|
||||
|
|
|
@ -50,7 +50,7 @@ export async function createToken(userID: string, gateway: Gateway) {
|
|||
let token = "";
|
||||
|
||||
if (config.tokenAuth == "uuid") {
|
||||
token = crypto.randomUUID();
|
||||
token = userID + "." + crypto.randomUUID();
|
||||
} else if (config.tokenAuth == "jwt") {
|
||||
token = generateJWT(userID, gateway);
|
||||
}
|
||||
|
|
|
@ -32,6 +32,7 @@ export const hi: ServerEventListener<"hi"> = {
|
|||
let token: string | undefined;
|
||||
let generatedToken = false;
|
||||
|
||||
if (config.tokenAuth !== "none") {
|
||||
if (typeof msg.token !== "string") {
|
||||
// Get a saved token
|
||||
token = await getToken(socket.getUserID());
|
||||
|
@ -55,6 +56,7 @@ export const hi: ServerEventListener<"hi"> = {
|
|||
|
||||
token = msg.token;
|
||||
}
|
||||
}
|
||||
|
||||
let part = socket.getParticipant();
|
||||
|
||||
|
|
|
@ -0,0 +1,243 @@
|
|||
[1mdiff --git a/src/channel/Channel.ts b/src/channel/Channel.ts[m
|
||||
[1mindex e33ddc6..b6072be 100755[m
|
||||
[1m--- a/src/channel/Channel.ts[m
|
||||
[1m+++ b/src/channel/Channel.ts[m
|
||||
[36m@@ -17,7 +17,7 @@[m [mimport Crown from "./Crown";[m
|
||||
import { ChannelList } from "./ChannelList";[m
|
||||
import { config } from "./config";[m
|
||||
import { config as usersConfig } from "../ws/usersConfig";[m
|
||||
import { saveChatHistory, [31m[-getChatHistory-][m[32m{+getChatHistory, deleteChatHistory+}[m } from "../data/history";
|
||||
import { mixin, darken } from "../util/helpers";[m
|
||||
import { User } from "@prisma/client";[m
|
||||
import { heapStats } from "bun:jsc";[m
|
||||
[36m@@ -42,7 +42,18 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
public chatHistory = new Array<ClientEvents["a"]>();[m
|
||||
|
||||
private async loadChatHistory() {[m
|
||||
[32m{+try {+}[m
|
||||
this.chatHistory = await getChatHistory(this.getID());
|
||||
|
||||
[32m{+this.sendArray([{+}[m
|
||||
[32m{+ m: "c",+}[m
|
||||
[32m{+ c: this.chatHistory+}[m
|
||||
[32m{+ }]);+}[m
|
||||
[32m{+ } catch (err) { }+}[m
|
||||
[32m{+ }+}[m
|
||||
|
||||
[32m{+ private async deleteChatHistory() {+}[m
|
||||
[32m{+ await deleteChatHistory(this.getID());+}[m
|
||||
}[m
|
||||
|
||||
public logger: Logger;[m
|
||||
[36m@@ -97,10 +108,8 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
|
||||
// ...and, possibly, an owner, too[m
|
||||
if (creator) {[m
|
||||
[31m[- // if (this.crown.canBeSetBy(creator)) {-][m
|
||||
const part = creator.getParticipant();[m
|
||||
if (part) [31m[-this.giveCrown(part);-][m
|
||||
[31m[- // }-][m[32m{+this.giveCrown(part, true, false);+}[m
|
||||
}[m
|
||||
} else {[m
|
||||
this.settings = config.lobbySettings;[m
|
||||
[36m@@ -129,14 +138,13 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
this.loadChatHistory();[m
|
||||
this.logger.info("Loaded chat history");[m
|
||||
|
||||
this.on("update", [31m[-()-][m[32m{+(self, uuid)+}[m => {[31m[-//this.logger.debug("-------- UPDATE START --------");-][m
|
||||
// Send updated info[m
|
||||
for (const socket of socketsBySocketID.values()) {[m
|
||||
for (const p of this.ppl) {[m
|
||||
[31m[-//if (socket.getParticipantID() == p.id) {-][m[32m{+const socketUUID = socket.getUUID();+}[m
|
||||
|
||||
if [31m[-(p.uuids.includes(socket.getUUID()))-][m[32m{+(p.uuids.includes(socketUUID) && socketUUID !== uuid)+}[m {[31m[-//this.logger.debug("sending to", socket.getUUID())-][m
|
||||
socket.sendChannelUpdate([m
|
||||
this.getInfo(),[m
|
||||
this.getParticipantList()[m
|
||||
[36m@@ -276,6 +284,7 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
);[m
|
||||
}[m
|
||||
|
||||
[32m{+this.logger.debug("Update from user data update handler");+}[m
|
||||
this.emit("update", this);[m
|
||||
} catch (err) {[m
|
||||
this.logger.error(err);[m
|
||||
[36m@@ -328,6 +337,7 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
// probably causes jank, but people can just reload their page or whatever[m
|
||||
// not sure what to do about the URL situation[m
|
||||
this._id = _id;[m
|
||||
[32m{+this.logger.debug("Update from setID");+}[m
|
||||
this.emit("update", this);[m
|
||||
}[m
|
||||
|
||||
[36m@@ -407,6 +417,7 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
}[m
|
||||
*/[m
|
||||
|
||||
[32m{+this.logger.debug("Update from changeSettings");+}[m
|
||||
this.emit("update", this);[m
|
||||
}[m
|
||||
|
||||
[36m@@ -486,7 +497,7 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
p.uuids.push(socket.getUUID())[m
|
||||
}[m
|
||||
|
||||
[31m[-socket.sendChannelUpdate(this.getInfo(),-][m[32m{+//socket.sendChannelUpdate(this.getInfo(),+}[m this.getParticipantList());
|
||||
} else {[m
|
||||
// Add them to the channel[m
|
||||
hasChangedChannel = true;[m
|
||||
[36m@@ -534,15 +545,13 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
|
||||
if (p) {[m
|
||||
// Give the crown back[m
|
||||
this.giveCrown(p, [31m[-true);-][m[32m{+true, false);+}[m
|
||||
}[m
|
||||
}[m
|
||||
}[m
|
||||
}[m
|
||||
}[m
|
||||
|
||||
[31m[- // Send our state data back-][m
|
||||
[31m[- // TODO Does this go above?-][m
|
||||
socket.sendArray([[m
|
||||
// {[m
|
||||
// m: "ch",[m
|
||||
[36m@@ -578,7 +587,9 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
part.id[m
|
||||
);[m
|
||||
|
||||
// Broadcast a channel update so everyone subscribed to the channel list can see [31m[-us-][m[32m{+the new user count+}[m
|
||||
[32m{+ //this.emit("update", this, socket.getUUID());+}[m
|
||||
[32m{+ this.logger.debug("Update from join");+}[m
|
||||
this.emit("update", this);[m
|
||||
|
||||
//this.logger.debug("Settings:", this.settings);[m
|
||||
[36m@@ -625,6 +636,7 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
}[m
|
||||
]);[m
|
||||
|
||||
[32m{+this.logger.debug("Update from leave");+}[m
|
||||
this.emit("update", this);[m
|
||||
} else {[m
|
||||
for (const p of this.ppl) {[m
|
||||
[36m@@ -789,6 +801,7 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
}[m
|
||||
|
||||
ChannelList.remove(this);[m
|
||||
[32m{+this.deleteChatHistory();+}[m
|
||||
this.logger.info("Destroyed");[m
|
||||
}[m
|
||||
|
||||
[36m@@ -819,7 +832,7 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
* @param part Participant to give crown to[m
|
||||
* @param force Whether or not to force-create a crown (useful for lobbies)[m
|
||||
*/[m
|
||||
public giveCrown(part: Participant, force = [31m[-false)-][m[32m{+false, update = true)+}[m {
|
||||
if (force) {[m
|
||||
if (!this.crown) this.crown = new Crown();[m
|
||||
}[m
|
||||
[36m@@ -828,7 +841,11 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
this.crown.userId = part._id;[m
|
||||
this.crown.participantId = part.id;[m
|
||||
this.crown.time = Date.now();[m
|
||||
|
||||
[32m{+if (update) {+}[m
|
||||
[32m{+ this.logger.debug("Update from giveCrown");+}[m
|
||||
this.emit("update", this);
|
||||
[32m{+}+}[m
|
||||
}[m
|
||||
}[m
|
||||
|
||||
[36m@@ -865,6 +882,7 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
|
||||
delete this.crown.participantId;[m
|
||||
|
||||
[32m{+this.logger.debug("Update from dropCrown");+}[m
|
||||
this.emit("update", this);[m
|
||||
}[m
|
||||
}[m
|
||||
[36m@@ -951,6 +969,7 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
}[m
|
||||
|
||||
if (shouldUpdate) {[m
|
||||
[32m{+this.logger.debug("Update from kickban");+}[m
|
||||
this.emit("update", this);[m
|
||||
|
||||
if (typeof banner !== "undefined") {[m
|
||||
[36m@@ -1149,7 +1168,7 @@[m [mexport class Channel extends EventEmitter {[m
|
||||
|
||||
public printMemoryInChat() {[m
|
||||
const mem = heapStats();[m
|
||||
[31m[-this.sendChatAdmin(`Size:-][m[32m{+this.sendChatAdmin(`Used:+}[m ${(mem.heapSize / 1000 / 1000).toFixed(2)}M / [31m[-Capacity:-][m[32m{+Allocated:+}[m ${(mem.heapCapacity / 1000 / 1000).toFixed(2)}M`);
|
||||
}[m
|
||||
}[m
|
||||
|
||||
[1mdiff --git a/src/data/history.ts b/src/data/history.ts[m
|
||||
[1mindex b769e27..0b68962 100755[m
|
||||
[1m--- a/src/data/history.ts[m
|
||||
[1m+++ b/src/data/history.ts[m
|
||||
[36m@@ -27,3 +27,7 @@[m [mexport async function getChatHistory(_id: string) {[m
|
||||
return [];[m
|
||||
}[m
|
||||
}[m
|
||||
|
||||
[32m{+export async function deleteChatHistory(_id: string) {+}[m
|
||||
[32m{+ await prisma.chatHistory.delete({ where: { id: _id } });+}[m
|
||||
[32m{+}+}[m
|
||||
[1mdiff --git a/src/util/token.ts b/src/util/token.ts[m
|
||||
[1mindex 54d22af..4e7bf6f 100644[m
|
||||
[1m--- a/src/util/token.ts[m
|
||||
[1m+++ b/src/util/token.ts[m
|
||||
[36m@@ -50,7 +50,7 @@[m [mexport async function createToken(userID: string, gateway: Gateway) {[m
|
||||
let token = "";[m
|
||||
|
||||
if (config.tokenAuth == "uuid") {[m
|
||||
token = [32m{+userID + "." ++}[m crypto.randomUUID();
|
||||
} else if (config.tokenAuth == "jwt") {[m
|
||||
token = generateJWT(userID, gateway);[m
|
||||
}[m
|
||||
[1mdiff --git a/src/ws/events/user/handlers/hi.ts b/src/ws/events/user/handlers/hi.ts[m
|
||||
[1mindex 91067b1..964c2cb 100755[m
|
||||
[1m--- a/src/ws/events/user/handlers/hi.ts[m
|
||||
[1m+++ b/src/ws/events/user/handlers/hi.ts[m
|
||||
[36m@@ -32,28 +32,30 @@[m [mexport const hi: ServerEventListener<"hi"> = {[m
|
||||
let token: string | undefined;[m
|
||||
let generatedToken = false;[m
|
||||
|
||||
if [32m{+(config.tokenAuth !== "none") {+}[m
|
||||
[32m{+ if+}[m (typeof msg.token !== "string") {
|
||||
// Get a saved token
|
||||
token = await getToken(socket.getUserID());[31m[-if (typeof token !== "string") {-][m
|
||||
[31m[- // Generate a new one-][m
|
||||
[31m[- token = await createToken(socket.getUserID(), socket.gateway);-][m
|
||||
if (typeof token !== "string") {[m
|
||||
[32m{+// Generate a new one+}[m
|
||||
[32m{+ token = await createToken(socket.getUserID(), socket.gateway);+}[m
|
||||
|
||||
[32m{+ if (typeof token !== "string") {+}[m
|
||||
logger.warn(`Unable to generate token for user ${socket.getUserID()}`);
|
||||
} else {
|
||||
generatedToken = true;
|
||||
[32m{+}+}[m
|
||||
[32m{+ }+}[m
|
||||
[32m{+ } else {+}[m
|
||||
[32m{+ // Validate the token+}[m
|
||||
[32m{+ const valid = await validateToken(socket.getUserID(), msg.token);+}[m
|
||||
[32m{+ if (!valid) {+}[m
|
||||
[32m{+ socket.ban(60000, "Invalid token");+}[m
|
||||
[32m{+ return;+}[m
|
||||
}[m
|
||||
[31m[- }-][m
|
||||
[31m[- } else {-][m
|
||||
[31m[- // Validate the token-][m
|
||||
[31m[- const valid = await validateToken(socket.getUserID(), msg.token);-][m
|
||||
[31m[- if (!valid) {-][m
|
||||
[31m[- socket.ban(60000, "Invalid token");-][m
|
||||
[31m[- return;-][m
|
||||
[31m[- }-][m
|
||||
|
||||
token = msg.token;
|
||||
[32m{+}+}[m
|
||||
}[m
|
||||
|
||||
let part = socket.getParticipant();[m
|
Loading…
Reference in New Issue