Remove backward compatibility (user.Account.name is always provided).
This commit is contained in:
parent
d2df17c7b9
commit
6f57931a9d
5
server/@types/peertube.d.ts
vendored
5
server/@types/peertube.d.ts
vendored
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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,48 +64,10 @@ 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')
|
||||
logger.error('There is no Account.name on the user')
|
||||
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
|
||||
}
|
||||
|
||||
export {
|
||||
getBaseRouterRoute,
|
||||
getBaseStaticRoute,
|
||||
|
Loading…
x
Reference in New Issue
Block a user