Peertube 3.2.0 will provide user.Account.name.
This commit is contained in:
parent
3658ee797a
commit
787fed19d0
7
server/@types/peertube.d.ts
vendored
7
server/@types/peertube.d.ts
vendored
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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<string | undefined> {
|
||||
// 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<string | undefined> {
|
||||
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') {
|
||||
|
@ -98,13 +98,8 @@ async function initApiRouter (options: RegisterServerOptions): Promise<Router> {
|
||||
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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user