mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 17:46:28 +08:00
core&ui: implement domain leave and kick
This commit is contained in:
13 files changed
+64
-28
No files matched your search
@@ -11,6 +11,7 @@ import avatar from '../lib/avatar';
|
||||
import { PERM, PERMS_BY_FAMILY, PRIV } from '../model/builtin';
|
||||
import * as discussion from '../model/discussion';
|
||||
import domain from '../model/domain';
|
||||
import MessageModel from '../model/message';
|
||||
import * as oplog from '../model/oplog';
|
||||
import { DOMAIN_SETTINGS, DOMAIN_SETTINGS_BY_KEY } from '../model/setting';
|
||||
import * as system from '../model/system';
|
||||
@@ -163,11 +164,15 @@ class DomainUserHandler extends ManageHandler {
|
||||
};
|
||||
}
|
||||
|
||||
@param('uids', Types.NumericArray)
|
||||
async post({ }, uids: number[]) {
|
||||
if (uids.includes(this.domain.owner)) throw new ForbiddenError();
|
||||
}
|
||||
|
||||
@requireSudo
|
||||
@post('uid', Types.Int)
|
||||
@post('role', Types.Role)
|
||||
async postSetUser(domainId: string, uid: number, role: string) {
|
||||
if (uid === this.domain.owner) throw new ForbiddenError();
|
||||
@param('uids', Types.NumericArray)
|
||||
@param('role', Types.Role)
|
||||
async postSetUsers(domainId: string, uid: number[], role: string) {
|
||||
await Promise.all([
|
||||
domain.setUserRole(domainId, uid, role),
|
||||
oplog.log(this, 'domain.setRole', { uid, role }),
|
||||
@@ -176,14 +181,20 @@ class DomainUserHandler extends ManageHandler {
|
||||
}
|
||||
|
||||
@requireSudo
|
||||
@param('uid', Types.NumericArray)
|
||||
@param('role', Types.Role)
|
||||
async postSetUsers(domainId: string, uid: number[], role: string) {
|
||||
if (uid.includes(this.domain.owner)) throw new ForbiddenError();
|
||||
@param('uids', Types.NumericArray)
|
||||
async postKick({ domainId }, uids: number[]) {
|
||||
const original = await domain.getMultiUserInDomain(domainId, { uid: { $in: uids } }).toArray();
|
||||
const needUpdate = uids.filter((uid) => original.find((i) => i.uid === uid)?.join);
|
||||
if (!needUpdate.length) return;
|
||||
await Promise.all([
|
||||
domain.setUserRole(domainId, uid, role),
|
||||
oplog.log(this, 'domain.setRole', { uid, role }),
|
||||
domain.setJoin(domainId, needUpdate.length > 1 ? needUpdate : needUpdate[0], false),
|
||||
oplog.log(this, 'domain.kick', { uids: needUpdate }),
|
||||
]);
|
||||
const msg = JSON.stringify({
|
||||
message: 'You have been kicked from domain {0} by {1}.',
|
||||
params: [this.domain.name, this.user.uname],
|
||||
});
|
||||
await Promise.all(needUpdate.map((i) => MessageModel.send(1, i, msg, MessageModel.FLAG_RICHTEXT | MessageModel.FLAG_UNREAD)));
|
||||
this.back();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -488,7 +488,7 @@ class HomeDomainHandler extends Handler {
|
||||
} else {
|
||||
for (const ddoc of ddocs) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
const udoc = (await user.getById(ddoc._id, this.user._id))!;
|
||||
const udoc = await user.getById(ddoc._id, this.user._id);
|
||||
canManage[ddoc._id] = udoc.hasPerm(PERM.PERM_EDIT_DOMAIN);
|
||||
role[ddoc._id] = udoc.role;
|
||||
}
|
||||
@@ -504,6 +504,12 @@ class HomeDomainHandler extends Handler {
|
||||
else user.setById(this.user._id, { pinnedDomains: this.user.pinnedDomains.filter((i) => i !== id) });
|
||||
this.back({ star });
|
||||
}
|
||||
|
||||
@param('id', Types.String)
|
||||
async postLeave({ }, id: string) {
|
||||
await domain.setJoin(id, this.user._id, false);
|
||||
this.back();
|
||||
}
|
||||
}
|
||||
|
||||
class HomeDomainCreateHandler extends Handler {
|
||||
|
||||
@@ -135,7 +135,7 @@ class DomainModel {
|
||||
@ArgMethod
|
||||
static async setUserRole(domainId: string, uid: MaybeArray<number>, role: string, autojoin = false) {
|
||||
const update = { $set: { role, ...(autojoin ? { join: true } : {}) } };
|
||||
if (!(uid instanceof Array)) {
|
||||
if (!(Array.isArray(uid))) {
|
||||
const res = await collUser.findOneAndUpdate(
|
||||
{ domainId, uid },
|
||||
update,
|
||||
@@ -152,6 +152,15 @@ class DomainModel {
|
||||
return await collUser.updateMany({ domainId, uid: { $in: uid } }, update, { upsert: true });
|
||||
}
|
||||
|
||||
static async setJoin(domainId: string, uid: MaybeArray<number>, join: boolean) {
|
||||
if (!(Array.isArray(uid))) {
|
||||
await DomainModel.updateUserInDomain(domainId, uid, { $set: { join } });
|
||||
return;
|
||||
}
|
||||
await collUser.updateMany({ domainId, uid: { $in: uid } }, { $set: { join } });
|
||||
deleteUserCache(domainId);
|
||||
}
|
||||
|
||||
static async getRoles(domainId: string, count?: boolean): Promise<any[]>;
|
||||
static async getRoles(domain: DomainDoc, count?: boolean): Promise<any[]>;
|
||||
@ArgMethod
|
||||
|
||||
Reference in new issue
Block a user