mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core&ui: contest balloon notice (#1071)
This commit is contained in:
1 parent
6d2442c6a3
commit
053978d9d4
12 files changed
+135
-63
No files matched your search
@@ -28,6 +28,7 @@ __langname: 简体中文
|
||||
'Copy failed :(': '复制失败 :('
|
||||
'Current dataset: {0}': '当前测试数据: {0}'
|
||||
'Discussion: Terms Of Service': 讨论区服务条款
|
||||
'First Blood Notice\n{0} solved problem {1} ({2})': '题目一血提示\n{0} 解决了题目 {1} ({2})'
|
||||
'Format: category 1, sub category 1.1, sub category 1.2, ..., sub category 1.x, ..., category n, sub category n.1, sub category n.2, ..., sub category n.m, ...': 格式:分类1, 子分类1.1, 子分类1.2, ..., 子分类1.x, ..., 分类n, 子分类n.1, 子分类n.2, ..., 子分类n.m, ...
|
||||
'Join {0}': '加入 {0}'
|
||||
'No': 否
|
||||
|
||||
@@ -568,19 +568,22 @@ class HomeMessagesHandler extends Handler {
|
||||
const messages = await message.getByUser(this.user._id);
|
||||
const uids = new Set<number>([
|
||||
...messages.map((mdoc) => mdoc.from),
|
||||
...messages.map((mdoc) => mdoc.to),
|
||||
...messages.flatMap((mdoc) => mdoc.to),
|
||||
]);
|
||||
const udict = await user.getList('system', Array.from(uids));
|
||||
// TODO(twd2): improve here:
|
||||
const parsed = {};
|
||||
for (const m of messages) {
|
||||
const target = m.from === this.user._id ? m.to : m.from;
|
||||
parsed[target] ||= {
|
||||
_id: target,
|
||||
udoc: { ...udict[target], avatarUrl: avatar(udict[target].avatar) },
|
||||
messages: [],
|
||||
};
|
||||
parsed[target].messages.push(m);
|
||||
const raw = m.from === this.user._id ? m.to : m.from;
|
||||
const targetArr = Array.isArray(raw) ? raw : [raw];
|
||||
for (const target of targetArr) {
|
||||
parsed[target] ||= {
|
||||
_id: target,
|
||||
udoc: { ...udict[target], avatarUrl: avatar(udict[target].avatar) },
|
||||
messages: [],
|
||||
};
|
||||
parsed[target].messages.push(m);
|
||||
}
|
||||
}
|
||||
await user.setById(this.user._id, { unreadMsg: 0 });
|
||||
this.response.body = { messages: parsed };
|
||||
@@ -601,19 +604,10 @@ class HomeMessagesHandler extends Handler {
|
||||
@param('messageId', Types.ObjectId)
|
||||
async postDeleteMessage({ }, messageId: ObjectId) {
|
||||
const msg = await message.get(messageId);
|
||||
if ([msg.from, msg.to].includes(this.user._id)) await message.del(messageId);
|
||||
if (msg.from === this.user._id) await message.del(messageId);
|
||||
else throw new PermissionError();
|
||||
this.back();
|
||||
}
|
||||
|
||||
@param('messageId', Types.ObjectId)
|
||||
async postRead({ }, messageId: ObjectId) {
|
||||
const msg = await message.get(messageId);
|
||||
if ([msg.from, msg.to].includes(this.user._id)) {
|
||||
await message.setFlag(messageId, message.FLAG_UNREAD);
|
||||
} else throw new PermissionError();
|
||||
this.back();
|
||||
}
|
||||
}
|
||||
|
||||
export const inject = { geoip: { required: false }, oauth: {} };
|
||||
|
||||
@@ -174,7 +174,7 @@ class UserWebauthnHandler extends Handler {
|
||||
|
||||
async post({ domainId, result, redirect }) {
|
||||
const challenge = this.session.challenge;
|
||||
if (!challenge) throw new ForbiddenError();
|
||||
if (!challenge) throw new ForbiddenError('no-challenge');
|
||||
const tdoc = await token.get(challenge, token.TYPE_WEBAUTHN);
|
||||
if (!tdoc) throw new InvalidTokenError(token.TYPE_TEXTS[token.TYPE_WEBAUTHN]);
|
||||
const udoc = await (tdoc.uid === 'login'
|
||||
|
||||
@@ -311,7 +311,7 @@ export interface DomainDoc extends Record<string, any> {
|
||||
// Message
|
||||
export interface MessageDoc {
|
||||
from: number;
|
||||
to: number;
|
||||
to: number | number[];
|
||||
content: string;
|
||||
flag: number;
|
||||
}
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { sumBy } from 'lodash';
|
||||
import { Filter, ObjectId } from 'mongodb';
|
||||
import {
|
||||
Counter, formatSeconds, getAlphabeticId, Time,
|
||||
Counter, formatSeconds, getAlphabeticId, sleep, Time,
|
||||
} from '@hydrooj/utils/lib/utils';
|
||||
import { Context } from '../context';
|
||||
import {
|
||||
ContestAlreadyAttendedError, ContestNotFoundError,
|
||||
ContestScoreboardHiddenError, ValidationError,
|
||||
@@ -11,14 +12,17 @@ import {
|
||||
BaseUserDict, ContestPrintDoc, ContestRule, ContestRules, ProblemDict, RecordDoc,
|
||||
ScoreboardConfig, ScoreboardNode, ScoreboardRow, SubtaskResult, Tdoc,
|
||||
} from '../interface';
|
||||
import avatar from '../lib/avatar';
|
||||
import bus from '../service/bus';
|
||||
import db from '../service/db';
|
||||
import type { Handler } from '../service/server';
|
||||
import { Optional } from '../typeutils';
|
||||
import { PERM, STATUS, STATUS_SHORT_TEXTS } from './builtin';
|
||||
import * as document from './document';
|
||||
import problem from './problem';
|
||||
import user, { User } from './user';
|
||||
import MessageModel from './message';
|
||||
import problem, { ProblemModel } from './problem';
|
||||
import RecordModel from './record';
|
||||
import UserModel, { User } from './user';
|
||||
|
||||
export enum PrintTaskStatus {
|
||||
pending = 'pending',
|
||||
@@ -233,7 +237,7 @@ const acm = buildContestRule({
|
||||
async scoreboard(config, _, tdoc, pdict, cursor) {
|
||||
const rankedTsdocs = await db.ranked(cursor, (a, b) => (a.score || 0) === (b.score || 0) && (a.time || 0) === (b.time || 0));
|
||||
const uids = rankedTsdocs.map(([, tsdoc]) => tsdoc.uid);
|
||||
const udict = await user.getListForRender(tdoc.domainId, uids, config.showDisplayName ? ['displayName'] : []);
|
||||
const udict = await UserModel.getListForRender(tdoc.domainId, uids, config.showDisplayName ? ['displayName'] : []);
|
||||
// Find first accept
|
||||
const first = {};
|
||||
const data = await document.collStatus.aggregate([
|
||||
@@ -407,7 +411,7 @@ const oi = buildContestRule({
|
||||
async scoreboard(config, _, tdoc, pdict, cursor) {
|
||||
const rankedTsdocs = await db.ranked(cursor, (a, b) => (a.score || 0) === (b.score || 0));
|
||||
const uids = rankedTsdocs.map(([, tsdoc]) => tsdoc.uid);
|
||||
const udict = await user.getListForRender(tdoc.domainId, uids, config.showDisplayName ? ['displayName'] : []);
|
||||
const udict = await UserModel.getListForRender(tdoc.domainId, uids, config.showDisplayName ? ['displayName'] : []);
|
||||
const psdict = {};
|
||||
const first = {};
|
||||
const useRelativeTime = !!tdoc.duration;
|
||||
@@ -776,7 +780,7 @@ const homework = buildContestRule({
|
||||
async scoreboard(config, _, tdoc, pdict, cursor) {
|
||||
const rankedTsdocs = await db.ranked(cursor, (a, b) => a.score === b.score);
|
||||
const uids = rankedTsdocs.map(([, tsdoc]) => tsdoc.uid);
|
||||
const udict = await user.getListForRender(tdoc.domainId, uids, config.showDisplayName ? ['displayName'] : []);
|
||||
const udict = await UserModel.getListForRender(tdoc.domainId, uids, config.showDisplayName ? ['displayName'] : []);
|
||||
const columns = await this.scoreboardHeader(config, _, tdoc, pdict);
|
||||
const rows: ScoreboardRow[] = [
|
||||
columns,
|
||||
@@ -851,16 +855,30 @@ export async function getRelated(domainId: string, pid: number, rule?: string) {
|
||||
}
|
||||
|
||||
export async function addBalloon(domainId: string, tid: ObjectId, uid: number, rid: ObjectId, pid: number) {
|
||||
const balloon = await collBalloon.findOne({
|
||||
domainId, tid, pid, uid,
|
||||
});
|
||||
if (balloon) return null;
|
||||
const bdcount = await collBalloon.countDocuments({ domainId, tid, pid });
|
||||
const balloon = await collBalloon.find({ domainId, tid, pid }).project({ uid: 1 }).toArray();
|
||||
if (balloon.find((i) => i.uid === uid)) return null;
|
||||
let isFirst = !balloon.length;
|
||||
if (isFirst) {
|
||||
let pending: RecordDoc[] = [];
|
||||
do {
|
||||
if (pending.length) await sleep(500); // eslint-disable-line no-await-in-loop
|
||||
pending = await RecordModel.getMulti(domainId, { // eslint-disable-line no-await-in-loop
|
||||
pid, contest: tid, _id: { $lt: rid }, status: {
|
||||
$in: [
|
||||
STATUS.STATUS_WAITING, STATUS.STATUS_COMPILING,
|
||||
STATUS.STATUS_JUDGING, STATUS.STATUS_FETCHED,
|
||||
STATUS.STATUS_ACCEPTED,
|
||||
],
|
||||
},
|
||||
}).limit(1).toArray();
|
||||
} while (pending.length && !pending.some((i) => i.status === STATUS.STATUS_ACCEPTED));
|
||||
if (pending.some((i) => i.status === STATUS.STATUS_ACCEPTED)) isFirst = false;
|
||||
}
|
||||
const newBdoc = {
|
||||
_id: rid, domainId, tid, pid, uid, ...(!bdcount ? { first: true } : {}),
|
||||
_id: rid, domainId, tid, pid, uid, ...(isFirst ? { first: true } : {}),
|
||||
};
|
||||
await collBalloon.insertOne(newBdoc);
|
||||
bus.broadcast('contest/balloon', domainId, tid, newBdoc);
|
||||
bus.emit('contest/balloon', domainId, tid, newBdoc);
|
||||
return rid;
|
||||
}
|
||||
|
||||
@@ -1090,7 +1108,34 @@ export function getMultiPrintTask(domainId: string, tid: ObjectId, query = {}) {
|
||||
.sort({ _id: 1 });
|
||||
}
|
||||
|
||||
export async function apply(ctx: Context) {
|
||||
ctx.on('contest/balloon', (domainId, tid, bdoc) => {
|
||||
if (!bdoc.first) return;
|
||||
(async () => {
|
||||
const tsdocs = await getMultiStatus(domainId, { docId: tid, subscribe: 1 }).toArray();
|
||||
const uids = Array.from<number>(new Set(tsdocs.map((tsdoc) => tsdoc.uid)));
|
||||
const [team, tdoc, pdoc] = await Promise.all([
|
||||
UserModel.getById(domainId, bdoc.uid),
|
||||
get(domainId, tid),
|
||||
ProblemModel.get(domainId, bdoc.pid),
|
||||
]);
|
||||
await MessageModel.send(1, uids, JSON.stringify({
|
||||
message: 'First Blood Notice\n{0} solved problem {1} ({2})',
|
||||
avatar: avatar(team.avatar),
|
||||
params: [team.uname, getAlphabeticId(tdoc.pids.indexOf(bdoc.pid)), pdoc.title],
|
||||
}), MessageModel.FLAG_I18N);
|
||||
})();
|
||||
});
|
||||
await ctx.db.ensureIndexes(
|
||||
collBalloon,
|
||||
{ key: { domainId: 1, tid: 1, pid: 1, uid: 1 }, unique: true, name: 'basic' },
|
||||
{ key: { domainId: 1, tid: 1, pid: 1 }, unique: true, name: 'first', partialFilterExpression: { first: true } },
|
||||
);
|
||||
}
|
||||
|
||||
global.Hydro.model.contest = {
|
||||
apply,
|
||||
|
||||
RULES,
|
||||
PrintTaskStatus,
|
||||
buildContestRule,
|
||||
|
||||
@@ -22,9 +22,9 @@ class MessageModel {
|
||||
content: string, flag: number = MessageModel.FLAG_UNREAD,
|
||||
) {
|
||||
if (!Array.isArray(to)) to = [to];
|
||||
const base = { from, content, flag };
|
||||
const base = { from, content, flag, to };
|
||||
if (!to.length) return base;
|
||||
await MessageModel.coll.insertMany(to.map((t) => ({ ...base, to: t })));
|
||||
await MessageModel.coll.insertOne(base);
|
||||
bus.broadcast('user/message', to, base);
|
||||
if (flag & MessageModel.FLAG_UNREAD) await user.inc(to, 'unreadMsg', 1);
|
||||
return base;
|
||||
@@ -52,14 +52,6 @@ class MessageModel {
|
||||
.toArray();
|
||||
}
|
||||
|
||||
static async setFlag(messageId: ObjectId, flag: number) {
|
||||
return await MessageModel.coll.findOneAndUpdate(
|
||||
{ _id: messageId },
|
||||
{ $bit: { flag: { xor: flag } } },
|
||||
{ returnDocument: 'after' },
|
||||
);
|
||||
}
|
||||
|
||||
static async del(_id: ObjectId) {
|
||||
return await MessageModel.coll.deleteOne({ _id });
|
||||
}
|
||||
|
||||
@@ -120,7 +120,11 @@ export class MongoService extends Service {
|
||||
index.background = true;
|
||||
if (!i) {
|
||||
logger.info('Indexing %s.%s with key %o', coll.collectionName, index.name, index.key);
|
||||
await coll.createIndexes([index]);
|
||||
try {
|
||||
await coll.createIndexes([index]);
|
||||
} catch (e) {
|
||||
logger.error('Failed to index %s.%s with key %o: %s', coll.collectionName, index.name, index.key, e);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
const isDifferent = () => {
|
||||
@@ -137,7 +141,11 @@ export class MongoService extends Service {
|
||||
}
|
||||
logger.info('Re-Index %s.%s with key %o', coll.collectionName, index.name, index.key);
|
||||
await coll.dropIndex(i.name);
|
||||
await coll.createIndexes([index]);
|
||||
try {
|
||||
await coll.createIndexes([index]);
|
||||
} catch (e) {
|
||||
logger.error('Failed to re-index %s.%s with key %o: %s', coll.collectionName, index.name, index.key, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user