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
|
email: string
|
||||||
blocked: boolean
|
blocked: boolean
|
||||||
role: UserRole
|
role: UserRole
|
||||||
Account?: {
|
Account?: { // FIXME: can this be undefined?
|
||||||
// Account.name comes in Peertube 3.2.0
|
name: string
|
||||||
name?: string
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,8 +57,6 @@ async function getAuthUser (options: RegisterServerOptions, res: Response): Prom
|
|||||||
return res.locals.oauth?.token?.User
|
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> {
|
async function getUserNickname (options: RegisterServerOptions, user: MUserDefault): Promise<string | undefined> {
|
||||||
const peertubeHelpers = options.peertubeHelpers
|
const peertubeHelpers = options.peertubeHelpers
|
||||||
const logger = peertubeHelpers.logger
|
const logger = peertubeHelpers.logger
|
||||||
@ -66,46 +64,8 @@ async function getUserNickname (options: RegisterServerOptions, user: MUserDefau
|
|||||||
if (user.Account?.name) {
|
if (user.Account?.name) {
|
||||||
return user.Account.name
|
return user.Account.name
|
||||||
}
|
}
|
||||||
|
logger.error('There is no Account.name on the user')
|
||||||
peertubeHelpers.logger.debug('Peertube does not provide user.Account.name, fallback on hack')
|
|
||||||
|
|
||||||
if (!user.id) {
|
|
||||||
logger.error('getUserNickname: missing user id')
|
|
||||||
return undefined
|
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 {
|
export {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user