mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
core&ui: add new features about domain (#528)
Co-authored-by: undefined <i@undefined.moe> - Add navigation hint - Update homework/contest filter by assign - Allow empty query for DomainSelectAutoComplete
This commit is contained in:
1 parent
9fb85300e7
commit
3581ccb065
12 files changed
+129
-35
No files matched your search
@@ -72,10 +72,13 @@ ScheduleModel.Worker.addHandler('contest', async (doc) => {
|
||||
export class ContestListHandler extends Handler {
|
||||
@param('rule', Types.Range(contest.RULES), true)
|
||||
@param('page', Types.PositiveInt, true)
|
||||
async get(domainId: string, rule = '', page = 1) {
|
||||
@param('all', Types.Boolean)
|
||||
async get(domainId: string, rule = '', page = 1, all = false) {
|
||||
if (rule && contest.RULES[rule].hidden) throw new BadRequestError();
|
||||
const rules = Object.keys(contest.RULES).filter((i) => !contest.RULES[i].hidden);
|
||||
const cursor = contest.getMulti(domainId, rule ? { rule } : { rule: { $in: rules } });
|
||||
if (all && !this.user.hasPerm(PERM.PERM_MOD_BADGE)) all = false;
|
||||
const q = { ...all ? { assign: { $in: [...this.user.group, null] } } : {}, ...rule ? { rule } : { rule: { $in: rules } } };
|
||||
const cursor = contest.getMulti(domainId, q);
|
||||
const qs = rule ? `rule=${rule}` : '';
|
||||
const [tdocs, tpcount] = await paginate<Tdoc>(cursor, page, system.get('pagination.contest'));
|
||||
const tids = [];
|
||||
|
||||
@@ -334,9 +334,14 @@ class DomainJoinHandler extends Handler {
|
||||
}
|
||||
|
||||
class DomainSearchHandler extends Handler {
|
||||
@param('q', Types.Content)
|
||||
async get(domainId: string, q: string) {
|
||||
const ddocs = await domain.getPrefixSearch(q, 20);
|
||||
@param('q', Types.Content, true)
|
||||
async get(domainId: string, q: string = '') {
|
||||
let ddocs: DomainDoc[] = [];
|
||||
if (!q) {
|
||||
const dudict = await domain.getDictUserByDomainId(this.user._id);
|
||||
const dids = Object.keys(dudict);
|
||||
ddocs = await domain.getMulti({ _id: { $in: dids } }).toArray();
|
||||
} else ddocs = await domain.getPrefixSearch(q, 20);
|
||||
for (let i = 0; i < ddocs.length; i++) {
|
||||
ddocs[i].avatarUrl = ddocs[i].avatar ? avatar(ddocs[i].avatar, 64) : '/img/team_avatar.png';
|
||||
}
|
||||
|
||||
@@ -438,6 +438,18 @@ class HomeDomainHandler extends Handler {
|
||||
this.response.template = 'home_domain.html';
|
||||
this.response.body = { ddocs, dudict, canManage };
|
||||
}
|
||||
|
||||
@param('id', Types.String)
|
||||
async postStar(domainId: string, id: string) {
|
||||
await user.setById(this.user._id, { pinnedDomains: this.user.pinnedDomains.concat(id) });
|
||||
this.back({ star: true });
|
||||
}
|
||||
|
||||
@param('id', Types.String)
|
||||
async postUnstar(domainId: string, id: string) {
|
||||
await user.setById(this.user._id, { pinnedDomains: this.user.pinnedDomains.filter((i) => i !== id) });
|
||||
this.back({ star: false });
|
||||
}
|
||||
}
|
||||
|
||||
class HomeDomainCreateHandler extends Handler {
|
||||
@@ -455,8 +467,11 @@ class HomeDomainCreateHandler extends Handler {
|
||||
if (doc) throw new DomainAlreadyExistsError(id);
|
||||
avatar = avatar || this.user.avatar || `gravatar:${this.user.mail}`;
|
||||
const domainId = await domain.add(id, this.user._id, name, bulletin);
|
||||
await domain.edit(domainId, { avatar });
|
||||
await domain.setUserRole(domainId, this.user._id, 'root');
|
||||
await Promise.all([
|
||||
domain.edit(domainId, { avatar }),
|
||||
domain.setUserRole(domainId, this.user._id, 'root'),
|
||||
user.setById(this.user._id, undefined, undefined, { pinnedDomains: [domainId] }),
|
||||
]);
|
||||
this.response.redirect = this.url('domain_dashboard', { domainId });
|
||||
this.response.body = { domainId };
|
||||
}
|
||||
|
||||
@@ -22,8 +22,10 @@ const convertPenaltyRules = validatePenaltyRules;
|
||||
|
||||
class HomeworkMainHandler extends Handler {
|
||||
@param('page', Types.PositiveInt, true)
|
||||
async get(domainId: string, page = 1) {
|
||||
const cursor = contest.getMulti(domainId, { rule: 'homework' });
|
||||
@param('all', Types.Boolean)
|
||||
async get(domainId: string, page = 1, all = false) {
|
||||
if (all && !this.user.hasPerm(PERM.PERM_MOD_BADGE)) all = false;
|
||||
const cursor = contest.getMulti(domainId, { rule: 'homework', ...all ? { assign: { $in: [...this.user.group, null] } } : {} });
|
||||
const [tdocs, tpcount] = await paginate<Tdoc>(cursor, page, system.get('pagination.contest'));
|
||||
const calendar = [];
|
||||
for (const tdoc of tdocs) {
|
||||
|
||||
@@ -200,6 +200,7 @@ AccountSetting(
|
||||
Setting('setting_storage', 'unreadMsg', 0, 'number', 'Unread Message Count', null, FLAG_DISABLED | FLAG_HIDDEN),
|
||||
Setting('setting_storage', 'badge', '', 'text', 'badge info', null, FLAG_DISABLED | FLAG_HIDDEN),
|
||||
Setting('setting_storage', 'banReason', '', 'text', 'ban reason', null, FLAG_DISABLED | FLAG_HIDDEN),
|
||||
Setting('setting_storage', 'pinnedDomains', [], 'json', 'pinned domains', null, FLAG_DISABLED | FLAG_HIDDEN),
|
||||
);
|
||||
|
||||
DomainSetting(
|
||||
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
Authenticator, BaseUserDict, FileInfo, GDoc,
|
||||
ownerInfo, Udict, Udoc, VUdoc,
|
||||
} from '../interface';
|
||||
import avatar from '../lib/avatar';
|
||||
import pwhash from '../lib/hash.hydro';
|
||||
import serializer from '../lib/serializer';
|
||||
import * as bus from '../service/bus';
|
||||
@@ -96,9 +97,10 @@ export class User {
|
||||
this.perm = dudoc.perm || 0n; // This is a fallback for unknown user
|
||||
this.scope = typeof scope === 'string' ? BigInt(scope) : scope;
|
||||
this.role = dudoc.role || 'default';
|
||||
this.domains = udoc.domains || [];
|
||||
this.tfa = !!udoc.tfa;
|
||||
this.authn = (udoc.authenticators || []).length > 0;
|
||||
if (dudoc.group) this.group = [...dudoc.group, this._id.toString()];
|
||||
if (dudoc.group) this.group = dudoc.group;
|
||||
|
||||
for (const key in setting.SETTINGS_BY_KEY) {
|
||||
this[key] = udoc[key] ?? (setting.SETTINGS_BY_KEY[key].value || system.get(`preference.${key}`));
|
||||
@@ -154,6 +156,11 @@ export class User {
|
||||
|
||||
async private() {
|
||||
const user = await new User(this._udoc, this._dudoc, this.scope).init();
|
||||
user.avatarUrl = avatar(user.avatar, 128);
|
||||
if (user.pinnedDomains instanceof Array) {
|
||||
const result = await Promise.allSettled(user.pinnedDomains.slice(0, 10).map((i) => domain.get(i)));
|
||||
user.domains = result.map((i) => (i.status === 'fulfilled' ? i.value : null)).filter((i) => i);
|
||||
}
|
||||
user._isPrivate = true;
|
||||
return user;
|
||||
}
|
||||
@@ -251,11 +258,12 @@ class UserModel {
|
||||
}
|
||||
|
||||
@ArgMethod
|
||||
static async setById(uid: number, $set?: Partial<Udoc>, $unset?: Value<Partial<Udoc>, ''>) {
|
||||
static async setById(uid: number, $set?: Partial<Udoc>, $unset?: Value<Partial<Udoc>, ''>, $push?: any) {
|
||||
if (uid < -999) return null;
|
||||
const op: any = {};
|
||||
if ($set && Object.keys($set).length) op.$set = $set;
|
||||
if ($unset && Object.keys($unset).length) op.$unset = $unset;
|
||||
if ($push && Object.keys($push).length) op.$push = $push;
|
||||
if (op.$set?.loginip) op.$addToSet = { ip: op.$set.loginip };
|
||||
const res = await coll.findOneAndUpdate({ _id: uid }, op, { returnDocument: 'after' });
|
||||
deleteUserCache(res.value);
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { NotFoundError } from '../../error';
|
||||
import avatar from '../../lib/avatar';
|
||||
import { PERM } from '../../model/builtin';
|
||||
import UserModel from '../../model/user';
|
||||
import type { KoaContext } from '../server';
|
||||
@@ -15,7 +14,6 @@ export default async (ctx: KoaContext, next) => {
|
||||
user = await UserModel.getById(domainId, ctx.session.uid, ctx.session.scope);
|
||||
}
|
||||
if (user._id === 0) delete user.viewLang;
|
||||
user.avatarUrl = avatar(user.avatar, 128);
|
||||
ctx.HydroContext.user = await user.private();
|
||||
if (!domain) {
|
||||
ctx.pendingError = new NotFoundError(args.domainId);
|
||||
|
||||
@@ -44,7 +44,7 @@ DomainSelectAutoComplete.defaultProps = {
|
||||
listStyle: {},
|
||||
multi: false,
|
||||
selectedKeys: [],
|
||||
allowEmptyQuery: false,
|
||||
allowEmptyQuery: true,
|
||||
freeSolo: false,
|
||||
freeSoloConverter: (input) => input,
|
||||
};
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import $ from 'jquery';
|
||||
import _ from 'lodash';
|
||||
import DomainSelectAutoComplete from 'vj/components/autocomplete/DomainSelectAutoComplete';
|
||||
import { ActionDialog, ConfirmDialog } from 'vj/components/dialog';
|
||||
import Dropdown from 'vj/components/dropdown/Dropdown';
|
||||
import createHint from 'vj/components/hint';
|
||||
@@ -176,19 +177,35 @@ async function handleOperation(operation) {
|
||||
}).open();
|
||||
if (action !== 'yes') return;
|
||||
} else if (operation === 'copy') {
|
||||
const action = await new ActionDialog({
|
||||
$body: tpl`
|
||||
<div class="typo">
|
||||
<label>
|
||||
${i18n('Target')}
|
||||
<div class="textbox-container">
|
||||
<input name="target" type="text" class="textbox" data-autofocus>
|
||||
</div>
|
||||
</label>
|
||||
$(tpl`
|
||||
<div style="display: none" class="dialog__body--problem-copy">
|
||||
<div class="row"><div class="columns">
|
||||
<h1 name="select_user_hint">${i18n('Copy Problems')}</h1>
|
||||
</div></div>
|
||||
<div class="row">
|
||||
<div class="columns">
|
||||
<label>
|
||||
${i18n('Target')}
|
||||
<div class="textbox-container">
|
||||
<input name="target" type="text" class="textbox" data-autofocus>
|
||||
</div>
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
`,
|
||||
</div>
|
||||
`).appendTo(document.body);
|
||||
const domainSelector = DomainSelectAutoComplete.getOrConstruct($('.dialog__body--problem-copy [name="target"]'));
|
||||
const copyDialog = await new ActionDialog({
|
||||
$body: $('.dialog__body--problem-copy > div'),
|
||||
onDispatch(action) {
|
||||
if (action === 'ok' && domainSelector.value() === null) {
|
||||
domainSelector.focus();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
}).open();
|
||||
if (action !== 'ok') return;
|
||||
if (copyDialog !== 'ok') return;
|
||||
const target = $('[name="target"]').val();
|
||||
if (!target) return;
|
||||
payload.target = target;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import $ from 'jquery';
|
||||
import DomainSelectAutoComplete from 'vj/components/autocomplete/DomainSelectAutoComplete';
|
||||
import { ActionDialog, ConfirmDialog } from 'vj/components/dialog';
|
||||
import Notification from 'vj/components/notification';
|
||||
import { NamedPage } from 'vj/misc/Page';
|
||||
@@ -22,17 +23,31 @@ const page = new NamedPage([
|
||||
});
|
||||
});
|
||||
$(document).on('click', '[name="problem-sidebar__copy"]', () => {
|
||||
new ActionDialog({
|
||||
$body: tpl`
|
||||
<div class="typo">
|
||||
<label>
|
||||
${i18n('Target')}
|
||||
<div class="textbox-container">
|
||||
<input name="target" type="text" class="textbox" data-autofocus>
|
||||
</div>
|
||||
</label>
|
||||
$(tpl`<div style="display: none" class="dialog__body--problem-copy">
|
||||
<div class="row"><div class="columns">
|
||||
<h1 name="select_user_hint">${i18n('Copy Problem')}</h1>
|
||||
</div></div>
|
||||
<div class="row">
|
||||
<div class="columns">
|
||||
<label>
|
||||
${i18n('Target')}
|
||||
<div class="textbox-container">
|
||||
<input name="target" type="text" class="textbox" data-autofocus>
|
||||
</div>
|
||||
`,
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>`).appendTo(document.body);
|
||||
const domainSelector = DomainSelectAutoComplete.getOrConstruct($('.dialog__body--problem-copy [name="target"]')) as any;
|
||||
new ActionDialog({
|
||||
$body: $('.dialog__body--problem-copy > div'),
|
||||
onDispatch(action) {
|
||||
if (action === 'ok' && domainSelector.value() === null) {
|
||||
domainSelector.focus();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
}).open().then(async (action) => {
|
||||
if (action !== 'ok') return;
|
||||
const target = $('[name="target"]').val();
|
||||
|
||||
@@ -33,6 +33,15 @@
|
||||
<img src="{{ avatarUrl(ddoc.avatar, 32) }}" width="32" height="32">
|
||||
</td>
|
||||
<td class="col--name">
|
||||
<form class="form--inline" method="post">
|
||||
<input type="hidden" name="id" value="{{ ddoc['_id'] }}">
|
||||
{% set _show = handler.user.pinnedDomains.includes(ddoc._id) %}
|
||||
<input type="hidden" name="operation" value="{{ "unstar" if _show else "star" }}">
|
||||
<button class="star{% if _show %} activated{% endif %}" type="submit">
|
||||
<span class="starred--hide"><span class="icon icon-star--outline" data-tooltip="{{ _('Star') }}"></span></span>
|
||||
<span class="starred--show"><span class="icon icon-star" data-tooltip="{{ _('Unstar') }}"></span></span>
|
||||
</button>
|
||||
</form>
|
||||
{{ ddoc['name'] }} ({{ ddoc['_id'] }})
|
||||
</td>
|
||||
<td class="col--role">
|
||||
|
||||
@@ -31,6 +31,27 @@
|
||||
</ol>
|
||||
</li>
|
||||
{% else %}
|
||||
<li class="nav__list-item" data-dropdown-pos="bottom right" data-dropdown-custom-class="nav__dropdown" data-dropdown-target="#menu-nav-domain" data-dropdown-disabledconstrainToWindow data-dropdown-trigger-desktop-only>
|
||||
<span class="nav__item"><span class="icon"><img class="small user-profile-avatar v-center" loading="lazy" src="{{ avatarUrl(UiContext.domain.avatar|default('')) }}" width="20" height="20"></span> {{ UiContext.domain.name }} <span class="icon icon-expand_more nojs--hide"></span></span>
|
||||
<ol class="dropdown-target menu" id="menu-nav-domain">
|
||||
{%- if handler.user.domains|length -%}
|
||||
{%- for ddoc in handler.user.domains -%}
|
||||
<li class="menu__item">
|
||||
<a href="{{ url('homepage', domainId=ddoc['_id']) }}" class="menu__link">
|
||||
<span class="icon"><img class="small user-profile-avatar v-center" loading="lazy" src="{{ avatarUrl(ddoc.avatar|default('')) }}" width="20" height="20"></span>
|
||||
{{ ddoc['name'] }}{% if ddoc['name'] != ddoc['_id']%} ({{ ddoc['_id'] }}){% endif %}
|
||||
</a>
|
||||
</li>
|
||||
{%- endfor -%}
|
||||
<li class="menu__seperator"></li>
|
||||
{%- endif -%}
|
||||
<li class="menu__item">
|
||||
<a href="{{ url('home_domain') }}" class="menu__link">
|
||||
<span class="icon icon-wrench"></span> {{ _('Manage') }}
|
||||
</a>
|
||||
</li>
|
||||
</ol>
|
||||
</li>
|
||||
<li class="nav__list-item" data-dropdown-pos="bottom right" data-dropdown-custom-class="nav__dropdown" data-dropdown-target="#menu-nav-user" data-dropdown-disabledconstrainToWindow data-dropdown-trigger-desktop-only>
|
||||
<a href="{{ url('user_detail', uid=handler.user._id) }}" class="nav__item">{{ handler.user.uname }} <span class="icon icon-expand_more nojs--hide"></span></a>
|
||||
<ol class="dropdown-target menu" id="menu-nav-user">
|
||||
|
||||
Reference in new issue
Block a user