Remove backward compatibility (peertubeHelpers.user.getAuthUser always available).

This commit is contained in:
John Livingston 2021-06-02 14:07:12 +02:00
parent 6f57931a9d
commit 228a60ca3a
3 changed files with 5 additions and 23 deletions

View File

@ -127,14 +127,12 @@ interface PeerTubeHelpers {
} }
videos: { videos: {
loadByUrl: (url: string) => Promise<MVideoThumbnail> loadByUrl: (url: string) => Promise<MVideoThumbnail>
// NB: loadByIdOrUUID was introduced in v3.1.0
loadByIdOrUUID: (id: number | string) => Promise<MVideoThumbnail> loadByIdOrUUID: (id: number | string) => Promise<MVideoThumbnail>
removeVideo: (videoId: number) => Promise<void> removeVideo: (videoId: number) => Promise<void>
} }
config: { config: {
getWebserverUrl: () => string getWebserverUrl: () => string
// getServerConfig comes with Peertube 3.2.0 getServerConfig: () => Promise<ServerConfig>
getServerConfig?: () => Promise<ServerConfig>
} }
moderation: { moderation: {
blockServer: (options: { byAccountId: number, hostToBlock: string }) => Promise<void> blockServer: (options: { byAccountId: number, hostToBlock: string }) => Promise<void>
@ -149,8 +147,7 @@ interface PeerTubeHelpers {
server: { server: {
getServerActor: () => Promise<ActorModel> getServerActor: () => Promise<ActorModel>
} }
// Added in Peertube 3.2.0 user: {
user?: {
getAuthUser: (res: express.Response) => Promise<MUserDefault | undefined> getAuthUser: (res: express.Response) => Promise<MUserDefault | undefined>
} }
// Added in Peertube 3.2.0 // Added in Peertube 3.2.0

View File

@ -29,10 +29,8 @@ function getBaseStaticRoute (options: RegisterServerOptions): string {
return '/plugins/' + pluginShortName + '/' + version + '/static/' return '/plugins/' + pluginShortName + '/' + version + '/static/'
} }
// Peertube <= 3.1.0 has no way to test that current user is admin
// Peertube >= 3.2.0 has getAuthUser helper
async function isUserAdmin (options: RegisterServerOptions, res: Response): Promise<boolean> { async function isUserAdmin (options: RegisterServerOptions, res: Response): Promise<boolean> {
const user = await getAuthUser(options, res) const user = await options.peertubeHelpers.user.getAuthUser(res)
if (!user) { if (!user) {
return false return false
} }
@ -45,18 +43,6 @@ async function isUserAdmin (options: RegisterServerOptions, res: Response): Prom
return true return true
} }
// Peertube <= 3.1.0 has no way to get user informations.
// This is a hack.
// Peertube >= 3.2.0 has getAuthUser helper
async function getAuthUser (options: RegisterServerOptions, res: Response): Promise<MUserDefault | undefined> {
const peertubeHelpers = options.peertubeHelpers
if (peertubeHelpers.user?.getAuthUser) {
return peertubeHelpers.user.getAuthUser(res)
}
peertubeHelpers.logger.debug('Peertube does not provide getAuthUser for now, fallback on hack')
return res.locals.oauth?.token?.User
}
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
@ -72,7 +58,6 @@ export {
getBaseRouterRoute, getBaseRouterRoute,
getBaseStaticRoute, getBaseStaticRoute,
isUserAdmin, isUserAdmin,
getAuthUser,
getUserNickname, getUserNickname,
pluginName, pluginName,
pluginShortName pluginShortName

View File

@ -3,7 +3,7 @@ import { videoHasWebchat } from '../../../shared/lib/video'
import { asyncMiddleware } from '../middlewares/async' import { asyncMiddleware } from '../middlewares/async'
import { getCheckAPIKeyMiddleware } from '../middlewares/apikey' import { getCheckAPIKeyMiddleware } from '../middlewares/apikey'
import { prosodyCheckUserPassword, prosodyRegisterUser, prosodyUserRegistered } from '../prosody/auth' import { prosodyCheckUserPassword, prosodyRegisterUser, prosodyUserRegistered } from '../prosody/auth'
import { getAuthUser, getUserNickname } from '../helpers' import { getUserNickname } from '../helpers'
import { Affiliations, getVideoAffiliations } from '../prosody/config/affiliations' import { Affiliations, getVideoAffiliations } from '../prosody/config/affiliations'
import { getProsodyDomain } from '../prosody/config/domain' import { getProsodyDomain } from '../prosody/config/domain'
@ -92,7 +92,7 @@ async function initApiRouter (options: RegisterServerOptions): Promise<Router> {
router.get('/auth', asyncMiddleware( router.get('/auth', asyncMiddleware(
async (req: Request, res: Response, _next: NextFunction) => { async (req: Request, res: Response, _next: NextFunction) => {
const user = await getAuthUser(options, res) const user = await peertubeHelpers.user.getAuthUser(res)
if (!user) { if (!user) {
res.sendStatus(403) res.sendStatus(403)
return return