From b44ddecdf89cb50982a326086c682b171a79da5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Tue, 17 Nov 2020 12:52:02 -0500 Subject: [PATCH] feat: #8204, separate notification type for group chats --- install/data/defaults.json | 1 + public/language/en-GB/notifications.json | 1 + .../components/schemas/SettingsObj.yaml | 3 +++ src/messaging/notifications.js | 3 ++- src/messaging/rooms.js | 26 ++++++++++++------- src/notifications.js | 1 + 6 files changed, 25 insertions(+), 10 deletions(-) diff --git a/install/data/defaults.json b/install/data/defaults.json index 24e1a4f9fb..811ffa243b 100644 --- a/install/data/defaults.json +++ b/install/data/defaults.json @@ -86,6 +86,7 @@ "notificationType_post-edit": "notification", "notificationType_follow": "notification", "notificationType_new-chat": "notification", + "notificationType_new-group-chat": "notification", "notificationType_group-invite": "notification", "notificationType_group-request-membership": "notification", "notificationType_mention": "notification", diff --git a/public/language/en-GB/notifications.json b/public/language/en-GB/notifications.json index 3b9e656c0c..17d7a23bdf 100644 --- a/public/language/en-GB/notifications.json +++ b/public/language/en-GB/notifications.json @@ -66,6 +66,7 @@ "notificationType_post-edit": "When a post is edited in a topic you are watching", "notificationType_follow": "When someone starts following you", "notificationType_new-chat": "When you receive a chat message", + "notificationType_new-group-chat": "When you receive a group chat message", "notificationType_group-invite": "When you receive a group invite", "notificationType_group-request-membership": "When someone requests to join a group you own", "notificationType_new-register": "When someone gets added to registration queue", diff --git a/public/openapi/components/schemas/SettingsObj.yaml b/public/openapi/components/schemas/SettingsObj.yaml index 9e95fb675c..a0d53b5bcb 100644 --- a/public/openapi/components/schemas/SettingsObj.yaml +++ b/public/openapi/components/schemas/SettingsObj.yaml @@ -58,6 +58,9 @@ Settings: notificationType_new-chat: type: string description: Notification type for new chat messages + notificationType_new-group-chat: + type: string + description: Notification type for new group chat messages notificationType_new-reply: type: string description: Notification type for new topic replies diff --git a/src/messaging/notifications.js b/src/messaging/notifications.js index bb735d4e31..7571187190 100644 --- a/src/messaging/notifications.js +++ b/src/messaging/notifications.js @@ -57,8 +57,9 @@ module.exports = function (Messaging) { return; } + const isGroupChat = await Messaging.isGroupChat(roomId); const notification = await notifications.create({ - type: 'new-chat', + type: isGroupChat ? 'new-group-chat' : 'new-chat', subject: '[[email:notif.chat.subject, ' + messageObj.fromUser.username + ']]', bodyShort: '[[notifications:new_message_from, ' + messageObj.fromUser.username + ']]', bodyLong: messageObj.content, diff --git a/src/messaging/rooms.js b/src/messaging/rooms.js index 0f99028cfe..87582b77d0 100644 --- a/src/messaging/rooms.js +++ b/src/messaging/rooms.js @@ -83,15 +83,7 @@ module.exports = function (Messaging) { } await db.sortedSetAdd('chat:room:' + roomId + ':uids', timestamps, uids); - const [userCount, roomData] = await Promise.all([ - db.sortedSetCard('chat:room:' + roomId + ':uids'), - db.getObject('chat:room:' + roomId), - ]); - - if (!roomData.hasOwnProperty('groupChat') && userCount > 2) { - await db.setObjectField('chat:room:' + roomId, 'groupChat', 1); - } - + await updateGroupChatField([roomId]); await Promise.all(uids.map(uid => Messaging.addSystemMessage('user-join', uid, roomId))); }; @@ -111,6 +103,20 @@ module.exports = function (Messaging) { await Messaging.leaveRoom(uids, roomId); }; + Messaging.isGroupChat = async function (roomId) { + return (await Messaging.getRoomData(roomId)).groupChat; + }; + + async function updateGroupChatField(roomIds) { + const userCounts = await db.sortedSetsCard(roomIds.map(roomId => 'chat:room:' + roomId + ':uids')); + const groupChats = roomIds.filter((roomId, index) => userCounts[index] > 2); + const privateChats = roomIds.filter((roomId, index) => userCounts[index] <= 2); + await Promise.all([ + db.setObjectField(groupChats.map(id => 'chat:room:' + id, 'groupChat', 1)), + db.setObjectField(privateChats.map(id => 'chat:room:' + id, 'groupChat', 0)), + ]); + } + Messaging.leaveRoom = async (uids, roomId) => { const isInRoom = await Promise.all(uids.map(uid => Messaging.isUserInRoom(uid, roomId))); uids = uids.filter((uid, index) => isInRoom[index]); @@ -126,6 +132,7 @@ module.exports = function (Messaging) { await Promise.all(uids.map(uid => Messaging.addSystemMessage('user-leave', uid, roomId))); await updateOwner(roomId); + await updateGroupChatField([roomId]); }; Messaging.leaveRooms = async (uid, roomIds) => { @@ -145,6 +152,7 @@ module.exports = function (Messaging) { roomIds.map(roomId => updateOwner(roomId)) .concat(roomIds.map(roomId => Messaging.addSystemMessage('user-leave', uid, roomId))) ); + await updateGroupChatField(roomIds); }; async function updateOwner(roomId) { diff --git a/src/notifications.js b/src/notifications.js index d3bcaa1ce0..d92aa71e8f 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -25,6 +25,7 @@ Notifications.baseTypes = [ 'notificationType_post-edit', 'notificationType_follow', 'notificationType_new-chat', + 'notificationType_new-group-chat', 'notificationType_group-invite', 'notificationType_group-request-membership', ];