diff --git a/server/lib/plugins/plugin-helpers-builder.ts b/server/lib/plugins/plugin-helpers-builder.ts index d57c69ef0..f1bc24d8b 100644 --- a/server/lib/plugins/plugin-helpers-builder.ts +++ b/server/lib/plugins/plugin-helpers-builder.ts @@ -17,6 +17,7 @@ import { VideoBlacklistCreate } from '@shared/models' import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist' import { getServerConfig } from '../config' import { blacklistVideo, unblacklistVideo } from '../video-blacklist' +import { UserModel } from '@server/models/account/user' function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers { const logger = buildPluginLogger(npmName) @@ -163,6 +164,11 @@ function buildPluginRelatedHelpers (plugin: MPlugin, npmName: string) { function buildUserHelpers () { return { - getAuthUser: (res: express.Response) => res.locals.oauth?.token?.User + getAuthUser: (res: express.Response) => { + const user = res.locals.oauth?.token?.User + if (!user) return undefined + + return UserModel.loadByIdFull(user.id) + } } } diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 00c6d73aa..513455773 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts @@ -565,6 +565,10 @@ export class UserModel extends Model { return UserModel.unscoped().findByPk(id) } + static loadByIdFull (id: number): Promise { + return UserModel.findByPk(id) + } + static loadByIdWithChannels (id: number, withStats = false): Promise { const scopes = [ ScopeNames.WITH_VIDEOCHANNELS diff --git a/server/tests/fixtures/peertube-plugin-test-four/main.js b/server/tests/fixtures/peertube-plugin-test-four/main.js index 6ed0c20d2..b9b207b81 100644 --- a/server/tests/fixtures/peertube-plugin-test-four/main.js +++ b/server/tests/fixtures/peertube-plugin-test-four/main.js @@ -88,8 +88,8 @@ async function register ({ return res.json({ routerRoute }) }) - router.get('/user', (req, res) => { - const user = peertubeHelpers.user.getAuthUser(res) + router.get('/user', async (req, res) => { + const user = await peertubeHelpers.user.getAuthUser(res) if (!user) return res.sendStatus(404) const isAdmin = user.role === 0 @@ -98,6 +98,7 @@ async function register ({ return res.json({ username: user.username, + displayName: user.Account.name, isAdmin, isModerator, isUser diff --git a/server/tests/plugins/plugin-helpers.ts b/server/tests/plugins/plugin-helpers.ts index 20020ec41..f72de8229 100644 --- a/server/tests/plugins/plugin-helpers.ts +++ b/server/tests/plugins/plugin-helpers.ts @@ -133,6 +133,7 @@ describe('Test plugin helpers', function () { }) expect(res.body.username).to.equal('root') + expect(res.body.displayName).to.equal('root') expect(res.body.isAdmin).to.be.true expect(res.body.isModerator).to.be.false expect(res.body.isUser).to.be.false diff --git a/server/types/plugins/register-server-option.model.ts b/server/types/plugins/register-server-option.model.ts index 4af476ed2..2432b7ac4 100644 --- a/server/types/plugins/register-server-option.model.ts +++ b/server/types/plugins/register-server-option.model.ts @@ -70,13 +70,13 @@ export type PeerTubeHelpers = { user: { // PeerTube >= 3.2 - getAuthUser: (response: Response) => { + getAuthUser: (response: Response) => Promise<{ id?: string username: string email: string blocked: boolean role: UserRole - } | undefined + } | undefined> } } diff --git a/support/doc/plugins/guide.md b/support/doc/plugins/guide.md index 53d53c26d..5b7d1cb31 100644 --- a/support/doc/plugins/guide.md +++ b/support/doc/plugins/guide.md @@ -261,8 +261,8 @@ function register ({ router.get('/ping', (req, res) => res.json({ message: 'pong' })) // Users are automatically authenticated - router.get('/auth', (res, res) => { - const user = peertubeHelpers.user.getAuthUser(res) + router.get('/auth', async (res, res) => { + const user = await peertubeHelpers.user.getAuthUser(res) const isAdmin = user.role === 0 const isModerator = user.role === 1