peertube-plugin-livechat/server/lib/helpers.ts

120 lines
3.7 KiB
TypeScript
Raw Normal View History

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
function getBaseRouterRoute (options: RegisterServerOptions): string {
// In Peertube <= 3.1.0, PeertubeHelpers dont provide this function
// Available in Peertube >= 3.2.0
if (options.peertubeHelpers.plugin) {
return options.peertubeHelpers.plugin.getBaseRouterRoute()
}
return '/plugins/' + pluginShortName + '/' + version + '/router/'
2021-04-09 19:28:16 +00:00
}
function getBaseStaticRoute (options: RegisterServerOptions): string {
// In Peertube <= 3.1.0, PeertubeHelpers dont provide this function.
// Available in Peertube >= 3.2.0
if (options.peertubeHelpers.plugin) {
return options.peertubeHelpers.plugin.getBaseStaticRoute()
}
2021-04-12 18:52:21 +00:00
return '/plugins/' + pluginShortName + '/' + version + '/static/'
2021-04-09 19:28:16 +00:00
}
// Peertube <= 3.1.0 has no way to test that current user is admin
// Peertube >= 3.2.0 has getAuthUser helper
2021-05-05 13:55:38 +00:00
async function isUserAdmin (options: RegisterServerOptions, res: Response): Promise<boolean> {
const user = await getAuthUser(options, res)
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
}
return true
}
// Peertube <= 3.1.0 has no way to get user informations.
// This is a hack.
// Peertube >= 3.2.0 has getAuthUser helper
2021-05-05 13:55:38 +00:00
async function getAuthUser (options: RegisterServerOptions, res: Response): Promise<MUserDefault | undefined> {
const peertubeHelpers = options.peertubeHelpers
if (peertubeHelpers.user?.getAuthUser) {
return peertubeHelpers.user.getAuthUser(res)
2021-04-10 11:57:47 +00:00
}
peertubeHelpers.logger.debug('Peertube does not provide getAuthUser for now, fallback on hack')
return res.locals.oauth?.token?.User
2021-04-10 11:57:47 +00:00
}
// Peertube <= 3.1.0 has no way to obtain user nickname/
// Peertube >= 3.2.0: getAuthUser has user.Account.name.
async function getUserNickname (options: RegisterServerOptions, user: MUserDefault): Promise<string | undefined> {
const peertubeHelpers = options.peertubeHelpers
2021-05-04 14:33:32 +00:00
const logger = peertubeHelpers.logger
if (user.Account?.name) {
return user.Account.name
}
peertubeHelpers.logger.debug('Peertube does not provide user.Account.name, fallback on hack')
if (!user.id) {
logger.error('getUserNickname: missing user id')
return undefined
}
const userId = Number.isInteger(user.id) ? user.id : parseInt(user.id as string)
if (Number.isNaN(userId)) {
logger.error('getUserNickname: Invalid User Id, Not a number')
return undefined
}
if (!Number.isInteger(userId)) {
2021-05-04 14:33:32 +00:00
logger.error('getUserNickname: Invalid User Id, should be an integer')
return undefined
}
const [results] = await peertubeHelpers.database.query(
`SELECT name FROM "account" WHERE "userId" = ${userId.toString()}`
)
2021-05-04 14:33:32 +00:00
if (!Array.isArray(results)) {
logger.error('getUserNickname: query result is not an array.')
return undefined
}
if (!results[0]) {
logger.error(`getUserNickname: no result for id ${userId}`)
2021-05-04 14:33:32 +00:00
return undefined
}
if (typeof results[0] !== 'object') {
logger.error('getUserNickname: query result is not an object')
return undefined
}
if (!('name' in results[0])) {
logger.error('getUserNickname: no name field in result')
return undefined
}
return results[0].name as string
}
2021-04-09 19:28:16 +00:00
export {
getBaseRouterRoute,
2021-04-10 11:57:47 +00:00
getBaseStaticRoute,
2021-04-12 18:52:21 +00:00
isUserAdmin,
getAuthUser,
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
}