core: add hooks for DomainDoc

This commit is contained in:
undefined committed 2020-10-30 15:09:09 +08:00
1 parent b3aa0a5e09
commit 508e42b0eb
3 files changed
+21 -4

No files matched your search

+1
View File
@@ -43,6 +43,7 @@ langs:
type: compiler
compile: /usr/bin/javac -d ${dir} -encoding utf8 ./Main.java
code_file: Main.java
target: Main.class
execute: /usr/bin/java Main
py:
type: compiler
+13 -4
View File
@@ -2,6 +2,7 @@ import { Dictionary } from 'lodash';
import { FilterQuery } from 'mongodb';
import { BUILTIN_ROLES, PRIV } from './builtin';
import { DomainDoc } from '../interface';
import * as bus from '../service/bus';
import * as db from '../service/db';
const coll = db.collection('domain');
@@ -39,20 +40,28 @@ export async function add(domainId: string, owner: number, name: string, bulleti
gravatar: '',
pidCounter: 0,
};
await bus.serial('domain/create', ddoc);
await coll.insertOne(ddoc);
return domainId;
}
export function get(domainId: string): Promise<DomainDoc | null> {
return coll.findOne({ _id: domainId });
export async function get(domainId: string): Promise<DomainDoc | null> {
const query: FilterQuery<DomainDoc> = { _id: domainId };
await bus.serial('domain/before-get', query);
const result = await coll.findOne(query);
await bus.serial('domain/get', result);
return result;
}
export function getMulti(query: FilterQuery<DomainDoc> = {}) {
return coll.find(query);
}
export function edit(domainId: string, $set: Partial<DomainDoc>) {
return coll.updateOne({ _id: domainId }, { $set });
export async function edit(domainId: string, $set: Partial<DomainDoc>) {
await bus.serial('domain/before-update', domainId, $set);
const result = await coll.findOneAndUpdate({ _id: domainId }, { $set }, { returnOriginal: false });
await bus.serial('domain/update', domainId, $set, result.value);
return result.value;
}
export async function inc(domainId: string, field: keyof DomainDoc, n: number): Promise<number | null> {
+7
View File
@@ -6,6 +6,7 @@ import { Logger } from '../logger';
import {
Mdoc, Pdoc, Rdoc, TrainingDoc, User,
} from '../interface';
import { DomainDoc } from '../loader';
type DocType = import('../model/document').DocType;
@@ -39,6 +40,12 @@ export interface EventMap {
'user/message': (uid: number, mdoc: Mdoc, udoc: User) => void
'user/get': (udoc: User) => void
'domain/create': (ddoc: DomainDoc) => void
'domain/before-get': (query: FilterQuery<DomainDoc>) => void
'domain/get': (ddoc: DomainDoc) => void
'domain/before-update': (domainId: string, $set: Partial<DomainDoc>) => void
'domain/update': (domainId: string, $set: Partial<DomainDoc>, ddoc: DomainDoc) => void
'document/add': (doc: any) => VoidReturn
'document/set': <T extends keyof DocType>
(domainId: string, docType: T, docId: DocType[T], $set: any) => VoidReturn