Skip to content

Commit 0890c75

Browse files
committed
framework&ui: add serialize guard [skip-cache]
1 parent 3d70cf3 commit 0890c75

5 files changed

Lines changed: 24 additions & 14 deletions

File tree

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ jobs:
4747
run: |
4848
git config --global user.name undefined
4949
git config --global user.email i@undefined.moe
50-
yarn set version stable
50+
yarn set version 4.13.0
5151
yarn
5252
yarn dedupe
5353
node build/prepare.js

build/prepare.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ const UIConfig = {
9090
'packages/ui-default/backendlib/misc.ts',
9191
'packages/ui-default/backendlib/template.ts',
9292
'packages/ui-default/backendlib/markdown.js',
93+
'packages/ui-default/service-worker.ts',
94+
'packages/ui-default/components/message/worker.ts',
9395
'**/node_modules',
9496
],
9597
include: ['ts', 'tsx', 'vue', 'json']

framework/framework/api.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,22 @@ type Projection<T, S> = S extends ProjectionSchemaId
7575
[K in keyof T & keyof S]: K extends keyof AsKeys<S> ? Projection<T[K], AsKeys<S>[K]> : never
7676
};
7777

78-
export const projection = <T, S extends ProjectionSchema<T>>(input: T, schema: S): Projection<T, S> => {
78+
export const projection = <T, S extends ProjectionSchema<T>>(input: T, schema: S, serializeCtx?: any): Projection<T, S> => {
7979
if (typeof input !== 'object' || input === null) throw new Error('Input must be an object.');
8080
type R = Projection<T, S>;
81+
if ('serialize' in input && typeof (input as any).serialize === 'function') {
82+
input = (input as any).serialize(serializeCtx);
83+
}
8184
if (Array.isArray(schema)) schema = Object.fromEntries(schema.map((s) => [s, 1])) as S;
8285
if (Array.isArray(input)) {
83-
return input.map((item) => projection(item, schema)) as R;
86+
return input.map((item) => projection(item, schema, serializeCtx)) as R;
8487
}
8588
const result = {} as R;
86-
for (const key of Reflect.ownKeys(input)) {
89+
for (const key of Reflect.ownKeys(input as any)) {
8790
const schemaIt = schema[key];
8891
if (!schemaIt) continue;
8992
if (schemaIt === 1 || !input[key]) result[key] = input[key];
90-
else result[key] = projection(input[key], schemaIt);
93+
else result[key] = projection(input[key], schemaIt, serializeCtx);
9194
}
9295
return result;
9396
};
@@ -174,7 +177,7 @@ export class ApiService extends Service {
174177
}
175178
}
176179
}
177-
return (project && typeof result === 'object' && result !== null) ? projection(result, project) : result;
180+
return (project && typeof result === 'object' && result !== null) ? projection(result, project, context) : result;
178181
}
179182
}
180183

packages/hydrooj/src/model/user.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { escapeRegExp, omit, pick, uniq } from 'lodash';
22
import { LRUCache } from 'lru-cache';
33
import { Collection, Filter, ObjectId } from 'mongodb';
4-
import { serializer } from '@hydrooj/framework';
54
import { LoginError, UserAlreadyExistError, UserNotFoundError } from '../error';
65
import {
76
Authenticator, BaseUserDict, FileInfo, GDoc,
@@ -167,11 +166,16 @@ export class User {
167166
return type === 'public' ? fields : fields.concat(this._privateFields);
168167
}
169168

170-
serialize(h) {
171-
if (!this._isPrivate) {
172-
return pick(this, this.getFields(h?.user?.hasPerm(PERM.PERM_VIEW_USER_PRIVATE_INFO) ? 'private' : 'public'));
169+
serialize(h?) {
170+
if (this._isPrivate) {
171+
const result: any = {};
172+
for (const key of Object.keys(this)) {
173+
if (key.startsWith('_') && key !== '_id') continue;
174+
result[key] = this[key];
175+
}
176+
return result;
173177
}
174-
return JSON.stringify(this, serializer(true, h));
178+
return pick(this, this.getFields(h?.user?.hasPerm(PERM.PERM_VIEW_USER_PRIVATE_INFO) ? 'private' : 'public'));
175179
}
176180
}
177181

packages/ui-default/backendlib/template.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ let { template } = argv.options;
1818
if (!template || typeof template !== 'string') template = findFileSync('@hydrooj/ui-default/templates');
1919
else template = findFileSync(template);
2020

21-
const replacer = (k, v) => {
21+
const replacer = (s) => (k, v) => {
2222
if (k.startsWith('_') && k !== '_id') return undefined;
2323
if (typeof v === 'bigint') return `BigInt::${v.toString()}`;
24+
if (v && typeof v === 'object' && 'serialize' in v && typeof v.serialize === 'function') return v.serialize(s);
2425
return v;
2526
};
2627

@@ -68,7 +69,7 @@ class Nunjucks extends nunjucks.Environment {
6869
callback(error);
6970
}
7071
}, true);
71-
this.addFilter('json', (self) => (self ? JSON.stringify(self, replacer) : ''));
72+
this.addFilter('json', (self, s) => (self ? JSON.stringify(self, replacer(s)) : ''));
7273
this.addFilter('parseYaml', (self) => yaml.load(self));
7374
this.addFilter('dumpYaml', (self) => yaml.dump(self));
7475
this.addFilter('assign', (self, data) => Object.assign(self, data));
@@ -79,7 +80,7 @@ class Nunjucks extends nunjucks.Environment {
7980
this.addFilter('base64_decode', (s) => Buffer.from(s, 'base64').toString());
8081
this.addFilter('jsesc', (self) => jsesc(self, { isScriptContext: true }));
8182
this.addFilter('bitand', (self, val) => self & val);
82-
this.addFilter('toString', (self) => (typeof self === 'string' ? self : JSON.stringify(self, replacer)));
83+
this.addFilter('toString', (self, s) => (typeof self === 'string' ? self : JSON.stringify(self, replacer(s))));
8384
this.addFilter('content', (content, language, html) => {
8485
let s: any = '';
8586
try {

0 commit comments

Comments
 (0)