From c119229cb9b0249f5e19f854ddaebf5247957e0a Mon Sep 17 00:00:00 2001 From: Hri7566 Date: Sat, 13 Jul 2024 02:34:31 -0400 Subject: [PATCH] Add move and rename_channel admin messages --- config/channels.yml | 1 + public | 2 +- src/channel/Channel.ts | 9 ++++ src/util/types.d.ts | 12 +++++ src/ws/events/admin/handlers/move.ts | 26 ++++++++++ .../events/admin/handlers/rename_channel.ts | 47 +++++++++++++++++++ src/ws/events/admin/index.ts | 4 +- 7 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 src/ws/events/admin/handlers/move.ts create mode 100644 src/ws/events/admin/handlers/rename_channel.ts diff --git a/config/channels.yml b/config/channels.yml index 4764560..e43cc57 100644 --- a/config/channels.yml +++ b/config/channels.yml @@ -24,3 +24,4 @@ lobbyBackdoor: lolwutsecretlobbybackdoor fullChannel: test/awkward sendLimit: false sendTags: false +chownOnRejoin: true diff --git a/public b/public index c15dee9..ccc02f6 160000 --- a/public +++ b/public @@ -1 +1 @@ -Subproject commit c15dee967b29a6c8ffb6fa1fd605c357adc33a5d +Subproject commit ccc02f653a3eebeb2d64f894b978cc78a5c62c42 diff --git a/src/channel/Channel.ts b/src/channel/Channel.ts index cf35bac..c920f5b 100644 --- a/src/channel/Channel.ts +++ b/src/channel/Channel.ts @@ -290,6 +290,15 @@ export class Channel extends EventEmitter { return this._id; } + /** + * Set this channel's ID (channel name) + **/ + public setID(_id: string) { + // probably causes jank + this._id = _id; + this.emit("update", this); + } + /** * Determine whether this channel is a lobby (uses regex from config) * @returns Boolean diff --git a/src/util/types.d.ts b/src/util/types.d.ts index 0f64f3f..f6d11b9 100644 --- a/src/util/types.d.ts +++ b/src/util/types.d.ts @@ -266,6 +266,18 @@ declare interface ServerEvents { key: string; value: any; }; + + move: { + m: "move", + ch: string; + _id?: string; + set?: Partial; + } + + rename_channel: { + m: "rename_channel"; + _id: string; + } } declare interface ClientEvents { diff --git a/src/ws/events/admin/handlers/move.ts b/src/ws/events/admin/handlers/move.ts new file mode 100644 index 0000000..cb46be7 --- /dev/null +++ b/src/ws/events/admin/handlers/move.ts @@ -0,0 +1,26 @@ +import { ServerEventListener } from "../../../../util/types"; +import { socketsBySocketID } from "../../../Socket"; + +export const move: ServerEventListener<"move"> = { + id: "move", + callback: async (msg, socket) => { + // Forcefully move user to another channel + let id = msg._id; + if (!id) id = socket.getUserID(); + + const ch = msg.ch; + const set = msg.set; + + if (typeof ch !== "string") return; + if (typeof set !== "object" && typeof set !== "undefined") return; + + // Loop through every socket + for (const sock of socketsBySocketID.values()) { + // Check their user ID + if (sock.getUserID() == id) { + // Forcefully move to channel + sock.setChannel(ch, set, true); + } + } + } +}; diff --git a/src/ws/events/admin/handlers/rename_channel.ts b/src/ws/events/admin/handlers/rename_channel.ts new file mode 100644 index 0000000..bba7af0 --- /dev/null +++ b/src/ws/events/admin/handlers/rename_channel.ts @@ -0,0 +1,47 @@ +import { ChannelList } from "../../../../channel/ChannelList"; +import { ServerEventListener } from "../../../../util/types"; +import { socketsBySocketID } from "../../../Socket"; + +export const rename_channel: ServerEventListener<"rename_channel"> = { + id: "rename_channel", + callback: async (msg, socket) => { + // Rename a channel by changing its ID + if (typeof msg._id !== "string") return; + + const ch = socket.getCurrentChannel(); + if (!ch) return; + + + const oldID = socket.currentChannelID; + + // We have to check if the desired ID exists already, + // and if it does, we'll merge the channels by forcing + // the users to join the new one, otherwise we'll just + // change the ID of the original channel and push an + // update event to propogate the changes to the clients. + + let found; + + for (const channel of ChannelList.getList()) { + if (msg._id == channel.getID()) { + found = channel; + } + } + + // Have we found a channel? + if (!found) { + // Not found, so it's safe to take up that ID + ch.setID(msg._id); + } else { + // Found, avoid jank by magically disappearing + ch.destroy(); + } + + for (const sock of socketsBySocketID.values()) { + // Are they in this channel? + if (sock.currentChannelID !== oldID) continue; + // Move them forcefully + sock.setChannel(msg._id, undefined, true); + } + } +}; diff --git a/src/ws/events/admin/index.ts b/src/ws/events/admin/index.ts index 599885e..c6af526 100644 --- a/src/ws/events/admin/index.ts +++ b/src/ws/events/admin/index.ts @@ -4,8 +4,10 @@ import { clear_chat } from "./handlers/clear_chat"; export const EVENT_GROUP_ADMIN = new EventGroup("admin"); import { color } from "./handlers/color"; +import { move } from "./handlers/move"; import { name } from "./handlers/name"; import { notification } from "./handlers/notification"; +import { rename_channel } from "./handlers/rename_channel"; import { restart } from "./handlers/restart"; import { user_flag } from "./handlers/user_flag"; @@ -13,6 +15,6 @@ import { user_flag } from "./handlers/user_flag"; // EVENT_GROUP_ADMIN.add(name); // EVENT_GROUP_ADMIN.add(user_flag); -EVENT_GROUP_ADMIN.addMany(color, name, user_flag, clear_chat, notification, restart); +EVENT_GROUP_ADMIN.addMany(color, name, user_flag, clear_chat, notification, restart, move, rename_channel); eventGroups.push(EVENT_GROUP_ADMIN);