mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core&ui: add PREM_VIEW_DISPLAYNAME (#410)
Co-authored-by: undefined <i@undefined.moe>
This commit is contained in:
1 parent
b9bc504917
commit
7ebbaacb2a
9 files changed
+40
-18
No files matched your search
@@ -1,23 +1,27 @@
|
||||
import { ObjectID } from 'mongodb';
|
||||
import avatar from 'hydrooj/src/lib/avatar';
|
||||
import { PERM } from 'hydrooj/src/model/builtin';
|
||||
import * as system from 'hydrooj/src/model/system';
|
||||
import { User } from 'hydrooj/src/model/user';
|
||||
import type { KoaContext } from '../server';
|
||||
|
||||
function serializer(k: string, v: any) {
|
||||
const serializer = (showDisplayName = false) => (k: string, v: any) => {
|
||||
if (k.startsWith('_') && k !== '_id') return undefined;
|
||||
if (typeof v === 'bigint') return `BigInt::${v.toString()}`;
|
||||
if (v instanceof User && !showDisplayName) delete v.displayName;
|
||||
return v;
|
||||
}
|
||||
};
|
||||
|
||||
export default (router, logger) => async (ctx: KoaContext, next) => {
|
||||
const { request, response } = ctx.HydroContext;
|
||||
const { request, response, user } = ctx.HydroContext;
|
||||
ctx.renderHTML = (templateName, args) => {
|
||||
const UserContext: any = {
|
||||
...(ctx.HydroContext.user || {}),
|
||||
avatar: avatar(ctx.HydroContext.user?.avatar || '', 128),
|
||||
...(user || {}),
|
||||
avatar: avatar(user?.avatar || '', 128),
|
||||
viewLang: ctx.translate('__id'),
|
||||
};
|
||||
const engine = global.Hydro.lib.template?.render || (() => JSON.stringify(args, serializer));
|
||||
const engine = global.Hydro.lib.template?.render
|
||||
|| (() => JSON.stringify(args, serializer(user?.hasPerm(PERM.PREM_VIEW_DISPLAYNAME))));
|
||||
return engine(templateName, {
|
||||
handler: ctx.handler,
|
||||
UserContext,
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
import { errorMessage } from '@hydrooj/utils/lib/utils';
|
||||
import { SystemError, UserFacingError } from 'hydrooj/src/error';
|
||||
import { PERM } from 'hydrooj/src/model/builtin';
|
||||
import { User } from 'hydrooj/src/model/user';
|
||||
import type { KoaContext } from '../server';
|
||||
|
||||
function serializer(k: string, v: any) {
|
||||
const serializer = (showDisplayName = false) => (k: string, v: any) => {
|
||||
if (k.startsWith('_') && k !== '_id') return undefined;
|
||||
if (typeof v === 'bigint') return `BigInt::${v.toString()}`;
|
||||
if (v instanceof User && !showDisplayName) delete v.displayName;
|
||||
return v;
|
||||
}
|
||||
};
|
||||
|
||||
export default (logger) => async (ctx: KoaContext, next) => {
|
||||
const {
|
||||
@@ -28,7 +31,7 @@ export default (logger) => async (ctx: KoaContext, next) => {
|
||||
response.body.UiContext = UiContext;
|
||||
response.body.UserContext = user;
|
||||
}
|
||||
response.body = JSON.stringify(response.body, serializer);
|
||||
response.body = JSON.stringify(response.body, serializer(user?.hasPerm(PERM.PREM_VIEW_DISPLAYNAME)));
|
||||
} catch (e) {
|
||||
response.body = new SystemError('Serialize failure', e.message);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user