Updating slow mode duration on existing rooms when changing channel options (related to #332).

This commit is contained in:
John Livingston 2024-03-07 17:33:18 +01:00
parent 08b84899f6
commit e67b21dd9f
No known key found for this signature in database
GPG Key ID: B17B5640CE66CDBC
5 changed files with 48 additions and 9 deletions

View File

@ -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

View File

@ -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

View File

@ -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(
() => {},

View File

@ -49,17 +49,16 @@ async function listProsodyRooms (options: RegisterServerOptions): Promise<Prosod
* Note: could be called without verifying that the room exists.
* On the Prosody side, non existing rooms will be ignored.
* @param options Peertube server options
* @param channelId associated channelId
* @param jid Room JID (can be only the local part, or the local + domain)
* @param data Data to update
* @param data Data to update. Note: will only try to update data that are given.
* @returns true if success
*/
async function updateProsodyRoom (
options: RegisterServerOptions,
channelId: number | string,
jid: string,
data: {
name: string
name?: string
slow_mode_duration?: number
}
): Promise<boolean> {
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))

View File

@ -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<string, Parameters<typeof updateProsodyRoom>[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)
}
}
/**