mpp-server-dev2/oldsrc/Notification.js

86 lines
2.6 KiB
JavaScript
Raw Normal View History

2023-04-01 07:05:12 +02:00
const Server = require("./Server");
2021-05-28 23:15:00 +02:00
module.exports = class Notification {
2023-04-01 07:05:12 +02:00
constructor(server, data) {
this.server = server;
this.cl = data.cl;
2021-05-28 23:15:00 +02:00
this.id = data.id;
this.title = data.title;
this.text = data.text;
this.html = data.html;
this.target = data.target;
this.duration = data.duration;
this.class = data.class;
2023-04-01 07:05:12 +02:00
this.targetChannel = data.targetChannel;
this.targetUser = data.targetUser;
this.chat = data.chat;
2021-05-28 23:15:00 +02:00
}
2023-04-01 07:05:12 +02:00
send() {
let msg = {
m: "notification",
id: this.id,
title: this.title,
text: this.text,
html: this.html,
target: this.target,
duration: this.duration,
class: this.class
};
2021-05-28 23:15:00 +02:00
2023-04-01 07:05:12 +02:00
// Object.assign(msg, this);
const targets = [];
if (this.targetChannel) {
switch (this.targetChannel) {
case "all":
// every channel
for (const cl of this.server.connections.values()) {
targets.push(cl);
}
break;
case "room":
case "channel":
// current channel
if (!this.cl) break;
if (!this.cl.channel) break;
for (const cl of this.server.connections.values()) {
if (!cl.channel) continue;
if (cl.channel._id == this.cl.channel._id) {
targets.push(cl);
}
}
break;
default:
// specific channel
for (const cl of this.server.connections.values()) {
if (!cl.channel) continue;
if (cl.channel._id == this.targetChannel) {
targets.push(cl);
}
}
break;
}
}
if (!this.chat) {
for (const cl of targets) {
if (this.targetUser) {
if (!cl.user) continue;
if (
cl.user._id == this.targetUser ||
cl.participantId == this.targetUser
2023-03-31 16:43:24 +02:00
)
2023-04-01 07:05:12 +02:00
cl.sendArray([msg]);
} else {
cl.sendArray([msg]);
}
}
} else {
this.cl.sendChat(this.text);
2021-05-28 23:15:00 +02:00
}
}
2023-03-31 16:43:24 +02:00
};