peertube-plugin-livechat/server/lib/routers/api.ts

164 lines
4.8 KiB
TypeScript
Raw Normal View History

2021-04-29 16:31:48 +00:00
import type { Router, Request, Response, NextFunction } from 'express'
import { videoHasWebchat } from '../../../shared/lib/video'
2021-05-03 18:06:36 +00:00
import { asyncMiddleware } from '../middlewares/async'
2021-04-29 16:31:48 +00:00
// See here for description: https://modules.prosody.im/mod_muc_http_defaults.html
interface RoomDefaults {
2021-05-01 08:22:06 +00:00
config: {
name: string
description: string
2021-05-02 12:35:58 +00:00
language?: string
persistent?: boolean
public?: boolean
members_only?: boolean
allow_member_invites?: boolean
public_jids?: boolean
2021-05-01 08:22:06 +00:00
subject: string
2021-05-02 12:35:58 +00:00
changesubject?: boolean
// historylength: number
moderated?: boolean
archiving?: boolean
2021-05-01 08:22:06 +00:00
}
affiliations?: Array<{
jid: string
affiliation: 'outcast' | 'none' | 'member' | 'admin' | 'owner'
nick?: string
}>
2021-04-29 16:31:48 +00:00
}
async function initApiRouter (options: RegisterServerOptions): Promise<Router> {
const { peertubeHelpers, getRouter } = options
const router = getRouter()
const logger = peertubeHelpers.logger
2021-05-03 18:06:36 +00:00
router.get('/room', asyncMiddleware(
async (req: Request, res: Response, _next: NextFunction) => {
2021-04-30 16:03:12 +00:00
const jid: string = req.query.jid as string || ''
logger.info(`Requesting room information for room '${jid}'.`)
const video = await peertubeHelpers.videos.loadByIdOrUUID(jid)
if (!video) {
2021-05-01 17:01:31 +00:00
logger.warn(`Video ${jid} not found`)
res.sendStatus(403)
return
2021-04-30 16:03:12 +00:00
}
// check settings (chat enabled for this video?)
const settings = await options.settingsManager.getSettings([
2021-05-03 18:37:23 +00:00
'chat-use-prosody',
'chat-only-locals',
'chat-all-lives',
'chat-all-non-lives',
'chat-videos-list'
])
2021-05-03 18:37:23 +00:00
if (!settings['chat-use-prosody']) {
logger.warn('Prosody chat is not active')
res.sendStatus(403)
return
}
if (!videoHasWebchat({
'chat-only-locals': settings['chat-only-locals'] as boolean,
'chat-all-lives': settings['chat-all-lives'] as boolean,
'chat-all-non-lives': settings['chat-all-non-lives'] as boolean,
'chat-videos-list': settings['chat-videos-list'] as string
}, video)) {
2021-05-01 17:01:31 +00:00
logger.warn(`Video ${jid} has not chat activated`)
res.sendStatus(403)
return
}
2021-04-30 16:03:12 +00:00
const roomDefaults: RoomDefaults = {
2021-05-01 08:22:06 +00:00
config: {
name: video.name,
description: '',
2021-05-02 12:35:58 +00:00
language: video.language,
2021-05-01 08:22:06 +00:00
subject: video.name
},
affiliations: [] // so that the first user will not be moderator/admin
2021-04-30 16:03:12 +00:00
}
res.json(roomDefaults)
2021-04-29 16:31:48 +00:00
}
2021-05-03 18:06:36 +00:00
))
2021-04-29 16:31:48 +00:00
2021-05-03 18:37:23 +00:00
router.post('/user/register', asyncMiddleware(
async (req: Request, res: Response, _next: NextFunction) => {
res.sendStatus(501)
}
))
router.get('/user/check_password', asyncMiddleware(
async (req: Request, res: Response, _next: NextFunction) => {
const settings = await options.settingsManager.getSettings([
'chat-use-prosody',
'chat-only-locals',
'chat-all-lives',
'chat-all-non-lives',
'chat-videos-list'
])
if (!settings['chat-use-prosody']) {
logger.warn('Prosody chat is not active')
res.status(200).send('false')
return
}
const user = req.query.user
const server = req.query.server
const pass = req.query.pass
if (server !== 'localhost') {
logger.warn(`Cannot call check_password on user on server ${server as string}.`)
res.status(200).send('false')
return
}
if (user === 'john' && pass === 'password') {
res.status(200).send('true')
return
}
res.status(200).send('false')
}
))
router.get('/user/user_exists', asyncMiddleware(
async (req: Request, res: Response, _next: NextFunction) => {
const settings = await options.settingsManager.getSettings([
'chat-use-prosody',
'chat-only-locals',
'chat-all-lives',
'chat-all-non-lives',
'chat-videos-list'
])
if (!settings['chat-use-prosody']) {
logger.warn('Prosody chat is not active')
res.status(200).send('false')
return
}
const user = req.query.user
const server = req.query.server
if (server !== 'localhost') {
logger.warn(`Cannot call user_exists on user on server ${server as string}.`)
res.status(200).send('false')
return
}
if (user === 'john') {
res.status(200).send('true')
}
res.status(200).send('false')
}
))
router.post('/user/set_password', asyncMiddleware(
async (req: Request, res: Response, _next: NextFunction) => {
res.sendStatus(501)
}
))
router.post('/user/remove_user', asyncMiddleware(
async (req: Request, res: Response, _next: NextFunction) => {
res.sendStatus(501)
}
))
2021-04-29 16:31:48 +00:00
return router
}
export {
initApiRouter
}