framework&ui: add serialize guard [skip-cache]

This commit is contained in:
undefined committed 2026-04-17 04:13:18 +08:00
1 parent 3d70cf3352
commit 0890c75c17
5 files changed
+24 -14

No files matched your search

+9 -5
View File
@@ -1,7 +1,6 @@
import { escapeRegExp, omit, pick, uniq } from 'lodash';
import { LRUCache } from 'lru-cache';
import { Collection, Filter, ObjectId } from 'mongodb';
import { serializer } from '@hydrooj/framework';
import { LoginError, UserAlreadyExistError, UserNotFoundError } from '../error';
import {
Authenticator, BaseUserDict, FileInfo, GDoc,
@@ -167,11 +166,16 @@ export class User {
return type === 'public' ? fields : fields.concat(this._privateFields);
}
serialize(h) {
if (!this._isPrivate) {
return pick(this, this.getFields(h?.user?.hasPerm(PERM.PERM_VIEW_USER_PRIVATE_INFO) ? 'private' : 'public'));
serialize(h?) {
if (this._isPrivate) {
const result: any = {};
for (const key of Object.keys(this)) {
if (key.startsWith('_') && key !== '_id') continue;
result[key] = this[key];
}
return result;
}
return JSON.stringify(this, serializer(true, h));
return pick(this, this.getFields(h?.user?.hasPerm(PERM.PERM_VIEW_USER_PRIVATE_INFO) ? 'private' : 'public'));
}
}
+4 -3
View File
@@ -18,9 +18,10 @@ let { template } = argv.options;
if (!template || typeof template !== 'string') template = findFileSync('@hydrooj/ui-default/templates');
else template = findFileSync(template);
const replacer = (k, v) => {
const replacer = (s) => (k, v) => {
if (k.startsWith('_') && k !== '_id') return undefined;
if (typeof v === 'bigint') return `BigInt::${v.toString()}`;
if (v && typeof v === 'object' && 'serialize' in v && typeof v.serialize === 'function') return v.serialize(s);
return v;
};
@@ -68,7 +69,7 @@ class Nunjucks extends nunjucks.Environment {
callback(error);
}
}, true);
this.addFilter('json', (self) => (self ? JSON.stringify(self, replacer) : ''));
this.addFilter('json', (self, s) => (self ? JSON.stringify(self, replacer(s)) : ''));
this.addFilter('parseYaml', (self) => yaml.load(self));
this.addFilter('dumpYaml', (self) => yaml.dump(self));
this.addFilter('assign', (self, data) => Object.assign(self, data));
@@ -79,7 +80,7 @@ class Nunjucks extends nunjucks.Environment {
this.addFilter('base64_decode', (s) => Buffer.from(s, 'base64').toString());
this.addFilter('jsesc', (self) => jsesc(self, { isScriptContext: true }));
this.addFilter('bitand', (self, val) => self & val);
this.addFilter('toString', (self) => (typeof self === 'string' ? self : JSON.stringify(self, replacer)));
this.addFilter('toString', (self, s) => (typeof self === 'string' ? self : JSON.stringify(self, replacer(s))));
this.addFilter('content', (content, language, html) => {
let s: any = '';
try {