From 787fed19d04fa5486f30ce1ba2674d67caa25c8b Mon Sep 17 00:00:00 2001 From: John Livingston Date: Wed, 5 May 2021 16:30:18 +0200 Subject: [PATCH] Peertube 3.2.0 will provide user.Account.name. --- server/@types/peertube.d.ts | 7 ++++--- server/lib/helpers.ts | 34 +++++++++++++++++++++++++++++----- server/lib/routers/api.ts | 7 +------ 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/server/@types/peertube.d.ts b/server/@types/peertube.d.ts index 8a128bcd..f64a4cf3 100644 --- a/server/@types/peertube.d.ts +++ b/server/@types/peertube.d.ts @@ -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 } } diff --git a/server/lib/helpers.ts b/server/lib/helpers.ts index 14c63186..cce42249 100644 --- a/server/lib/helpers.ts +++ b/server/lib/helpers.ts @@ -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 { +// 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 { + 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') { diff --git a/server/lib/routers/api.ts b/server/lib/routers/api.ts index 4cbd5b99..e2995bdd 100644 --- a/server/lib/routers/api.ts +++ b/server/lib/routers/api.ts @@ -98,13 +98,8 @@ async function initApiRouter (options: RegisterServerOptions): Promise { 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,