2024-05-23 09:42:14 +00:00
|
|
|
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-09-07 15:20:01 +00:00
|
|
|
import type { RegisterServerOptions } from '@peertube/peertube-types'
|
|
|
|
import type { Router, Request, Response, NextFunction } from 'express'
|
|
|
|
import { videoHasWebchat } from '../../../../shared/lib/video'
|
|
|
|
import { asyncMiddleware } from '../../middlewares/async'
|
|
|
|
import { getCheckAPIKeyMiddleware } from '../../middlewares/apikey'
|
|
|
|
import { Affiliations, getVideoAffiliations, getChannelAffiliations } from '../../prosody/config/affiliations'
|
|
|
|
import { fillVideoCustomFields } from '../../custom-fields'
|
|
|
|
import { getChannelInfosById } from '../../database/channel'
|
2023-09-08 18:00:14 +00:00
|
|
|
import { RoomChannel } from '../../room-channel'
|
2024-02-13 11:49:22 +00:00
|
|
|
import {
|
|
|
|
getChannelConfigurationOptions,
|
|
|
|
getDefaultChannelConfigurationOptions
|
|
|
|
} from '../../configuration/channel/storage'
|
2023-09-07 15:20:01 +00:00
|
|
|
|
|
|
|
// See here for description: https://modules.prosody.im/mod_muc_http_defaults.html
|
|
|
|
interface RoomDefaults {
|
|
|
|
config: {
|
|
|
|
name: string
|
|
|
|
description: string
|
|
|
|
language?: string
|
|
|
|
persistent?: boolean
|
|
|
|
public?: boolean
|
|
|
|
members_only?: boolean
|
|
|
|
allow_member_invites?: boolean
|
|
|
|
public_jids?: boolean
|
|
|
|
// subject_from: string
|
|
|
|
// subject: string
|
|
|
|
changesubject?: boolean
|
|
|
|
// historylength: number
|
|
|
|
moderated?: boolean
|
|
|
|
archiving?: boolean
|
2024-02-13 11:49:22 +00:00
|
|
|
|
|
|
|
// Following fields are specific to livechat (for now), and requires a customized version for mod_muc_http_defaults.
|
2024-02-16 14:16:44 +00:00
|
|
|
slow_mode_duration?: number
|
2024-06-20 14:46:14 +00:00
|
|
|
mute_anonymous?: boolean
|
2023-09-07 15:20:01 +00:00
|
|
|
}
|
|
|
|
affiliations?: Affiliations
|
|
|
|
}
|
|
|
|
|
2024-06-20 14:46:14 +00:00
|
|
|
async function _getChannelSpecificOptions (
|
|
|
|
options: RegisterServerOptions,
|
|
|
|
channelId: number
|
|
|
|
): Promise<Partial<RoomDefaults['config']>> {
|
2024-02-13 11:49:22 +00:00
|
|
|
const channelOptions = await getChannelConfigurationOptions(options, channelId) ??
|
|
|
|
getDefaultChannelConfigurationOptions(options)
|
2024-06-20 14:46:14 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
slow_mode_duration: channelOptions.slowMode.duration,
|
|
|
|
mute_anonymous: channelOptions.mute.anonymous
|
|
|
|
}
|
2024-02-13 11:49:22 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 15:20:01 +00:00
|
|
|
/**
|
|
|
|
* Instanciate the route for room APIs.
|
|
|
|
* These APIs are used by Prosody to get room defaults from the Peertube server.
|
|
|
|
* @param options server register options
|
|
|
|
*/
|
|
|
|
async function initRoomApiRouter (options: RegisterServerOptions, router: Router): Promise<void> {
|
|
|
|
const logger = options.peertubeHelpers.logger
|
|
|
|
|
|
|
|
router.get('/room', asyncMiddleware([
|
|
|
|
getCheckAPIKeyMiddleware(options),
|
|
|
|
async (req: Request, res: Response, _next: NextFunction) => {
|
|
|
|
const jid: string = req.query.jid as string || ''
|
|
|
|
logger.info(`Requesting room information for room '${jid}'.`)
|
|
|
|
|
|
|
|
const settings = await options.settingsManager.getSettings([
|
|
|
|
'prosody-room-type'
|
|
|
|
])
|
|
|
|
// Now, we have two different room type: per video or per channel.
|
|
|
|
if (settings['prosody-room-type'] === 'channel') {
|
|
|
|
const matches = jid.match(/^channel\.(\d+)$/)
|
|
|
|
if (!matches || !matches[1]) {
|
|
|
|
logger.warn(`Invalid channel room jid '${jid}'.`)
|
|
|
|
res.sendStatus(403)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
const channelId = parseInt(matches[1])
|
|
|
|
const channelInfos = await getChannelInfosById(options, channelId)
|
|
|
|
if (!channelInfos) {
|
|
|
|
logger.warn(`Channel ${channelId} not found`)
|
|
|
|
res.sendStatus(403)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let affiliations: Affiliations
|
|
|
|
try {
|
|
|
|
affiliations = await getChannelAffiliations(options, channelId)
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(`Failed to get channel affiliations for ${channelId}:`, error)
|
|
|
|
// affiliations: should at least be {}, so that the first user will not be moderator/admin
|
|
|
|
affiliations = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
const roomDefaults: RoomDefaults = {
|
2024-06-20 14:46:14 +00:00
|
|
|
config: Object.assign(
|
|
|
|
{
|
|
|
|
name: channelInfos.displayName,
|
|
|
|
description: ''
|
|
|
|
// subject: channelInfos.displayName
|
|
|
|
},
|
|
|
|
await _getChannelSpecificOptions(options, channelId)
|
|
|
|
),
|
2023-09-07 15:20:01 +00:00
|
|
|
affiliations: affiliations
|
|
|
|
}
|
2023-09-07 17:01:56 +00:00
|
|
|
|
2023-09-18 13:10:20 +00:00
|
|
|
RoomChannel.singleton().link(channelId, jid)
|
2023-09-07 17:01:56 +00:00
|
|
|
|
2023-09-07 15:20:01 +00:00
|
|
|
res.json(roomDefaults)
|
|
|
|
} else {
|
|
|
|
// FIXME: @peertube/peertype-types@4.2.2: wrongly considere video as MVideoThumbnail.
|
|
|
|
const video = await options.peertubeHelpers.videos.loadByIdOrUUID(jid)
|
|
|
|
if (!video) {
|
|
|
|
logger.warn(`Video ${jid} not found`)
|
|
|
|
res.sendStatus(403)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adding the custom fields and data:
|
|
|
|
await fillVideoCustomFields(options, video)
|
|
|
|
|
|
|
|
// check settings (chat enabled for this video?)
|
|
|
|
const settings = await options.settingsManager.getSettings([
|
|
|
|
'chat-per-live-video',
|
|
|
|
'chat-all-lives',
|
|
|
|
'chat-all-non-lives',
|
|
|
|
'chat-videos-list'
|
|
|
|
])
|
|
|
|
if (!videoHasWebchat({
|
|
|
|
'chat-per-live-video': !!settings['chat-per-live-video'],
|
|
|
|
'chat-all-lives': !!settings['chat-all-lives'],
|
|
|
|
'chat-all-non-lives': !!settings['chat-all-non-lives'],
|
|
|
|
'chat-videos-list': settings['chat-videos-list'] as string
|
|
|
|
}, video)) {
|
|
|
|
logger.warn(`Video ${jid} has not chat activated`)
|
|
|
|
res.sendStatus(403)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let affiliations: Affiliations
|
|
|
|
try {
|
|
|
|
affiliations = await getVideoAffiliations(options, video)
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(`Failed to get video affiliations for ${video.uuid}:`, error)
|
|
|
|
// affiliations: should at least be {}, so that the first user will not be moderator/admin
|
|
|
|
affiliations = {}
|
|
|
|
}
|
|
|
|
|
|
|
|
const roomDefaults: RoomDefaults = {
|
2024-06-20 14:46:14 +00:00
|
|
|
config: Object.assign(
|
|
|
|
{
|
|
|
|
name: video.name,
|
|
|
|
description: '',
|
|
|
|
language: video.language
|
|
|
|
// subject: video.name
|
|
|
|
},
|
|
|
|
await _getChannelSpecificOptions(options, video.channelId)
|
|
|
|
),
|
2023-09-07 15:20:01 +00:00
|
|
|
affiliations: affiliations
|
|
|
|
}
|
2023-09-07 17:01:56 +00:00
|
|
|
|
2023-09-18 13:10:20 +00:00
|
|
|
RoomChannel.singleton().link(video.channelId, jid)
|
2023-09-07 17:01:56 +00:00
|
|
|
|
2023-09-07 15:20:01 +00:00
|
|
|
res.json(roomDefaults)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]))
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
initRoomApiRouter
|
|
|
|
}
|