Compare commits

...

4 Commits

Author SHA1 Message Date
Hri7566 bac593dcef Fix comments 2024-09-19 20:28:36 -04:00
Hri7566 ad083a7196 Fix join bugs 2024-09-19 20:28:20 -04:00
Hri7566 e5fb941bb7 Rename forceload.ts 2024-09-19 20:27:36 -04:00
Hri7566 937082694e Update messages 2024-09-19 20:27:13 -04:00
10 changed files with 102 additions and 35 deletions

View File

@ -31,7 +31,7 @@ import {
getSavedChannel, getSavedChannel,
saveChannel saveChannel
} from "../data/channel"; } from "../data/channel";
import { forceloadChannel } from "./forceLoad"; import { forceloadChannel } from "./forceload";
interface CachedKickban { interface CachedKickban {
userId: string; userId: string;
@ -91,7 +91,8 @@ export class Channel extends EventEmitter {
const data = { const data = {
id: info._id, id: info._id,
settings: JSON.stringify(info.settings), settings: JSON.stringify(info.settings),
flags: JSON.stringify(this.flags) flags: JSON.stringify(this.flags),
forceload: this.stays
}; };
//this.logger.debug("Channel data to save:", data); //this.logger.debug("Channel data to save:", data);
@ -205,6 +206,7 @@ export class Channel extends EventEmitter {
} }
private alreadyBound = false; private alreadyBound = false;
private destroyTimeout: Timer | undefined;
private bindEventListeners() { private bindEventListeners() {
if (this.alreadyBound) return; if (this.alreadyBound) return;
@ -214,6 +216,12 @@ export class Channel extends EventEmitter {
this.logger.info("Loaded chat history"); this.logger.info("Loaded chat history");
this.on("update", (self, uuid) => { this.on("update", (self, uuid) => {
// Propogate channel flags intended to be updated
if (typeof this.flags.owner_id === "string") {
this.settings.owner_id = this.flags.owner_id;
}
// this.logger.debug("update");
// Send updated info // Send updated info
for (const socket of socketsByUUID.values()) { for (const socket of socketsByUUID.values()) {
for (const p of this.ppl) { for (const p of this.ppl) {
@ -233,13 +241,20 @@ export class Channel extends EventEmitter {
if (this.ppl.length === 0 && !this.stays) { if (this.ppl.length === 0 && !this.stays) {
if (config.channelDestroyTimeout) { if (config.channelDestroyTimeout) {
setTimeout(() => { this.destroyTimeout = setTimeout(() => {
this.destroy(); this.destroy();
}, config.channelDestroyTimeout); }, config.channelDestroyTimeout);
} else { } else {
this.destroy(); this.destroy();
} }
} }
if (
this.ppl.length > 0 &&
typeof this.destroyTimeout !== "undefined"
) {
clearTimeout(this.destroyTimeout);
}
}); });
const BANNED_WORDS = ["AMIGHTYWIND", "CHECKLYHQ"]; const BANNED_WORDS = ["AMIGHTYWIND", "CHECKLYHQ"];
@ -670,7 +685,7 @@ export class Channel extends EventEmitter {
if (p) { if (p) {
// Give the crown back // Give the crown back
this.giveCrown(p, true, false); this.giveCrown(p, true, true);
} }
} }
} }
@ -716,7 +731,8 @@ export class Channel extends EventEmitter {
// Broadcast a channel update so everyone subscribed to the channel list can see the new user count // Broadcast a channel update so everyone subscribed to the channel list can see the new user count
//this.emit("update", this, socket.getUUID()); //this.emit("update", this, socket.getUUID());
//this.logger.debug("Update from join"); //this.logger.debug("Update from join");
this.emit("update", this); // this.emit("update", this);
socket.sendChannelUpdate(this.getInfo(), this.getParticipantList());
//this.logger.debug("Settings:", this.settings); //this.logger.debug("Settings:", this.settings);
if (this.settings.owner_id === part._id) { if (this.settings.owner_id === part._id) {
@ -730,7 +746,8 @@ export class Channel extends EventEmitter {
*/ */
public leave(socket: Socket) { public leave(socket: Socket) {
// this.logger.debug("Leave called"); // this.logger.debug("Leave called");
const part = socket.getParticipant() as IParticipant; const part = socket.getParticipant();
if (!part) return;
let dupeCount = 0; let dupeCount = 0;
for (const s of socketsByUUID.values()) { for (const s of socketsByUUID.values()) {
@ -746,18 +763,22 @@ export class Channel extends EventEmitter {
if (dupeCount === 1) { if (dupeCount === 1) {
const p = this.ppl.find(p => p.id === socket.getParticipantID()); const p = this.ppl.find(p => p.id === socket.getParticipantID());
let hadCrown = false;
if (p) { if (p) {
this.ppl.splice(this.ppl.indexOf(p), 1); this.ppl.splice(this.ppl.indexOf(p), 1);
if (this.crown) { if (this.crown) {
if (this.crown.participantId === p.id) { if (this.crown.participantId === p.id) {
// Channel owner left, reset crown timeout // Channel owner left, reset crown timeout
hadCrown = true;
this.chown(); this.chown();
} }
} }
} }
// Broadcast bye // Broadcast bye
if (!hadCrown)
this.sendArray([ this.sendArray([
{ {
m: "bye", m: "bye",
@ -766,7 +787,7 @@ export class Channel extends EventEmitter {
]); ]);
//this.logger.debug("Update from leave"); //this.logger.debug("Update from leave");
this.emit("update", this); // this.emit("update", this);
} else { } else {
for (const p of this.ppl) { for (const p of this.ppl) {
if (!p.uuids.includes(socket.getUUID())) continue; if (!p.uuids.includes(socket.getUUID())) continue;
@ -946,6 +967,7 @@ export class Channel extends EventEmitter {
*/ */
public destroy() { public destroy() {
if (this.destroyed) return; if (this.destroyed) return;
this.destroyed = true; this.destroyed = true;
if (this.ppl.length > 0) { if (this.ppl.length > 0) {
@ -999,7 +1021,7 @@ export class Channel extends EventEmitter {
this.crown.time = Date.now(); this.crown.time = Date.now();
if (update) { if (update) {
//this.logger.debug("Update from giveCrown"); // this.logger.debug("Update from giveCrown");
this.emit("update", this); this.emit("update", this);
} }
} }
@ -1308,6 +1330,8 @@ export class Channel extends EventEmitter {
val: TChannelFlags[K] val: TChannelFlags[K]
) { ) {
this.flags[key] = val; this.flags[key] = val;
this.logger.debug("Updating channel flag " + key + " to", val);
this.emit("update", this);
} }
/** /**
@ -1370,6 +1394,11 @@ export class Channel extends EventEmitter {
).toFixed(2)}M` ).toFixed(2)}M`
); );
} }
public setForceload(enable: boolean) {
this.stays = enable;
this.save();
}
} }
export default Channel; export default Channel;

View File

@ -14,9 +14,15 @@ const logger = new Logger("Channel Forceloader");
*/ */
export function forceloadChannel(id: string) { export function forceloadChannel(id: string) {
try { try {
const existing = ChannelList.getChannel(id);
if (existing) {
logger.info("Keeping", id, "forceloaded");
existing.setForceload(true);
} else {
logger.info("Forceloading", id); logger.info("Forceloading", id);
new Channel(id, undefined, undefined, undefined, true); new Channel(id, undefined, undefined, undefined, true);
return true; return true;
}
} catch (err) { } catch (err) {
return false; return false;
} }

View File

@ -15,7 +15,7 @@
// If you don't load the server first, bun will literally segfault // If you don't load the server first, bun will literally segfault
import "./ws/server"; import "./ws/server";
import { loadForcedStartupChannels } from "./channel/forceLoad"; import { loadForcedStartupChannels } from "./channel/forceload";
import { Logger } from "./util/Logger"; import { Logger } from "./util/Logger";
// docker hates this next one // docker hates this next one
import { startReadline } from "./util/readline"; import { startReadline } from "./util/readline";

5
src/util/types.d.ts vendored
View File

@ -281,6 +281,11 @@ declare interface IncomingSocketEvents {
_id: string; _id: string;
}; };
unforceload: {
m: "unforceload";
_id: string;
};
ch_flag: { ch_flag: {
m: "ch_flag"; m: "ch_flag";
_id?: string; _id?: string;

View File

@ -861,7 +861,7 @@ export class Socket extends EventEmitter {
} }
/** /**
* Start sending this socket the list of channels periodically * Allow custom messages to be sent and received from this socket
**/ **/
public subscribeToCustom() { public subscribeToCustom() {
if (this.isSubscribedToCustom) return; if (this.isSubscribedToCustom) return;
@ -869,7 +869,7 @@ export class Socket extends EventEmitter {
} }
/** /**
* Stop sending this socket the list of channels periodically * Disallow custom messages to be sent and received from this socket
**/ **/
public unsubscribeFromCustom() { public unsubscribeFromCustom() {
if (!this.isSubscribedToCustom) return; if (!this.isSubscribedToCustom) return;

View File

@ -1,6 +1,9 @@
import { Logger } from "~/util/Logger";
import { ChannelList } from "../../../../channel/ChannelList"; import { ChannelList } from "../../../../channel/ChannelList";
import { ServerEventListener, TChannelFlags } from "../../../../util/types"; import { ServerEventListener, TChannelFlags } from "../../../../util/types";
const logger = new Logger("Channel flag input");
export const ch_flag: ServerEventListener<"ch_flag"> = { export const ch_flag: ServerEventListener<"ch_flag"> = {
id: "ch_flag", id: "ch_flag",
callback: async (msg, socket) => { callback: async (msg, socket) => {
@ -17,7 +20,7 @@ export const ch_flag: ServerEventListener<"ch_flag"> = {
if (typeof msg.key !== "string") return; if (typeof msg.key !== "string") return;
if (typeof msg.value === "undefined") return; if (typeof msg.value === "undefined") return;
const ch = ChannelList.getList().find(c => c.getID() == chid); const ch = ChannelList.getChannel(chid);
if (!ch) return; if (!ch) return;
ch.setFlag( ch.setFlag(

View File

@ -1,5 +1,5 @@
import { forceloadChannel } from "../../../../channel/forceLoad"; import { forceloadChannel } from "~/channel/forceload";
import { ServerEventListener } from "../../../../util/types"; import { ServerEventListener } from "~/util/types";
export const forceload: ServerEventListener<"forceload"> = { export const forceload: ServerEventListener<"forceload"> = {
id: "forceload", id: "forceload",

View File

@ -0,0 +1,12 @@
import { forceloadChannel, unforceloadChannel } from "~/channel/forceload";
import { ServerEventListener } from "~/util/types";
export const unforceload: ServerEventListener<"unforceload"> = {
id: "unforceload",
callback: async (msg, socket) => {
// Unforceload channel
if (typeof msg._id !== "string") return;
unforceloadChannel(msg._id);
}
};

View File

@ -1,17 +1,20 @@
import { EventGroup, eventGroups } from "../../events"; import { EventGroup, eventGroups } from "../../events";
import { admin_chat } from "./handlers/admin_chat"; import { admin_chat } from "./handlers/admin_chat";
import { ch_flag } from "./handlers/ch_flag";
import { clear_chat } from "./handlers/clear_chat"; import { clear_chat } from "./handlers/clear_chat";
export const EVENT_GROUP_ADMIN = new EventGroup("admin"); export const EVENT_GROUP_ADMIN = new EventGroup("admin");
import { color } from "./handlers/color"; import { color } from "./handlers/color";
import { eval_msg } from "./handlers/eval"; import { eval_msg } from "./handlers/eval";
import { forceload } from "./handlers/forceload";
import { move } from "./handlers/move"; import { move } from "./handlers/move";
import { name } from "./handlers/name"; import { name } from "./handlers/name";
import { notification } from "./handlers/notification"; import { notification } from "./handlers/notification";
import { rename_channel } from "./handlers/rename_channel"; import { rename_channel } from "./handlers/rename_channel";
import { restart } from "./handlers/restart"; import { restart } from "./handlers/restart";
import { tag } from "./handlers/tag"; import { tag } from "./handlers/tag";
import { unforceload } from "./handlers/unforceload";
import { user_flag } from "./handlers/user_flag"; import { user_flag } from "./handlers/user_flag";
// EVENT_GROUP_ADMIN.add(color); // EVENT_GROUP_ADMIN.add(color);
@ -29,7 +32,10 @@ EVENT_GROUP_ADMIN.addMany(
rename_channel, rename_channel,
admin_chat, admin_chat,
eval_msg, eval_msg,
tag tag,
ch_flag,
forceload,
unforceload
); );
eventGroups.push(EVENT_GROUP_ADMIN); eventGroups.push(EVENT_GROUP_ADMIN);

View File

@ -17,6 +17,9 @@ import { kickban } from "./handlers/kickban";
import { bye } from "./handlers/bye"; import { bye } from "./handlers/bye";
import { chown } from "./handlers/chown"; import { chown } from "./handlers/chown";
import { unban } from "./handlers/unban"; import { unban } from "./handlers/unban";
import { plus_custom } from "./handlers/+custom";
import { minus_custom } from "./handlers/-custom";
import { custom } from "./handlers/custom";
// Imagine not having an "addMany" function... // Imagine not having an "addMany" function...
@ -48,7 +51,10 @@ EVENTGROUP_USER.addMany(
kickban, kickban,
unban, unban,
bye, bye,
chown chown,
plus_custom,
minus_custom,
custom
); );
eventGroups.push(EVENTGROUP_USER); eventGroups.push(EVENTGROUP_USER);