Prosody: use the user nickname.

This commit is contained in:
John Livingston 2021-05-04 16:33:32 +02:00
parent deff34ab95
commit ad2d7742e9
3 changed files with 45 additions and 4 deletions

View File

@ -17,6 +17,7 @@ function inIframe (): boolean {
interface AuthentInfos { interface AuthentInfos {
jid: string jid: string
password: string password: string
nickname?: string
} }
async function authenticatedMode (authenticationUrl: string): Promise<false | AuthentInfos> { async function authenticatedMode (authenticationUrl: string): Promise<false | AuthentInfos> {
try { try {
@ -61,7 +62,8 @@ async function authenticatedMode (authenticationUrl: string): Promise<false | Au
} }
return { return {
jid: data.jid, jid: data.jid,
password: data.password password: data.password,
nickname: data.nickname
} }
} catch (error) { } catch (error) {
console.error(error) console.error(error)
@ -132,7 +134,11 @@ window.initConverse = async function initConverse ({
params.auto_reconnect = true params.auto_reconnect = true
params.jid = auth.jid params.jid = auth.jid
params.password = auth.password params.password = auth.password
if (auth.nickname) {
params.nickname = auth.nickname
} else {
params.muc_nickname_from_jid = true params.muc_nickname_from_jid = true
}
// FIXME: use params.oauth_providers? // FIXME: use params.oauth_providers?
} }
} }

View File

@ -48,11 +48,39 @@ function getAuthUser ({ peertubeHelpers }: RegisterServerOptions, res: Response)
return res.locals.oauth?.token?.User 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> {
const logger = peertubeHelpers.logger
if (!Number.isInteger(id)) {
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}`)
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}`)
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 {
getBaseRouter, getBaseRouter,
getBaseStaticRoute, getBaseStaticRoute,
isUserAdmin, isUserAdmin,
getAuthUser, getAuthUser,
getUserNickname,
pluginName, pluginName,
pluginShortName pluginShortName
} }

View File

@ -2,7 +2,7 @@ import type { Router, Request, Response, NextFunction } from 'express'
import { videoHasWebchat } from '../../../shared/lib/video' import { videoHasWebchat } from '../../../shared/lib/video'
import { asyncMiddleware } from '../middlewares/async' import { asyncMiddleware } from '../middlewares/async'
import { prosodyCheckUserPassword, prosodyRegisterUser, prosodyUserRegistered } from '../prosody/auth' import { prosodyCheckUserPassword, prosodyRegisterUser, prosodyUserRegistered } from '../prosody/auth'
import { getAuthUser } from '../helpers' import { getAuthUser, getUserNickname } from '../helpers'
// See here for description: https://modules.prosody.im/mod_muc_http_defaults.html // See here for description: https://modules.prosody.im/mod_muc_http_defaults.html
interface RoomDefaults { interface RoomDefaults {
@ -92,10 +92,17 @@ async function initApiRouter (options: RegisterServerOptions): Promise<Router> {
res.sendStatus(403) res.sendStatus(403)
return 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 password: string = await prosodyRegisterUser(user.username)
const nickname: string | undefined = userId ? (await getUserNickname(options, userId)) : undefined
res.status(200).json({ res.status(200).json({
jid: user.username + '@localhost', jid: user.username + '@localhost',
password: password password: password,
nickname: nickname
}) })
} }
)) ))