2022-01-11 00:29:33 +00:00
|
|
|
import type { RegisterServerOptions, PeerTubeHelpers } from '@peertube/peertube-types'
|
2021-04-10 11:57:47 +00:00
|
|
|
import { Response } from 'express'
|
|
|
|
|
2021-04-09 19:28:16 +00:00
|
|
|
const packagejson: any = require('../../../package.json')
|
|
|
|
const version: string = packagejson.version || ''
|
|
|
|
if (!/^\d+\.\d+\.\d+/.test(version)) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-01-11 00:29:33 +00:00
|
|
|
// FIXME: @peertube/peertube-types@4.0.0-beta.3 is missing user.Account.name definition.
|
|
|
|
type Unpack<T> = T extends Promise<infer U | undefined> ? U : T
|
|
|
|
type AuthUser = Unpack<ReturnType<PeerTubeHelpers['user']['getAuthUser']>>
|
|
|
|
interface AuthUserFixed extends AuthUser {
|
|
|
|
Account?: {
|
|
|
|
name: string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getUserNickname (options: RegisterServerOptions, user: AuthUserFixed): 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 {
|
2021-05-18 16:06:11 +00:00
|
|
|
getBaseRouterRoute,
|
2021-04-10 11:57:47 +00:00
|
|
|
getBaseStaticRoute,
|
2021-04-12 18:52:21 +00:00
|
|
|
isUserAdmin,
|
2021-05-04 14:33:32 +00:00
|
|
|
getUserNickname,
|
2021-04-12 18:52:21 +00:00
|
|
|
pluginName,
|
|
|
|
pluginShortName
|
2021-04-09 19:28:16 +00:00
|
|
|
}
|