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

pull/10/head
undefined 4 years ago
parent 147637d539
commit 6ba93841ea

@ -1,4 +1,5 @@
dist
*.d.ts
*.js
test
test
tests

@ -10,7 +10,8 @@
"lint": "eslint packages --ext ts --fix",
"pub": "node build/publish",
"jest": "node build/jest",
"start": "node --async-stack-traces --trace-deprecation --enable-source-maps build/start"
"start": "node --async-stack-traces --trace-deprecation --enable-source-maps build/start",
"clean": "zsh -c \"rm -r tsconfig.build.tsbuildinfo ./packages/*/tsconfig.build.tsbuildinfo ./packages/*/dist ./coverage\""
},
"version": "1.0.0",
"license": "AGPL-3.0-only",

@ -7,7 +7,7 @@ import { compilerText } from './utils';
export = async function compile(
lang: string, code: string, target: string, copyIn: any, next?: Function,
) {
const LANGS = yaml.safeLoad(await global.Hydro.model.system.get('hydrojudge.langs') as string);
const LANGS = yaml.safeLoad(await global.Hydro.model.system.get('hydrojudge.langs'));
if (!LANGS[lang]) throw new SystemError(`不支持的语言:${lang}`);
const info = LANGS[lang];
const f = {};

@ -8,6 +8,12 @@ import { ObjectID } from 'bson';
import fs from 'fs-extra';
import { homedir, tmpdir } from 'os';
declare module 'hydrooj' {
interface SystemKeys {
'hydrojudge.langs': string,
}
}
async function postInit() {
// Only start a single daemon
if (!cluster.isFirstWorker) return;

@ -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', {

@ -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])));
}
}

@ -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 });

@ -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 {

@ -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 },
});

@ -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);
}
}

@ -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 = {

@ -16,7 +16,7 @@
"src"
],
"exclude": [
"__mocks__",
"**/__mocks__",
"bin",
"dist"
]

@ -4,6 +4,14 @@ import superagentProxy from 'superagent-proxy';
superagentProxy(superagent);
declare module 'hydrooj' {
interface SystemKeys {
'login-with-github.id': string,
'login-with-github.secret': string,
'login-with-github.proxy': string,
}
}
async function get() {
const { system, token } = global.Hydro.model;
const [appid, [state]] = await Promise.all([

@ -4,6 +4,14 @@ import proxy from 'superagent-proxy';
proxy(superagent);
declare module 'hydrooj' {
interface SystemKeys {
'login-with-google.id': string,
'login-with-google.secret': string,
'login-with-google.proxy': string,
}
}
async function get() {
const { system, token } = global.Hydro.model;
const [appid, url, [state]] = await Promise.all([
@ -26,7 +34,7 @@ async function callback({
s,
] = await Promise.all([
system.getMany([
'login-with-google.id', 'oauth.googlesecret', 'server.url', 'proxy',
'login-with-google.id', 'login-with-google.secret', 'server.url', 'login-with-google.proxy',
]),
token.get(state, token.TYPE_OAUTH),
]);

@ -1,6 +1,6 @@
{
"name": "@hydrooj/login-with-google",
"version": "0.0.7",
"version": "0.0.8",
"main": "package.json",
"repository": "git@github.com:hydro-dev/Hydro.git",
"author": "undefined <masnn0@outlook.com>",

@ -4,6 +4,14 @@ import superagentProxy from 'superagent-proxy';
superagentProxy(superagent);
declare module 'hydrooj' {
interface SystemKeys {
'login-with-osu.id': string,
'login-with-osu.secret': string,
'login-with-osu.proxy': string,
}
}
const BASE_URL = 'https://osu.ppy.sh/';
async function get() {

@ -2,6 +2,14 @@ import { } from 'hydrooj';
import * as bus from 'hydrooj/dist/service/bus';
import WebSocket from 'ws';
declare module 'hydrooj' {
interface SystemKeys {
'login-with-qq.id': string,
'login-with-qq.url': string,
'login-with-qq.token': string,
}
}
export async function postInit() {
const { system, token } = global.Hydro.model;
const [url, accessToken] = await system.getMany(['login-with-qq.url', 'login-with-qq.token']);

Loading…
Cancel
Save