Peertube 3.2.0 will provide user.Account.name.

This commit is contained in:
John Livingston 2021-05-05 16:30:18 +02:00
parent 3658ee797a
commit 787fed19d0
3 changed files with 34 additions and 14 deletions

View File

@ -85,13 +85,14 @@ enum UserRole {
}
interface MUserDefault { // FIXME: this interface is not complete
id?: string
id?: string | number // FIXME: type is not clear... Documentation says string, but it seems to be number
username: string
email: string
blocked: boolean
role: UserRole
Account: {
name: string
Account?: {
// Account.name comes in Peertube 3.2.0
name?: string
}
}

View File

@ -49,20 +49,44 @@ async function getAuthUser (options: RegisterServerOptions, res: Response): Prom
return res.locals.oauth?.token?.User
}
// FIXME: Peertube <= 3.1.0 has no way to obtain user nickname
async function getUserNickname ({ peertubeHelpers }: RegisterServerOptions, id: number): Promise<string | undefined> {
// FIXME: 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
const logger = peertubeHelpers.logger
if (!Number.isInteger(id)) {
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)) {
logger.error('getUserNickname: Invalid User Id, should be an integer')
return undefined
}
const [results] = await peertubeHelpers.database.query(`SELECT name FROM "account" WHERE "userId" = ${id}`)
const [results] = await peertubeHelpers.database.query(
`SELECT name FROM "account" WHERE "userId" = ${userId.toString()}`
)
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 ${id}`)
logger.error(`getUserNickname: no result for id ${userId}`)
return undefined
}
if (typeof results[0] !== 'object') {

View File

@ -98,13 +98,8 @@ async function initApiRouter (options: RegisterServerOptions): Promise<Router> {
res.sendStatus(403)
return
}
let userId: number | undefined
if (user.id) {
userId = parseInt(user.id)
if (Number.isNaN(userId)) { userId = undefined }
}
const password: string = await prosodyRegisterUser(user.username)
const nickname: string | undefined = userId ? (await getUserNickname(options, userId)) : undefined
const nickname: string | undefined = await getUserNickname(options, user)
res.status(200).json({
jid: user.username + '@localhost',
password: password,