Update tags when changed through message

This commit is contained in:
Hri7566 2024-10-26 20:28:53 -04:00
parent f2687db64c
commit 12bb3321c3
8 changed files with 60 additions and 26 deletions

View File

@ -1049,7 +1049,7 @@ export class Channel extends EventEmitter {
* @param part Participant to give crown to (or undefined to drop crown) * @param part Participant to give crown to (or undefined to drop crown)
*/ */
public chown(part?: IParticipant, admin = false) { public chown(part?: IParticipant, admin = false) {
this.logger.debug("chown called"); // this.logger.debug("chown called");
if (part) { if (part) {
this.giveCrown(part, admin); this.giveCrown(part, admin);
} else { } else {

View File

@ -83,5 +83,11 @@ export function loadBehaviors() {
} }
}); });
bus.on("user data update", user => {
for (const ch of ChannelList.getList()) {
ch.emit("user data update", user);
}
});
bus.emit("ready"); bus.emit("ready");
} }

View File

@ -3,6 +3,7 @@ import { Tag } from "./types";
import { User } from "@prisma/client"; import { User } from "@prisma/client";
import { readUser, updateUser } from "~/data/user"; import { readUser, updateUser } from "~/data/user";
import { ChannelList } from "~/channel/ChannelList"; import { ChannelList } from "~/channel/ChannelList";
import { bus } from "~/event/bus";
export const builtinTags = ConfigManager.loadConfig<Record<string, Tag>>( export const builtinTags = ConfigManager.loadConfig<Record<string, Tag>>(
"config/tags.yml", "config/tags.yml",
@ -35,11 +36,7 @@ export const builtinTags = ConfigManager.loadConfig<Record<string, Tag>>(
); );
function propogateUser(user: User) { function propogateUser(user: User) {
const channelList = ChannelList.getList(); bus.emit("user data update", user);
for (const ch of channelList) {
ch.emit("user data update", user);
}
} }
export async function setBuiltinTag(userId: string, tagId: string) { export async function setBuiltinTag(userId: string, tagId: string) {

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

@ -328,6 +328,11 @@ declare interface IncomingSocketEvents {
m: "eval"; m: "eval";
str: string; str: string;
}; };
remove_tag: {
m: "remove_tag";
_id: string;
};
} }
declare interface OutgoingSocketEvents { declare interface OutgoingSocketEvents {

View File

@ -43,7 +43,7 @@ import {
validatePermission validatePermission
} from "~/data/permissions"; } from "~/data/permissions";
import { getRoles } from "~/data/role"; import { getRoles } from "~/data/role";
import { setTag } from "~/util/tags"; import { removeTag, setTag } from "~/util/tags";
import { bus } from "~/event/bus"; import { bus } from "~/event/bus";
import { notificationConfig } from "~/util/notificationConfig"; import { notificationConfig } from "~/util/notificationConfig";
@ -812,10 +812,18 @@ export class Socket extends EventEmitter {
* @param text Text of the tag * @param text Text of the tag
* @param color Color of the tag * @param color Color of the tag
**/ **/
public setTag(text: string, color: string) { public async setTag(text: string, color: string) {
//logger.debug("Setting tag:", text, color); //logger.debug("Setting tag:", text, color);
if (!this.user) return; if (!this.user) return;
setTag(this.user.id, { text, color }); await setTag(this.user.id, { text, color });
}
/**
* Remove this socket's user's tag
*/
public async removeTag() {
if (!this.user) return;
await removeTag(this.user.id);
} }
/** /**

View File

@ -0,0 +1,21 @@
import { ChannelList } from "~/channel/ChannelList";
import { readUser, updateUser } from "~/data/user";
import { removeTag } from "~/util/tags";
import { ServerEventListener } from "~/util/types";
import { findSocketsByUserID } from "~/ws/Socket";
export const remove_tag: ServerEventListener<"remove_tag"> = {
id: "remove_tag",
callback: async (msg, socket) => {
// Change someone else's tag
const id = msg._id;
if (typeof id !== "string") return;
// Check if user exists
const user = await readUser(msg._id);
if (!user) return;
await removeTag(msg._id);
}
};

View File

@ -1,34 +1,29 @@
import { ChannelList } from "~/channel/ChannelList"; import { readUser } from "~/data/user";
import { readUser, updateUser } from "~/data/user"; import { Logger } from "~/util/Logger";
import { setTag } from "~/util/tags";
import { ServerEventListener } from "~/util/types"; import { ServerEventListener } from "~/util/types";
import { findSocketsByUserID } from "~/ws/Socket";
const logger = new Logger("tag");
export const tag: ServerEventListener<"tag"> = { export const tag: ServerEventListener<"tag"> = {
id: "tag", id: "tag",
callback: async (msg, socket) => { callback: async (msg, socket) => {
// Change someone else's tag // Remove someone else's tag
const id = msg._id; const id = msg._id;
const tag = msg.tag; const tag = msg.tag;
if (typeof id !== "string") return; if (typeof id !== "string") return;
// Validate tag info
if (typeof tag !== "object") return; if (typeof tag !== "object") return;
if (typeof tag["text"] !== "string") return; if (typeof tag.text !== "string") return;
if (typeof tag.color !== "string") return; if (typeof tag.color !== "string") return;
if (!tag.color.match(/^#[0-9a-f]{6}$/i)) return; //if (!tag.color.match(/^#[0-9a-f]{6}$/i)) return;
const user = await readUser(msg._id); const user = await readUser(msg._id);
if (!user) return; if (!user) return;
user.tag = JSON.stringify(tag); user.tag = JSON.stringify(tag);
await updateUser(id, user); await setTag(id, tag);
const toUpdate = findSocketsByUserID(id);
toUpdate.forEach(s => {
s.setTag(msg.tag.text, msg.tag.color);
});
for (const ch of ChannelList.getList()) {
ch.emit("user data update", user);
}
} }
}; };

View File

@ -11,6 +11,7 @@ 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 { remove_tag } from "./handlers/remove_tag";
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 { setcolor } from "./handlers/setcolor"; import { setcolor } from "./handlers/setcolor";
@ -39,7 +40,8 @@ EVENT_GROUP_ADMIN.addMany(
forceload, forceload,
unforceload, unforceload,
setcolor, setcolor,
setname setname,
remove_tag
); );
eventGroups.push(EVENT_GROUP_ADMIN); eventGroups.push(EVENT_GROUP_ADMIN);