2015-12-16 11:15:43 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
2022-01-19 20:52:12 -05:00
|
|
|
const winston = require('winston');
|
|
|
|
|
|
2023-07-12 13:03:54 -04:00
|
|
|
const batch = require('../batch');
|
|
|
|
|
const db = require('../database');
|
2020-09-28 11:18:43 -04:00
|
|
|
const notifications = require('../notifications');
|
2023-07-12 13:03:54 -04:00
|
|
|
const io = require('../socket.io');
|
2020-09-28 11:18:43 -04:00
|
|
|
const plugins = require('../plugins');
|
|
|
|
|
const meta = require('../meta');
|
2015-12-16 11:15:43 +02:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (Messaging) {
|
2023-07-12 13:03:54 -04:00
|
|
|
// Only used to notify a user of a new chat message
|
|
|
|
|
Messaging.notifyQueue = {};
|
2019-07-22 11:18:13 -04:00
|
|
|
Messaging.notifyUsersInRoom = async (fromUid, roomId, messageObj) => {
|
2023-07-12 13:03:54 -04:00
|
|
|
const isPublic = parseInt(await db.getObjectField(`chat:room:${roomId}`, 'public'), 10) === 1;
|
2017-04-26 10:45:40 -06:00
|
|
|
|
2019-07-22 11:18:13 -04:00
|
|
|
let data = {
|
|
|
|
|
roomId: roomId,
|
|
|
|
|
fromUid: fromUid,
|
|
|
|
|
message: messageObj,
|
2023-07-12 13:03:54 -04:00
|
|
|
public: isPublic,
|
2019-07-22 11:18:13 -04:00
|
|
|
};
|
2020-11-20 16:06:26 -05:00
|
|
|
data = await plugins.hooks.fire('filter:messaging.notify', data);
|
2023-07-12 13:03:54 -04:00
|
|
|
if (!data) {
|
2019-07-22 11:18:13 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2017-04-26 10:45:40 -06:00
|
|
|
|
2023-07-12 13:03:54 -04:00
|
|
|
// delivers full message to all online users in roomId
|
|
|
|
|
io.in(`chat_room_${roomId}`).emit('event:chats.receive', data);
|
|
|
|
|
|
|
|
|
|
const unreadData = { roomId, fromUid, public: isPublic };
|
|
|
|
|
if (isPublic && !messageObj.system) {
|
|
|
|
|
// delivers unread public msg to all online users on the chats page
|
|
|
|
|
io.in(`chat_room_public_${roomId}`).emit('event:chats.public.unread', unreadData);
|
|
|
|
|
}
|
|
|
|
|
if (messageObj.system || isPublic) {
|
2019-10-09 13:29:49 -04:00
|
|
|
return;
|
|
|
|
|
}
|
2023-07-12 13:03:54 -04:00
|
|
|
|
|
|
|
|
// push unread count only for private rooms
|
2023-07-14 11:19:57 -04:00
|
|
|
const uids = await Messaging.getAllUidsInRoomFromSet(`chat:room:${roomId}:uids:online`);
|
2023-07-12 13:03:54 -04:00
|
|
|
Messaging.pushUnreadCount(uids, unreadData);
|
|
|
|
|
|
2019-07-22 11:18:13 -04:00
|
|
|
// Delayed notifications
|
2021-02-03 23:59:08 -07:00
|
|
|
let queueObj = Messaging.notifyQueue[`${fromUid}:${roomId}`];
|
2019-07-22 11:18:13 -04:00
|
|
|
if (queueObj) {
|
2021-02-03 23:59:08 -07:00
|
|
|
queueObj.message.content += `\n${messageObj.content}`;
|
2019-07-22 11:18:13 -04:00
|
|
|
clearTimeout(queueObj.timeout);
|
|
|
|
|
} else {
|
|
|
|
|
queueObj = {
|
|
|
|
|
message: messageObj,
|
|
|
|
|
};
|
2021-02-03 23:59:08 -07:00
|
|
|
Messaging.notifyQueue[`${fromUid}:${roomId}`] = queueObj;
|
2019-07-22 11:18:13 -04:00
|
|
|
}
|
2015-12-16 11:15:43 +02:00
|
|
|
|
2022-01-19 20:52:12 -05:00
|
|
|
queueObj.timeout = setTimeout(async () => {
|
|
|
|
|
try {
|
2023-07-12 13:03:54 -04:00
|
|
|
await sendNotification(fromUid, roomId, queueObj.message);
|
|
|
|
|
delete Messaging.notifyQueue[`${fromUid}:${roomId}`];
|
2022-01-19 20:52:12 -05:00
|
|
|
} catch (err) {
|
|
|
|
|
winston.error(`[messaging/notifications] Unabled to send notification\n${err.stack}`);
|
|
|
|
|
}
|
2021-06-21 11:17:57 -04:00
|
|
|
}, meta.config.notificationSendDelay * 1000);
|
2017-02-22 13:34:57 +03:00
|
|
|
};
|
2015-12-16 11:15:43 +02:00
|
|
|
|
2023-07-12 13:03:54 -04:00
|
|
|
async function sendNotification(fromUid, roomId, messageObj) {
|
2022-01-19 16:19:11 +01:00
|
|
|
const { displayname } = messageObj.fromUser;
|
2020-11-17 12:52:02 -05:00
|
|
|
const isGroupChat = await Messaging.isGroupChat(roomId);
|
2019-07-22 11:18:13 -04:00
|
|
|
const notification = await notifications.create({
|
2020-11-17 12:52:02 -05:00
|
|
|
type: isGroupChat ? 'new-group-chat' : 'new-chat',
|
2022-01-19 16:19:11 +01:00
|
|
|
subject: `[[email:notif.chat.subject, ${displayname}]]`,
|
|
|
|
|
bodyShort: `[[notifications:new_message_from, ${displayname}]]`,
|
2021-02-11 12:55:00 -05:00
|
|
|
bodyLong: messageObj.content,
|
2023-07-12 13:03:54 -04:00
|
|
|
nid: `chat_${fromUid}_${roomId}`,
|
|
|
|
|
from: fromUid,
|
2021-02-03 23:59:08 -07:00
|
|
|
path: `/chats/${messageObj.roomId}`,
|
2015-12-16 11:15:43 +02:00
|
|
|
});
|
2019-07-22 11:18:13 -04:00
|
|
|
|
2023-07-14 11:19:57 -04:00
|
|
|
await batch.processSortedSet(`chat:room:${roomId}:uids:online`, async (uids) => {
|
2023-07-12 13:03:54 -04:00
|
|
|
const hasRead = await Messaging.hasRead(uids, roomId);
|
|
|
|
|
uids = uids.filter((uid, index) => !hasRead[index] && parseInt(fromUid, 10) !== parseInt(uid, 10));
|
|
|
|
|
|
|
|
|
|
notifications.push(notification, uids);
|
|
|
|
|
}, {
|
2023-07-20 22:26:55 -04:00
|
|
|
reverse: true,
|
2023-07-12 13:03:54 -04:00
|
|
|
batch: 500,
|
|
|
|
|
interval: 1000,
|
|
|
|
|
});
|
2015-12-16 11:15:43 +02:00
|
|
|
}
|
2017-02-18 02:30:48 -07:00
|
|
|
};
|