fix: fix append group member roles will occur error when operate multi users

pull/105/merge
moonrailgun 2 years ago
parent 967e671521
commit 52647a98b5

@ -320,29 +320,24 @@ export class Group extends TimeStamps implements Base {
} }
/** /**
* *
*
*
*/ */
static async updateGroupMemberField< static async checkGroupFieldPermission<
K extends keyof Pick<GroupMember, 'roles' | 'muteUntil'> K extends keyof Pick<GroupMember, 'roles' | 'muteUntil'>
>( >(
this: ReturnModelType<typeof Group>, this: ReturnModelType<typeof Group>,
ctx: TcContext, ctx: TcContext,
groupId: string, groupId: string,
memberId: string, fieldName: K
fieldName: K, ) {
fieldValue: GroupMember[K] | ((member: GroupMember) => void), const userId = ctx.meta.userId;
operatorUserId: string
): Promise<Group> {
const group = await this.findById(groupId);
const t = ctx.meta.t; const t = ctx.meta.t;
if (fieldName === 'roles') { if (fieldName === 'roles') {
// 检查操作用户是否有管理角色的权限 // 检查操作用户是否有管理角色的权限
const [hasRolePermission] = await call(ctx).checkUserPermissions( const [hasRolePermission] = await call(ctx).checkUserPermissions(
groupId, groupId,
operatorUserId, userId,
[PERMISSION.core.manageRoles] [PERMISSION.core.manageRoles]
); );
if (!hasRolePermission) { if (!hasRolePermission) {
@ -352,13 +347,34 @@ export class Group extends TimeStamps implements Base {
// 检查操作用户是否有管理用户权限 // 检查操作用户是否有管理用户权限
const [hasUserPermission] = await call(ctx).checkUserPermissions( const [hasUserPermission] = await call(ctx).checkUserPermissions(
groupId, groupId,
operatorUserId, userId,
[PERMISSION.core.manageUser] [PERMISSION.core.manageUser]
); );
if (!hasUserPermission) { if (!hasUserPermission) {
throw new NoPermissionError(t('没有操作用户权限')); throw new NoPermissionError(t('没有操作用户权限'));
} }
} }
}
/**
*
*
*
*/
static async updateGroupMemberField<
K extends keyof Pick<GroupMember, 'roles' | 'muteUntil'>
>(
this: ReturnModelType<typeof Group>,
ctx: TcContext,
groupId: string,
memberId: string,
fieldName: K,
fieldValue: GroupMember[K] | ((member: GroupMember) => void)
): Promise<Group> {
const group = await this.findById(groupId);
const t = ctx.meta.t;
await this.checkGroupFieldPermission(ctx, groupId, fieldName);
const member = group.members.find((m) => String(m.userId) === memberId); const member = group.members.find((m) => String(m.userId) === memberId);
if (!member) { if (!member) {

@ -22,6 +22,7 @@ import {
PanelFeature, PanelFeature,
config, config,
SYSTEM_USERID, SYSTEM_USERID,
db,
} from 'tailchat-server-sdk'; } from 'tailchat-server-sdk';
import moment from 'moment'; import moment from 'moment';
@ -655,20 +656,25 @@ class GroupService extends TcService {
}> }>
) { ) {
const { groupId, memberIds, roles } = ctx.params; const { groupId, memberIds, roles } = ctx.params;
const { userId } = ctx.meta;
await Promise.all( await this.adapter.model.checkGroupFieldPermission(ctx, groupId, 'roles');
memberIds.map((memberId) =>
this.adapter.model.updateGroupMemberField( // 更新内容
ctx, await this.adapter.model.updateMany(
groupId, {
memberId, _id: new db.Types.ObjectId(groupId),
'roles', 'members.userId': {
(member) => $in: [...memberIds],
(member['roles'] = _.uniq([...member['roles'], ...roles])), },
userId },
) {
) $addToSet: {
'members.$[elem].roles': {
$each: roles,
},
},
},
{ arrayFilters: [{ 'elem.userId': { $in: [...memberIds] } }] }
); );
const group = await this.adapter.model.findById(groupId); const group = await this.adapter.model.findById(groupId);
@ -692,19 +698,25 @@ class GroupService extends TcService {
}> }>
) { ) {
const { groupId, memberIds, roles } = ctx.params; const { groupId, memberIds, roles } = ctx.params;
const { userId } = ctx.meta;
await Promise.all( await this.adapter.model.checkGroupFieldPermission(ctx, groupId, 'roles');
memberIds.map((memberId) =>
this.adapter.model.updateGroupMemberField( // 更新内容
ctx, await this.adapter.model.updateMany(
groupId, {
memberId, _id: new db.Types.ObjectId(groupId),
'roles', 'members.userId': {
(member) => (member['roles'] = _.without(member['roles'], ...roles)), $in: [...memberIds],
userId },
) },
) {
$pull: {
'members.$[elem].roles': {
$in: roles,
},
},
},
{ arrayFilters: [{ 'elem.userId': { $in: [...memberIds] } }] }
); );
const group = await this.adapter.model.findById(groupId); const group = await this.adapter.model.findById(groupId);
@ -1112,7 +1124,6 @@ class GroupService extends TcService {
}> }>
) { ) {
const { groupId, memberId, muteMs } = ctx.params; const { groupId, memberId, muteMs } = ctx.params;
const userId = ctx.meta.userId;
const language = ctx.meta.language; const language = ctx.meta.language;
const isUnmute = muteMs < 0; const isUnmute = muteMs < 0;
@ -1121,8 +1132,7 @@ class GroupService extends TcService {
groupId, groupId,
memberId, memberId,
'muteUntil', 'muteUntil',
isUnmute ? undefined : new Date(new Date().valueOf() + muteMs), isUnmute ? undefined : new Date(new Date().valueOf() + muteMs)
userId
); );
this.notifyGroupInfoUpdate(ctx, group); this.notifyGroupInfoUpdate(ctx, group);

Loading…
Cancel
Save