Add move and rename_channel admin messages

This commit is contained in:
Hri7566 2024-07-13 02:34:31 -04:00
parent 2347b75f78
commit c119229cb9
7 changed files with 99 additions and 2 deletions

View File

@ -24,3 +24,4 @@ lobbyBackdoor: lolwutsecretlobbybackdoor
fullChannel: test/awkward fullChannel: test/awkward
sendLimit: false sendLimit: false
sendTags: false sendTags: false
chownOnRejoin: true

2
public

@ -1 +1 @@
Subproject commit c15dee967b29a6c8ffb6fa1fd605c357adc33a5d Subproject commit ccc02f653a3eebeb2d64f894b978cc78a5c62c42

View File

@ -290,6 +290,15 @@ export class Channel extends EventEmitter {
return this._id; 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) * Determine whether this channel is a lobby (uses regex from config)
* @returns Boolean * @returns Boolean

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

@ -266,6 +266,18 @@ declare interface ServerEvents {
key: string; key: string;
value: any; value: any;
}; };
move: {
m: "move",
ch: string;
_id?: string;
set?: Partial<IChannelSettings>;
}
rename_channel: {
m: "rename_channel";
_id: string;
}
} }
declare interface ClientEvents { declare interface ClientEvents {

View File

@ -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);
}
}
}
};

View File

@ -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);
}
}
};

View File

@ -4,8 +4,10 @@ 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 { 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 { restart } from "./handlers/restart"; import { restart } from "./handlers/restart";
import { user_flag } from "./handlers/user_flag"; 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(name);
// EVENT_GROUP_ADMIN.add(user_flag); // 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); eventGroups.push(EVENT_GROUP_ADMIN);