Moderation configuration screen: WIP.
This commit is contained in:
@ -51,6 +51,7 @@ interface ChannelInfos {
|
||||
id: number
|
||||
name: string
|
||||
displayName: string
|
||||
ownerAccountId: number
|
||||
}
|
||||
|
||||
async function getChannelInfosById (options: RegisterServerOptions, channelId: number): Promise<ChannelInfos | null> {
|
||||
@ -64,7 +65,8 @@ async function getChannelInfosById (options: RegisterServerOptions, channelId: n
|
||||
'SELECT' +
|
||||
' "actor"."preferredUsername" as "channelName",' +
|
||||
' "videoChannel"."id" as "channelId",' +
|
||||
' "videoChannel"."name" as "channelDisplayName"' +
|
||||
' "videoChannel"."name" as "channelDisplayName",' +
|
||||
' "videoChannel"."accountId" as "ownerAccountId"' +
|
||||
' FROM "videoChannel"' +
|
||||
' RIGHT JOIN "actor" ON "actor"."id" = "videoChannel"."actorId"' +
|
||||
' WHERE "videoChannel"."id" = ' + channelId.toString()
|
||||
@ -79,7 +81,8 @@ async function getChannelInfosById (options: RegisterServerOptions, channelId: n
|
||||
return {
|
||||
id: results[0].channelId,
|
||||
name: results[0].channelName ?? '',
|
||||
displayName: results[0].channelDisplayName ?? ''
|
||||
displayName: results[0].channelDisplayName ?? '',
|
||||
ownerAccountId: results[0].ownerAccountId
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -75,6 +75,20 @@ async function isUserAdmin (options: RegisterServerOptions, res: Response): Prom
|
||||
return true
|
||||
}
|
||||
|
||||
async function isUserAdminOrModerator (options: RegisterServerOptions, res: Response): Promise<boolean> {
|
||||
const user = await options.peertubeHelpers.user.getAuthUser(res)
|
||||
if (!user) {
|
||||
return false
|
||||
}
|
||||
if (user.blocked) {
|
||||
return false
|
||||
}
|
||||
if (user.role !== 0 && user.role !== 1) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
type Unpack<T> = T extends Promise<infer U | undefined> ? U : T
|
||||
type AuthUser = Unpack<ReturnType<PeerTubeHelpers['user']['getAuthUser']>>
|
||||
|
||||
@ -95,6 +109,7 @@ export {
|
||||
getBaseWebSocketRoute,
|
||||
getBaseStaticRoute,
|
||||
isUserAdmin,
|
||||
isUserAdminOrModerator,
|
||||
getUserNickname,
|
||||
pluginName,
|
||||
pluginShortName,
|
||||
|
@ -7,6 +7,7 @@ import { isDebugMode } from '../debug'
|
||||
import { initRoomApiRouter } from './api/room'
|
||||
import { initAuthApiRouter, initUserAuthApiRouter } from './api/auth'
|
||||
import { initFederationServerInfosApiRouter } from './api/federation-server-infos'
|
||||
import { initModerationApiRouter } from './api/moderation'
|
||||
|
||||
/**
|
||||
* Initiate API routes
|
||||
@ -52,6 +53,8 @@ async function initApiRouter (options: RegisterServerOptions): Promise<Router> {
|
||||
))
|
||||
}
|
||||
|
||||
router.use('/moderation', await initModerationApiRouter(options))
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
|
62
server/lib/routers/api/moderation.ts
Normal file
62
server/lib/routers/api/moderation.ts
Normal file
@ -0,0 +1,62 @@
|
||||
import type { RegisterServerOptions } from '@peertube/peertube-types'
|
||||
import type { Router, Request, Response, NextFunction } from 'express'
|
||||
import type { ChannelModerationOptions } from '../../../../shared/lib/types'
|
||||
import { asyncMiddleware } from '../../middlewares/async'
|
||||
import { getChannelInfosById } from '../../database/channel'
|
||||
import { isUserAdminOrModerator } from '../../helpers'
|
||||
|
||||
async function initModerationApiRouter (options: RegisterServerOptions): Promise<Router> {
|
||||
const router = options.getRouter()
|
||||
const logger = options.peertubeHelpers.logger
|
||||
|
||||
router.get('/channel/:channelId', asyncMiddleware(
|
||||
async (req: Request, res: Response, _next: NextFunction): Promise<void> => {
|
||||
const channelId = req.params.channelId
|
||||
const currentUser = await options.peertubeHelpers.user.getAuthUser(res)
|
||||
|
||||
if (!channelId || !/^\d+$/.test(channelId)) {
|
||||
res.sendStatus(400)
|
||||
return
|
||||
}
|
||||
|
||||
const channelInfos = await getChannelInfosById(options, parseInt(channelId))
|
||||
if (!channelInfos) {
|
||||
logger.warn(`Channel ${channelId} not found`)
|
||||
res.sendStatus(404)
|
||||
return
|
||||
}
|
||||
|
||||
// To access this page, you must either be:
|
||||
// - the channel owner,
|
||||
// - an instance modo/admin
|
||||
// - TODO: a channel chat moderator, as defined in this page.
|
||||
if (channelInfos.ownerAccountId === currentUser.Account.id) {
|
||||
logger.debug('Current user is the channel owner')
|
||||
} else if (await isUserAdminOrModerator(options, res)) {
|
||||
logger.debug('Current user is an instance moderator or admin')
|
||||
} else {
|
||||
logger.warn('Current user tries to access a channel for which he has no right.')
|
||||
res.sendStatus(403)
|
||||
return
|
||||
}
|
||||
|
||||
logger.debug('User can access the moderation channel api.')
|
||||
|
||||
const result: ChannelModerationOptions = {
|
||||
channel: {
|
||||
id: channelInfos.id,
|
||||
name: channelInfos.name,
|
||||
displayName: channelInfos.displayName
|
||||
}
|
||||
}
|
||||
res.status(200)
|
||||
res.json(result)
|
||||
}
|
||||
))
|
||||
|
||||
return router
|
||||
}
|
||||
|
||||
export {
|
||||
initModerationApiRouter
|
||||
}
|
Reference in New Issue
Block a user