Fix chown, add remove parameter to user flags

This commit is contained in:
Hri7566 2024-07-09 10:47:28 -04:00
parent 9d84c5f81b
commit e1937aea02
3 changed files with 17 additions and 11 deletions

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

@ -234,7 +234,8 @@ declare interface ServerEvents {
m: "user_flag";
_id: string;
key: keyof UserFlags;
value: UserFlags[keyof UserFlags];
value?: UserFlags[keyof UserFlags];
remove?: true;
};
clear_chat: {

View File

@ -1,14 +1,19 @@
import { readUser, updateUser } from "../../../../data/user";
import { Logger } from "../../../../util/Logger";
import { ServerEventListener } from "../../../../util/types";
import { findSocketsByUserID } from "../../../Socket";
const logger = new Logger("User flag handler");
export const user_flag: ServerEventListener<"user_flag"> = {
id: "user_flag",
callback: async (msg, socket) => {
// User flag modification (changing some real specific shit)
if (typeof msg._id !== "string") return;
if (typeof msg.key !== "string") return;
if (typeof msg.value == "undefined") return;
if (typeof msg.remove !== "boolean" && typeof msg.value == "undefined") {
return
}
// socket.getCurrentChannel()?.logger.debug(msg);
@ -18,7 +23,11 @@ export const user_flag: ServerEventListener<"user_flag"> = {
// Set the flag
const flags = JSON.parse(user.flags);
flags[msg.key] = msg.value;
if (msg.remove === true) {
delete flags[msg.key];
} else {
flags[msg.key] = msg.value;
}
user.flags = JSON.stringify(flags);
// Save the user data

View File

@ -7,22 +7,18 @@ export const chown: ServerEventListener<"chown"> = {
id: "chown",
callback: (msg, socket) => {
// Change channel ownership
if (typeof msg.id == "undefined") return;
const ch = socket.getCurrentChannel();
if (!ch) return;
if (!socket.isOwner()) return;
if (!ch.crown) {
// TODO Crown admin stuff
} else {
if (!ch.crown.canBeSetBy(socket)) return;
const heir = ch.getParticipantList().find(p => p.id == msg.id);
if (!heir) return;
// This user may not always exist,
// but sometimes we don't provide a user
// to drop the crown
let heir = ch.getParticipantList().find(p => p.id == msg.id);
ch.chown(heir);
}
}