diff --git a/server/@types/peertube.d.ts b/server/@types/peertube.d.ts index 4baa2b57..0e286a7b 100644 --- a/server/@types/peertube.d.ts +++ b/server/@types/peertube.d.ts @@ -127,14 +127,12 @@ interface PeerTubeHelpers { } videos: { loadByUrl: (url: string) => Promise - // NB: loadByIdOrUUID was introduced in v3.1.0 loadByIdOrUUID: (id: number | string) => Promise removeVideo: (videoId: number) => Promise } config: { getWebserverUrl: () => string - // getServerConfig comes with Peertube 3.2.0 - getServerConfig?: () => Promise + getServerConfig: () => Promise } moderation: { blockServer: (options: { byAccountId: number, hostToBlock: string }) => Promise @@ -149,8 +147,7 @@ interface PeerTubeHelpers { server: { getServerActor: () => Promise } - // Added in Peertube 3.2.0 - user?: { + user: { getAuthUser: (res: express.Response) => Promise } // Added in Peertube 3.2.0 diff --git a/server/lib/helpers.ts b/server/lib/helpers.ts index 7e66b321..5a592802 100644 --- a/server/lib/helpers.ts +++ b/server/lib/helpers.ts @@ -29,10 +29,8 @@ function getBaseStaticRoute (options: RegisterServerOptions): string { 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 { - const user = await getAuthUser(options, res) + const user = await options.peertubeHelpers.user.getAuthUser(res) if (!user) { return false } @@ -45,18 +43,6 @@ async function isUserAdmin (options: RegisterServerOptions, res: Response): Prom 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 { - 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 { const peertubeHelpers = options.peertubeHelpers const logger = peertubeHelpers.logger @@ -72,7 +58,6 @@ export { getBaseRouterRoute, getBaseStaticRoute, isUserAdmin, - getAuthUser, getUserNickname, pluginName, pluginShortName diff --git a/server/lib/routers/api.ts b/server/lib/routers/api.ts index 75cb305f..2695418a 100644 --- a/server/lib/routers/api.ts +++ b/server/lib/routers/api.ts @@ -3,7 +3,7 @@ import { videoHasWebchat } from '../../../shared/lib/video' import { asyncMiddleware } from '../middlewares/async' import { getCheckAPIKeyMiddleware } from '../middlewares/apikey' import { prosodyCheckUserPassword, prosodyRegisterUser, prosodyUserRegistered } from '../prosody/auth' -import { getAuthUser, getUserNickname } from '../helpers' +import { getUserNickname } from '../helpers' import { Affiliations, getVideoAffiliations } from '../prosody/config/affiliations' import { getProsodyDomain } from '../prosody/config/domain' @@ -92,7 +92,7 @@ async function initApiRouter (options: RegisterServerOptions): Promise { router.get('/auth', asyncMiddleware( async (req: Request, res: Response, _next: NextFunction) => { - const user = await getAuthUser(options, res) + const user = await peertubeHelpers.user.getAuthUser(res) if (!user) { res.sendStatus(403) return