feat: add option to toggle chat join/leave message

closes #13508
This commit is contained in:
Barış Soner Uşaklı
2025-06-27 14:18:53 -04:00
parent a41d2c0b1a
commit 92a3859f7b
9 changed files with 62 additions and 16 deletions

View File

@@ -48,6 +48,7 @@
"chat.add-user": "Add User",
"chat.notification-settings": "Notification Settings",
"chat.default-notification-setting": "Default Notification Setting",
"chat.join-leave-messages": "Join/Leave Messages",
"chat.notification-setting-room-default": "Room Default",
"chat.notification-setting-none": "No notifications",
"chat.notification-setting-at-mention-only": "@mention only",

View File

@@ -27,6 +27,10 @@ RoomObject:
description: Timestamp of when room was created
notificationSetting:
type: number
description: The notification setting for the room, 0 = no notifications, 1 = only mentions, 2 = all messages
joinLeaveMessages:
type: number
description: Whether join/leave messages are enabled in the room
MessageObject:
type: object
properties:

View File

@@ -56,6 +56,8 @@ get:
type: array
notificationOptionsIcon:
type: string
joinLeaveMessages:
type: number
messages:
type: array
items:
@@ -360,6 +362,8 @@ get:
type: string
notificationSetting:
type: number
joinLeaveMessages:
type: number
publicRooms:
type: array
items:

View File

@@ -49,7 +49,7 @@ put:
type: number
required: true
description: a valid message id
example: 5
example: 3
requestBody:
required: true
content:
@@ -92,7 +92,7 @@ delete:
type: number
required: true
description: a valid message id
example: 5
example: 3
responses:
'200':
description: Message successfully deleted
@@ -125,7 +125,7 @@ post:
type: number
required: true
description: a valid message id
example: 5
example: 3
responses:
'200':
description: message successfully restored

View File

@@ -17,7 +17,7 @@ get:
type: string
required: true
description: a valid chat message id
example: 5
example: 3
responses:
'200':
description: Chat message ip address retrieved

View File

@@ -75,12 +75,16 @@ define('forum/chats/manage', [
modal.find('[component="chat/manage/save"]').on('click', () => {
const notifSettingEl = modal.find('[component="chat/room/notification/setting"]');
const joinLeaveMessagesEl = modal.find('[component="chat/room/join-leave-messages"]');
api.put(`/chats/${roomId}`, {
groups: modal.find('[component="chat/room/groups"]').val(),
notificationSetting: notifSettingEl.val(),
joinLeaveMessages: joinLeaveMessagesEl.is(':checked') ? 1 : 0,
}).then((payload) => {
ajaxify.data.groups = payload.groups;
ajaxify.data.notificationSetting = payload.notificationSetting;
ajaxify.data.joinLeaveMessages = payload.joinLeaveMessages;
const roomDefaultOption = payload.notificationOptions[0];
$('[component="chat/notification/setting"] [data-icon]').first().attr(
'data-icon', roomDefaultOption.icon

View File

@@ -162,9 +162,17 @@ chatsAPI.update = async (caller, data) => {
await db.setObjectField(`chat:room:${data.roomId}`, 'groups', JSON.stringify(data.groups));
}
}
if (data.hasOwnProperty('notificationSetting') && isAdmin) {
await db.setObjectField(`chat:room:${data.roomId}`, 'notificationSetting', data.notificationSetting);
if (isAdmin) {
const updateData = {};
if (data.hasOwnProperty('notificationSetting')) {
updateData.notificationSetting = data.notificationSetting;
}
if (data.hasOwnProperty('joinLeaveMessages')) {
updateData.joinLeaveMessages = data.joinLeaveMessages;
}
await db.setObject(`chat:room:${data.roomId}`, updateData);
}
const loadedRoom = await messaging.loadRoom(caller.uid, {
roomId: data.roomId,
});

View File

@@ -22,7 +22,7 @@ const roomUidCache = cacheCreate({
});
const intFields = [
'roomId', 'timestamp', 'userCount', 'messageCount',
'roomId', 'timestamp', 'userCount', 'messageCount', 'joinLeaveMessages',
];
module.exports = function (Messaging) {
@@ -88,6 +88,7 @@ module.exports = function (Messaging) {
timestamp: now,
notificationSetting: data.notificationSetting,
messageCount: 0,
joinLeaveMessages: 0,
};
if (data.hasOwnProperty('roomName') && data.roomName) {
@@ -280,12 +281,22 @@ module.exports = function (Messaging) {
async function addUidsToRoom(uids, roomId) {
const now = Date.now();
const timestamps = uids.map(() => now);
await Promise.all([
db.sortedSetAdd(`chat:room:${roomId}:uids`, timestamps, uids),
db.sortedSetAdd(`chat:room:${roomId}:uids:online`, timestamps, uids),
]);
await updateUserCount([roomId]);
await Promise.all(uids.map(uid => Messaging.addSystemMessage('user-join', uid, roomId)));
if (await joinLeaveMessagesEnabled(roomId)) {
await Promise.all(
uids.map(uid => Messaging.addSystemMessage('user-join', uid, roomId))
);
}
}
async function joinLeaveMessagesEnabled(roomId) {
const roomData = await Messaging.getRoomData(roomId, ['joinLeaveMessages']);
return roomData && roomData.joinLeaveMessages === 1;
}
Messaging.removeUsersFromRoom = async (uid, uids, roomId) => {
@@ -319,7 +330,9 @@ module.exports = function (Messaging) {
}
Messaging.leaveRoom = async (uids, roomId) => {
const isInRoom = await Promise.all(uids.map(uid => Messaging.isUserInRoom(uid, roomId)));
const isInRoom = await Promise.all(
uids.map(uid => Messaging.isUserInRoom(uid, roomId))
);
uids = uids.filter((uid, index) => isInRoom[index]);
const keys = uids
@@ -334,8 +347,11 @@ module.exports = function (Messaging) {
], uids),
db.sortedSetsRemove(keys, roomId),
]);
await Promise.all(uids.map(uid => Messaging.addSystemMessage('user-leave', uid, roomId)));
if (await joinLeaveMessagesEnabled(roomId)) {
await Promise.all(
uids.map(uid => Messaging.addSystemMessage('user-leave', uid, roomId))
);
}
await updateOwner(roomId);
await updateUserCount([roomId]);
};
@@ -357,10 +373,13 @@ module.exports = function (Messaging) {
], roomIds),
]);
await Promise.all(
roomIds.map(roomId => updateOwner(roomId))
.concat(roomIds.map(roomId => Messaging.addSystemMessage('user-leave', uid, roomId)))
);
await Promise.all(roomIds.map(async (roomId) => {
await updateOwner(roomId);
if (await joinLeaveMessagesEnabled(roomId)) {
await Messaging.addSystemMessage('user-leave', uid, roomId);
}
}));
await updateUserCount(roomIds);
};

View File

@@ -1,6 +1,6 @@
<div class="">
{{{ if user.isAdmin }}}
<div class="d-flex gap-2 mb-3 align-items-center justify-content-between">
<div class="d-flex gap-2 mb-3 align-items-center gap-2">
<label class="form-label text-nowrap mb-0">[[modules:chat.default-notification-setting]]</label>
<select component="chat/room/notification/setting" class="form-select" style="width: 200px;">
<option value="1" {{{ if (room.notificationSetting == "1") }}}selected{{{ end }}}>[[modules:chat.notification-setting-none]]</option>
@@ -8,6 +8,12 @@
<option value="3" {{{ if (room.notificationSetting == "3") }}}selected{{{ end }}}>[[modules:chat.notification-setting-all-messages]]</option>
</select>
</div>
<div class="mb-3 d-flex gap-2 align-items-center">
<label class="form-label">[[modules:chat.join-leave-messages]]</label>
<div class="form-check form-switch">
<input component="chat/room/join-leave-messages" class="form-check-input" type="checkbox" {{{ if room.joinLeaveMessages }}}checked{{{ end }}} />
</div>
</div>
<hr/>
{{{ end }}}