Fix #87: updating chat room title when video/channel title is changed:

* renaming module list_rooms to manage_rooms
* added some API to update room info
* when a video or a channel is updated, sending an API call to update
  the room
This commit is contained in:
John Livingston
2024-03-07 16:22:14 +01:00
parent 2115b352a4
commit 4dd4f18965
10 changed files with 205 additions and 51 deletions

View File

@ -1,41 +0,0 @@
import type { RegisterServerOptions } from '@peertube/peertube-types'
import { getCurrentProsody } from './host'
import { getAPIKey } from '../../apikey'
const got = require('got')
interface ProsodyRoomDesc {
jid: string
localpart: string
name: string
lang: string
description: string
lasttimestamp?: number
}
async function listProsodyRooms (options: RegisterServerOptions): Promise<ProsodyRoomDesc[]> {
const logger = options.peertubeHelpers.logger
const currentProsody = getCurrentProsody()
if (!currentProsody) {
throw new Error('It seems that prosody is not binded... Cant list rooms.')
}
// Requesting on localhost, because currentProsody.host does not always resolves correctly (docker use case, ...)
const apiUrl = `http://localhost:${currentProsody.port}/peertubelivechat_list_rooms/list-rooms`
logger.debug('Calling list rooms API on url: ' + apiUrl)
const rooms = await got(apiUrl, {
method: 'GET',
headers: {
authorization: 'Bearer ' + await getAPIKey(options),
host: currentProsody.host
},
responseType: 'json',
resolveBodyOnly: true
})
return rooms
}
export {
listProsodyRooms
}

View File

@ -0,0 +1,110 @@
import type { RegisterServerOptions } from '@peertube/peertube-types'
import { getCurrentProsody } from './host'
import { getAPIKey } from '../../apikey'
import { getProsodyDomain } from '../config/domain'
const got = require('got')
interface ProsodyRoomDesc {
jid: string
localpart: string
name: string
lang: string
description: string
lasttimestamp?: number
}
/**
* Lists existing chatrooms.
* @param options Peertube server options
* @returns List of chat rooms on the Prosody server
*/
async function listProsodyRooms (options: RegisterServerOptions): Promise<ProsodyRoomDesc[]> {
const logger = options.peertubeHelpers.logger
const currentProsody = getCurrentProsody()
if (!currentProsody) {
throw new Error('It seems that prosody is not binded... Cant list rooms.')
}
// Requesting on localhost, because currentProsody.host does not always resolves correctly (docker use case, ...)
const apiUrl = `http://localhost:${currentProsody.port}/peertubelivechat_manage_rooms/list-rooms`
logger.debug('Calling list rooms API on url: ' + apiUrl)
const rooms = await got(apiUrl, {
method: 'GET',
headers: {
authorization: 'Bearer ' + await getAPIKey(options),
host: currentProsody.host
},
responseType: 'json',
resolveBodyOnly: true
})
return rooms
}
/**
* Update room metadata on the Prosody server.
* Uses an API provided by mod_http_peertubelivechat_manage_rooms.
*
* 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
* @returns true if success
*/
async function updateProsodyRoom (
options: RegisterServerOptions,
channelId: number | string,
jid: string,
data: {
name: string
}
): Promise<boolean> {
const logger = options.peertubeHelpers.logger
const currentProsody = getCurrentProsody()
if (!currentProsody) {
throw new Error('It seems that prosody is not binded... Cant update room.')
}
if (!jid.includes('@')) {
jid = jid + '@room.' + await getProsodyDomain(options)
}
logger.debug('Calling update room for ' + jid)
// 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
}
try {
logger.debug('Calling update room API on url: ' + apiUrl + ', with data: ' + JSON.stringify(apiData))
const result = await got(apiUrl, {
method: 'POST',
headers: {
authorization: 'Bearer ' + await getAPIKey(options),
host: currentProsody.host
},
json: apiData,
responseType: 'json',
resolveBodyOnly: true
})
logger.debug('Update room API response: ' + JSON.stringify(result))
} catch (err) {
// We consider it is not very bad if the metadata are not correctly updated.
// Nothing too important.
logger.error(`Failed to update room: ' ${err as string}`)
return false
}
return true
}
export {
listProsodyRooms,
updateProsodyRoom
}

View File

@ -313,7 +313,7 @@ async function getProsodyConfig (options: RegisterServerOptionsV5): Promise<Pros
// TODO: add a settings to choose?
config.useDefaultPersistent()
config.useListRoomsApi(apikey)
config.useManageRoomsApi(apikey)
config.usePeertubeVCards(basePeertubeUrl)
config.useAnonymousRandomVCards(paths.avatars, paths.avatarsFiles)

View File

@ -415,9 +415,9 @@ class ProsodyConfigContent {
this.muc.set('muc_room_default_persistent', true)
}
useListRoomsApi (apikey: string): void {
this.muc.add('modules_enabled', 'http_peertubelivechat_list_rooms')
this.muc.set('peertubelivechat_list_rooms_apikey', apikey)
useManageRoomsApi (apikey: string): void {
this.muc.add('modules_enabled', 'http_peertubelivechat_manage_rooms')
this.muc.set('peertubelivechat_manage_rooms_apikey', apikey)
}
useTestModule (prosodyApikey: string, apiurl: string): void {