修复oauth.google setting字段异常问题,将__mock__添加至ignore

This commit is contained in:
undefined committed 2020-09-07 22:55:18 +08:00
1 parent 147637d539
commit 6ba93841ea
17 files changed
+103 -16

No files matched your search

+1 -1
View File
@@ -159,7 +159,7 @@ class HomeSecurityHandler extends Handler {
if (udoc) throw new UserAlreadyExistError(email);
const [code] = await token.add(
token.TYPE_CHANGEMAIL,
await system.get('changemail_token_expire_seconds') as number,
await system.get('changemail_token_expire_seconds'),
{ uid: this.user._id, email },
);
const m = await this.renderHTML('user_changemail_mail.html', {
+2
View File
@@ -151,10 +151,12 @@ class SystemSettingHandler extends SystemHandler {
if (typeof args[key] === 'object') {
for (const subkey in args[key]) {
if (typeof set(`${key}.${subkey}`, args[key][subkey]) !== 'undefined') {
// @ts-ignore
tasks.push(system.set(`${key}.${subkey}`, set(`${key}.${subkey}`, args[key][subkey])));
}
}
} else if (typeof set(key, args[key]) !== 'undefined') {
// @ts-ignore
tasks.push(system.set(key, set(key, args[key])));
}
}
+2 -2
View File
@@ -62,7 +62,7 @@ class UserRegisterHandler extends Handler {
this.limitRate('send_mail', 3600, 30);
const t = await token.add(
token.TYPE_REGISTRATION,
await system.get('registration_token_expire_seconds') as number,
await system.get('registration_token_expire_seconds'),
{ mail },
);
if (await system.get('smtp.user')) {
@@ -118,7 +118,7 @@ class UserLostPassHandler extends Handler {
if (!udoc) throw new UserNotFoundError(mail);
const [tid] = await token.add(
token.TYPE_LOSTPASS,
await system.get('lostpass_token_expire_seconds') as number,
await system.get('lostpass_token_expire_seconds'),
{ uid: udoc._id },
);
const m = await this.renderHTML('user_lostpass_mail', { url: `lostpass/${tid}`, uname: udoc.uname });
+23 -1
View File
@@ -9,7 +9,29 @@ export type NumberKeys<O> = {
export interface System {
_id: string,
value: string | number,
value: any,
}
export interface SystemKeys {
'smtp.user': string,
'smtp.from': string,
'smtp.pass': string,
'smtp.host': string,
'smtp.port': number,
'smtp.secure': boolean,
'user': number,
'server.url': string,
'server.xff': string,
'server.worker': number,
'server.port': number,
'session.keys': string[],
'session.secure': boolean,
'session.expire_seconds': number,
'session.unsaved_expire_seconds': number,
'lostpass_token_expire_seconds': number,
'registration_token_expire_seconds': number,
'changemail_token_expire_seconds': number,
'user.quota': number,
}
export interface Setting {
+1 -1
View File
@@ -12,7 +12,7 @@ export async function sendMail(to: string, subject: string, text: string, html:
system.get('smtp.user'),
system.get('smtp.pass'),
system.get('smtp.from'),
]) as unknown as [string, number, boolean, string, string, string];
]);
const transporter = nodemailer.createTransport({
host, port, secure, auth: { user, pass },
});
+2
View File
@@ -143,8 +143,10 @@ SystemSetting(
bus.once('app/started', async () => {
for (const setting of SYSTEM_SETTINGS) {
if (setting.value) {
// @ts-ignore
const current = await global.Hydro.model.system.get(setting.key);
if (current === null || current === '') {
// @ts-ignore
await global.Hydro.model.system.set(setting.key, setting.value);
}
}
+26 -5
View File
@@ -1,12 +1,14 @@
import Lru from 'lru-cache';
import { NumberKeys, SystemKeys } from '../interface';
import * as db from '../service/db';
const coll = db.collection('system');
const cache = new Lru<string, string | number>({
const cache = new Lru<string, any>({
maxAge: 5000,
});
export async function get(_id: string): Promise<string | number> {
export async function get<K extends keyof SystemKeys>(_id: K): Promise<SystemKeys[K]> {
const res = cache.get(_id);
if (res !== undefined) return res;
const doc = await coll.findOne({ _id });
@@ -17,6 +19,25 @@ export async function get(_id: string): Promise<string | number> {
return null;
}
export async function getMany<
A extends keyof SystemKeys, B extends keyof SystemKeys,
>(keys: [A, B]): Promise<[SystemKeys[A], SystemKeys[B]]>
export async function getMany<
A extends keyof SystemKeys, B extends keyof SystemKeys, C extends keyof SystemKeys,
>(keys: [A, B, C]): Promise<[SystemKeys[A], SystemKeys[B], SystemKeys[C]]>
export async function getMany<
A extends keyof SystemKeys, B extends keyof SystemKeys, C extends keyof SystemKeys,
D extends keyof SystemKeys,
>(keys: [A, B, C, D]): Promise<[SystemKeys[A], SystemKeys[B], SystemKeys[C], SystemKeys[D]]>
export async function getMany<
A extends keyof SystemKeys, B extends keyof SystemKeys, C extends keyof SystemKeys,
D extends keyof SystemKeys, E extends keyof SystemKeys,
>(keys: [A, B, C, D, E]): Promise<[SystemKeys[A], SystemKeys[B], SystemKeys[C], SystemKeys[D], SystemKeys[E]]>
export async function getMany<
A extends keyof SystemKeys, B extends keyof SystemKeys, C extends keyof SystemKeys,
D extends keyof SystemKeys, E extends keyof SystemKeys, F extends keyof SystemKeys,
>(keys: [A, B, C, D, E, F]): Promise<[SystemKeys[A], SystemKeys[B], SystemKeys[C], SystemKeys[D], SystemKeys[E], SystemKeys[F]]>
export async function getMany(keys: (keyof SystemKeys)[]): Promise<any[]>
export async function getMany(keys: string[]): Promise<any[]> {
const r = [];
let success = true;
@@ -38,7 +59,7 @@ export async function getMany(keys: string[]): Promise<any[]> {
return res;
}
export async function set(_id: string, value: any) {
export async function set<K extends keyof SystemKeys>(_id: K, value: SystemKeys[K]) {
cache.set(_id, value);
const res = await coll.findOneAndUpdate(
{ _id },
@@ -48,7 +69,7 @@ export async function set(_id: string, value: any) {
return res.value.value;
}
export async function inc(_id: string) {
export async function inc<K extends NumberKeys<SystemKeys>>(_id: K) {
const res = await coll.findOneAndUpdate(
{ _id },
// FIXME NumberKeys<>
@@ -57,7 +78,7 @@ export async function inc(_id: string) {
{ upsert: true, returnOriginal: false },
);
cache.set(_id, res.value.value);
return res.value.value as number;
return res.value.value;
}
global.Hydro.model.system = {
+1 -1
View File
@@ -16,7 +16,7 @@
"src"
],
"exclude": [
"__mocks__",
"**/__mocks__",
"bin",
"dist"
]