From e67b21dd9f86e539de7eb3f1be4ecc62644f1881 Mon Sep 17 00:00:00 2001 From: John Livingston Date: Thu, 7 Mar 2024 17:33:18 +0100 Subject: [PATCH] Updating slow mode duration on existing rooms when changing channel options (related to #332). --- CHANGELOG.md | 1 + ...mod_http_peertubelivechat_manage_rooms.lua | 6 ++++ server/lib/configuration/channel/init.ts | 4 +-- server/lib/prosody/api/manage-rooms.ts | 16 ++++++---- server/lib/room-channel/room-channel-class.ts | 30 ++++++++++++++++++- 5 files changed, 48 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 710120b9..8a72967f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Fix #87: updating chat room title when video/channel title is changed. * Updating xmppjs-chat-box version. * Translation updates: japanese. +* Updating slow mode duration on existing rooms when changing channel options (related to #332). ## 8.3.2 diff --git a/prosody-modules/mod_http_peertubelivechat_manage_rooms/mod_http_peertubelivechat_manage_rooms.lua b/prosody-modules/mod_http_peertubelivechat_manage_rooms/mod_http_peertubelivechat_manage_rooms.lua index 5e732e64..7cc7ada9 100644 --- a/prosody-modules/mod_http_peertubelivechat_manage_rooms/mod_http_peertubelivechat_manage_rooms.lua +++ b/prosody-modules/mod_http_peertubelivechat_manage_rooms/mod_http_peertubelivechat_manage_rooms.lua @@ -83,6 +83,12 @@ local function update_room(event) must104 = true; end end + if type(config.slow_mode_duration) == "number" then + if room._data.slow_mode_duration ~= config.slow_mode_duration then + room._data.slow_mode_duration = config.slow_mode_duration; + must104 = true; + end + end if must104 then -- we must broadcast a status 104 message, so that clients can update room info diff --git a/server/lib/configuration/channel/init.ts b/server/lib/configuration/channel/init.ts index 9aae761f..7e165619 100644 --- a/server/lib/configuration/channel/init.ts +++ b/server/lib/configuration/channel/init.ts @@ -106,7 +106,7 @@ async function initChannelConfiguration (options: RegisterServerOptions): Promis // FIXME: this piece of code should not be in this file (nothing to do with initChannelConfiguration, // but will be more efficient to add here, as we already tested hasChat). // Note: no need to await here, would only degrade performances. - updateProsodyRoom(options, video.channelId, video.uuid, { + updateProsodyRoom(options, video.uuid, { name: video.name }).then( () => {}, @@ -133,7 +133,7 @@ async function initChannelConfiguration (options: RegisterServerOptions): Promis if (settings['prosody-room-type'] === 'channel') { const jid = 'channel.' + channel.id.toString() // Note: no need to await here, would only degrade performances. - updateProsodyRoom(options, channel.id, jid, { + updateProsodyRoom(options, jid, { name: channel.displayName }).then( () => {}, diff --git a/server/lib/prosody/api/manage-rooms.ts b/server/lib/prosody/api/manage-rooms.ts index 44c14e57..6a4840dc 100644 --- a/server/lib/prosody/api/manage-rooms.ts +++ b/server/lib/prosody/api/manage-rooms.ts @@ -49,17 +49,16 @@ async function listProsodyRooms (options: RegisterServerOptions): Promise { const logger = options.peertubeHelpers.logger @@ -78,8 +77,13 @@ async function updateProsodyRoom ( // Requesting on localhost, because currentProsody.host does not always resolves correctly (docker use case, ...) const apiUrl = `http://localhost:${currentProsody.port}/peertubelivechat_manage_rooms/update-room` const apiData = { - jid, - name: data.name + jid + } as any + if ('name' in data) { + apiData.name = data.name + } + if ('slow_mode_duration' in data) { + apiData.slow_mode_duration = data.slow_mode_duration } try { logger.debug('Calling update room API on url: ' + apiUrl + ', with data: ' + JSON.stringify(apiData)) diff --git a/server/lib/room-channel/room-channel-class.ts b/server/lib/room-channel/room-channel-class.ts index d5f458d6..a5d9615d 100644 --- a/server/lib/room-channel/room-channel-class.ts +++ b/server/lib/room-channel/room-channel-class.ts @@ -1,7 +1,7 @@ import type { RegisterServerOptions } from '@peertube/peertube-types' import type { RoomConf } from 'xmppjs-chat-bot' import { getProsodyDomain } from '../prosody/config/domain' -import { listProsodyRooms } from '../prosody/api/manage-rooms' +import { listProsodyRooms, updateProsodyRoom } from '../prosody/api/manage-rooms' import { getChannelInfosById } from '../database/channel' import { ChannelConfigurationOptions } from '../../../shared/lib/types' import { @@ -287,6 +287,9 @@ class RoomChannel { } this.logger.info('Syncing...') this.isWriting = true + + const prosodyRoomUpdates = new Map[2]>() + try { const data = this._serializeData() // must be atomic this.needSync = false // Note: must be done atomicly with the read @@ -344,6 +347,14 @@ class RoomChannel { ) await BotConfiguration.singleton().updateRoom(roomJID, botConf) + + // Now we also must update some room metadata (slow mode duration, ...) + // This can be done without waiting for the API call to finish, but we don't want to send thousands of + // API calls at the same time. So storing data in a map, and we well launch it sequentially at the end + prosodyRoomUpdates.set(roomJID, { + slow_mode_duration: channelConfigurationOptions.slowMode.defaultDuration + }) + this.roomConfToUpdate.delete(roomJID) } @@ -355,6 +366,23 @@ class RoomChannel { } finally { this.isWriting = false } + + if (prosodyRoomUpdates.size) { + // Here we don't have to wait. + // If it fails (for example because we are turning off prosody), it is not a big deal. + // Does not worth the cost to wait. + // eslint-disable-next-line @typescript-eslint/no-misused-promises + setTimeout(async () => { + this.logger.info('Syncing done, but still some data to send to Prosody') + for (const [roomJID, data] of prosodyRoomUpdates.entries()) { + try { + await updateProsodyRoom(this.options, roomJID, data) + } catch (err) { + this.logger.error(`Failed updating prosody room info: "${err as string}".`) + } + } + }, 0) + } } /**