2022-01-11 00:29:33 +00:00
|
|
|
import type { RegisterServerOptions, PeerTubeHelpers } from '@peertube/peertube-types'
|
2022-10-13 16:34:41 +00:00
|
|
|
import type { Response } from 'express'
|
|
|
|
import type { IncomingMessage } from 'http'
|
|
|
|
import type { Duplex } from 'stream'
|
2021-04-10 11:57:47 +00:00
|
|
|
|
2023-04-20 10:13:22 +00:00
|
|
|
const pluginVersionRegexp = /^\d+\.\d+\.\d+(?:-(?:rc|alpha|beta)\.\d+)?$/
|
|
|
|
const pluginVersionWordBreakRegex = /\b\d+\.\d+\.\d+(?:-(?:rc|alpha|beta)\.\d+)?\b/
|
|
|
|
|
2021-04-09 19:28:16 +00:00
|
|
|
const packagejson: any = require('../../../package.json')
|
|
|
|
const version: string = packagejson.version || ''
|
2023-04-20 10:13:22 +00:00
|
|
|
if (!pluginVersionRegexp.test(version)) {
|
2021-04-09 19:28:16 +00:00
|
|
|
throw new Error('Incorrect version in package.json.')
|
|
|
|
}
|
2021-04-12 18:52:21 +00:00
|
|
|
const pluginName: string = packagejson.name || ''
|
|
|
|
if (!/^peertube-plugin-[-a-z]+$/.test(pluginName)) {
|
2021-04-09 19:28:16 +00:00
|
|
|
throw new Error('Incorrect plugin name in package.json.')
|
|
|
|
}
|
2021-04-12 18:52:21 +00:00
|
|
|
const pluginShortName = pluginName.substring('peertube-plugin-'.length)
|
2021-04-09 19:28:16 +00:00
|
|
|
|
2021-05-18 16:06:11 +00:00
|
|
|
function getBaseRouterRoute (options: RegisterServerOptions): string {
|
2021-06-02 13:48:56 +00:00
|
|
|
if (!options.peertubeHelpers.plugin) {
|
|
|
|
throw new Error('Missing peertubeHelpers.plugin, have you the correct Peertube version?')
|
2021-05-18 16:06:11 +00:00
|
|
|
}
|
2021-06-02 13:48:56 +00:00
|
|
|
return options.peertubeHelpers.plugin.getBaseRouterRoute()
|
2021-04-09 19:28:16 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 16:34:41 +00:00
|
|
|
interface RegisterServerWebSocketRouteOptions {
|
|
|
|
route: string
|
|
|
|
handler: (request: IncomingMessage, socket: Duplex, head: Buffer) => any
|
|
|
|
}
|
|
|
|
// getBaseWebSocketRoute() comes with Peertube 5.0.0.
|
|
|
|
type RegisterServerOptionsV5 = RegisterServerOptions & {
|
|
|
|
registerWebSocketRoute?: (options: RegisterServerWebSocketRouteOptions) => void
|
|
|
|
peertubeHelpers: {
|
|
|
|
plugin: {
|
|
|
|
getBaseWebSocketRoute?: () => string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-23 16:53:12 +00:00
|
|
|
/**
|
2022-10-13 16:34:41 +00:00
|
|
|
* Returns the base route for Websocket endpoint.
|
|
|
|
* This features comes with Peertube >=5.0.0.
|
2022-08-23 16:53:12 +00:00
|
|
|
* @param options server options
|
2022-10-13 16:34:41 +00:00
|
|
|
* @returns the route, or undefined if the Peertube version does not provide this feature
|
2022-08-23 16:53:12 +00:00
|
|
|
*/
|
2022-10-13 16:34:41 +00:00
|
|
|
function getBaseWebSocketRoute (options: RegisterServerOptionsV5): string | undefined {
|
|
|
|
if (!options.peertubeHelpers.plugin) {
|
|
|
|
throw new Error('Missing peertubeHelpers.plugin, have you the correct Peertube version?')
|
|
|
|
}
|
|
|
|
if (!options.peertubeHelpers.plugin.getBaseWebSocketRoute) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
return options.peertubeHelpers.plugin.getBaseWebSocketRoute()
|
2022-08-23 16:53:12 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 16:06:11 +00:00
|
|
|
function getBaseStaticRoute (options: RegisterServerOptions): string {
|
2021-06-02 13:48:56 +00:00
|
|
|
if (!options.peertubeHelpers.plugin) {
|
|
|
|
throw new Error('Missing peertubeHelpers.plugin, have you the correct Peertube version?')
|
2021-05-18 16:06:11 +00:00
|
|
|
}
|
2021-06-02 13:48:56 +00:00
|
|
|
return options.peertubeHelpers.plugin.getBaseStaticRoute()
|
2021-04-09 19:28:16 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 13:55:38 +00:00
|
|
|
async function isUserAdmin (options: RegisterServerOptions, res: Response): Promise<boolean> {
|
2021-06-02 12:07:12 +00:00
|
|
|
const user = await options.peertubeHelpers.user.getAuthUser(res)
|
2021-05-04 11:00:44 +00:00
|
|
|
if (!user) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if (user.blocked) {
|
|
|
|
return false
|
|
|
|
}
|
2021-05-04 13:33:05 +00:00
|
|
|
if (user.role !== 0) {
|
2021-04-10 11:57:47 +00:00
|
|
|
return false
|
|
|
|
}
|
2021-05-04 11:00:44 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2023-08-08 16:26:40 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-01-11 00:29:33 +00:00
|
|
|
type Unpack<T> = T extends Promise<infer U | undefined> ? U : T
|
|
|
|
type AuthUser = Unpack<ReturnType<PeerTubeHelpers['user']['getAuthUser']>>
|
|
|
|
|
2022-12-07 15:55:44 +00:00
|
|
|
async function getUserNickname (options: RegisterServerOptions, user: AuthUser): Promise<string | undefined> {
|
2021-05-05 14:30:18 +00:00
|
|
|
const peertubeHelpers = options.peertubeHelpers
|
2021-05-04 14:33:32 +00:00
|
|
|
const logger = peertubeHelpers.logger
|
2021-05-05 14:30:18 +00:00
|
|
|
|
|
|
|
if (user.Account?.name) {
|
|
|
|
return user.Account.name
|
|
|
|
}
|
2021-06-02 11:14:03 +00:00
|
|
|
logger.error('There is no Account.name on the user')
|
|
|
|
return undefined
|
2021-05-04 14:33:32 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 19:28:16 +00:00
|
|
|
export {
|
2022-10-13 16:34:41 +00:00
|
|
|
RegisterServerOptionsV5,
|
2021-05-18 16:06:11 +00:00
|
|
|
getBaseRouterRoute,
|
2022-10-13 16:34:41 +00:00
|
|
|
getBaseWebSocketRoute,
|
2021-04-10 11:57:47 +00:00
|
|
|
getBaseStaticRoute,
|
2021-04-12 18:52:21 +00:00
|
|
|
isUserAdmin,
|
2023-08-08 16:26:40 +00:00
|
|
|
isUserAdminOrModerator,
|
2021-05-04 14:33:32 +00:00
|
|
|
getUserNickname,
|
2021-04-12 18:52:21 +00:00
|
|
|
pluginName,
|
2023-04-20 10:13:22 +00:00
|
|
|
pluginShortName,
|
|
|
|
pluginVersionRegexp,
|
|
|
|
pluginVersionWordBreakRegex
|
2021-04-09 19:28:16 +00:00
|
|
|
}
|