Changing defaults MUC affiliation (#385):

* For Peertube moderators/admins, we add a button "Promote". Clicking on it will promote them as MUC owner.
This commit is contained in:
John Livingston
2024-05-17 15:17:36 +02:00
parent 5745e8c8a3
commit da75765bdb
12 changed files with 324 additions and 9 deletions

View File

@ -376,6 +376,8 @@ async function _localRoomJID (
}
room = room.replace(/{{CHANNEL_ID}}/g, `${channelId}`)
if (room.includes('{{CHANNEL_NAME}}')) {
// FIXME: this should no more exists, since we removed options to include other chat server.
// So we should remove this code. (and simplify the above code)
const channelName = await getChannelNameById(options, channelId)
if (channelName === null) {
throw new Error('Channel not found')

View File

@ -8,6 +8,7 @@ import { initRoomApiRouter } from './api/room'
import { initAuthApiRouter, initUserAuthApiRouter } from './api/auth'
import { initFederationServerInfosApiRouter } from './api/federation-server-infos'
import { initConfigurationApiRouter } from './api/configuration'
import { initPromoteApiRouter } from './api/promote'
/**
* Initiate API routes
@ -36,6 +37,7 @@ async function initApiRouter (options: RegisterServerOptions): Promise<Router> {
await initFederationServerInfosApiRouter(options, router)
await initConfigurationApiRouter(options, router)
await initPromoteApiRouter(options, router)
if (isDebugMode(options)) {
// Only add this route if the debug mode is enabled at time of the server launch.

View File

@ -0,0 +1,53 @@
import type { RegisterServerOptions } from '@peertube/peertube-types'
import type { Router, Request, Response, NextFunction } from 'express'
import type { Affiliations } from '../../prosody/config/affiliations'
import { asyncMiddleware } from '../../middlewares/async'
import { isUserAdminOrModerator } from '../../helpers'
import { getProsodyDomain } from '../../prosody/config/domain'
import { updateProsodyRoom } from '../../prosody/api/manage-rooms'
async function initPromoteApiRouter (options: RegisterServerOptions, router: Router): Promise<void> {
const logger = options.peertubeHelpers.logger
router.put('/promote/:roomJID', asyncMiddleware(
async (req: Request, res: Response, _next: NextFunction): Promise<void> => {
try {
const roomJIDLocalPart = req.params.roomJID
const user = await options.peertubeHelpers.user.getAuthUser(res)
if (!user || !await isUserAdminOrModerator(options, res)) {
logger.warn('Current user tries to access the promote API for which he has no right.')
res.sendStatus(403)
return
}
if (!/^(channel\.\d+|(\w|-)+)$/.test(roomJIDLocalPart)) { // just check if it looks alright.
logger.warn('Current user tries to access the promote API using an invalid room key.')
res.sendStatus(400)
return
}
const normalizedUsername = user.username.toLowerCase()
const prosodyDomain = await getProsodyDomain(options)
const jid = normalizedUsername + '@' + prosodyDomain
const mucJID = roomJIDLocalPart + '@' + 'room.' + prosodyDomain
logger.info('We must give owner affiliation to ' + jid + ' on ' + mucJID)
const addAffiliations: Affiliations = {}
addAffiliations[jid] = 'owner'
await updateProsodyRoom(options, mucJID, {
addAffiliations
})
res.sendStatus(200)
} catch (err) {
logger.error(err)
res.sendStatus(500)
}
}
))
}
export {
initPromoteApiRouter
}