Remove backward compatibility (user.Account.name is always provided).

This commit is contained in:
John Livingston 2021-06-02 13:14:03 +02:00
parent d2df17c7b9
commit 6f57931a9d
2 changed files with 4 additions and 45 deletions

View File

@ -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
}
}

View File

@ -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<string | undefined> {
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 {