From 6f57931a9d3bcc82fcc321d2fa42b5d16b7fbf99 Mon Sep 17 00:00:00 2001 From: John Livingston Date: Wed, 2 Jun 2021 13:14:03 +0200 Subject: [PATCH] Remove backward compatibility (user.Account.name is always provided). --- server/@types/peertube.d.ts | 5 ++--- server/lib/helpers.ts | 44 ++----------------------------------- 2 files changed, 4 insertions(+), 45 deletions(-) diff --git a/server/@types/peertube.d.ts b/server/@types/peertube.d.ts index 8184eb04..4baa2b57 100644 --- a/server/@types/peertube.d.ts +++ b/server/@types/peertube.d.ts @@ -91,9 +91,8 @@ interface MUserDefault { // FIXME: this interface is not complete email: string blocked: boolean role: UserRole - Account?: { - // Account.name comes in Peertube 3.2.0 - name?: string + Account?: { // FIXME: can this be undefined? + name: string } } diff --git a/server/lib/helpers.ts b/server/lib/helpers.ts index c0f4c3b9..7e66b321 100644 --- a/server/lib/helpers.ts +++ b/server/lib/helpers.ts @@ -57,8 +57,6 @@ async function getAuthUser (options: RegisterServerOptions, res: Response): Prom return res.locals.oauth?.token?.User } -// 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 @@ -66,46 +64,8 @@ async function getUserNickname (options: RegisterServerOptions, user: MUserDefau 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" = ${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 ${userId}`) - 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 + logger.error('There is no Account.name on the user') + return undefined } export {