-
Notifications
You must be signed in to change notification settings - Fork 340
/
Copy pathgroup.service.ts
1306 lines (1169 loc) · 30.7 KB
/
group.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import _ from 'lodash';
import { Types } from 'mongoose';
import { isValidStr } from '../../../lib/utils';
import type {
Group,
GroupDocument,
GroupModel,
GroupPanel,
} from '../../../models/group/group';
import {
TcService,
GroupBaseInfo,
TcContext,
TcDbService,
PureContext,
call,
DataNotFoundError,
EntityError,
NoPermissionError,
PERMISSION,
GroupPanelType,
PanelFeature,
config,
SYSTEM_USERID,
db,
} from 'tailchat-server-sdk';
import moment from 'moment';
import type { GroupStruct } from 'tailchat-server-sdk';
interface GroupService
extends TcService,
TcDbService<GroupDocument, GroupModel> {}
class GroupService extends TcService {
get serviceName(): string {
return 'group';
}
onInit(): void {
this.registerLocalDb(require('../../../models/group/group').default);
this.registerAction('createGroup', this.createGroup, {
params: {
name: 'string',
panels: 'array',
},
});
this.registerAction('getUserGroups', this.getUserGroups);
this.registerAction(
'getJoinedGroupAndPanelIds',
this.getJoinedGroupAndPanelIds
);
this.registerAction('getGroupSocketRooms', this.getGroupSocketRooms);
this.registerAction('getGroupBasicInfo', this.getGroupBasicInfo, {
params: {
groupId: 'string',
},
cache: {
keys: ['groupId'],
ttl: 60 * 60, // 1 hour
},
});
this.registerAction('getGroupInfo', this.getGroupInfo, {
params: {
groupId: 'string',
},
cache: {
keys: ['groupId'],
ttl: 60 * 60, // 1 hour
},
visibility: 'public',
});
this.registerAction('updateGroupField', this.updateGroupField, {
params: {
groupId: 'string',
fieldName: 'string',
fieldValue: 'any',
},
});
this.registerAction('updateGroupConfig', this.updateGroupConfig, {
params: {
groupId: 'string',
configName: 'string',
configValue: 'any',
},
});
this.registerAction('isGroupOwner', this.isGroupOwner, {
params: {
groupId: 'string',
},
});
this.registerAction('addMember', this.addMember, {
params: {
groupId: 'string',
userId: 'string',
},
visibility: 'public',
});
/**
* 加入群组
* @deprecated 请尽量使用 addMember
*/
this.registerAction('joinGroup', this.joinGroup, {
params: {
groupId: 'string',
},
visibility: 'public',
});
this.registerAction('quitGroup', this.quitGroup, {
params: {
groupId: 'string',
},
});
this.registerAction('isMember', this.isMember, {
params: {
groupId: 'string',
},
});
this.registerAction('appendGroupMemberRoles', this.appendGroupMemberRoles, {
params: {
groupId: 'string',
memberIds: { type: 'array', items: 'string' },
roles: { type: 'array', items: 'string' },
},
});
this.registerAction('removeGroupMemberRoles', this.removeGroupMemberRoles, {
params: {
groupId: 'string',
memberIds: { type: 'array', items: 'string' },
roles: { type: 'array', items: 'string' },
},
});
this.registerAction('createGroupPanel', this.createGroupPanel, {
params: {
groupId: 'string',
name: 'string',
type: 'number',
parentId: { type: 'string', optional: true },
provider: { type: 'string', optional: true },
pluginPanelName: { type: 'string', optional: true },
meta: { type: 'object', optional: true },
},
});
this.registerAction('modifyGroupPanel', this.modifyGroupPanel, {
params: {
groupId: 'string',
panelId: 'string',
name: 'string',
type: 'number',
provider: { type: 'string', optional: true },
pluginPanelName: { type: 'string', optional: true },
meta: { type: 'object', optional: true },
},
});
this.registerAction('deleteGroupPanel', this.deleteGroupPanel, {
params: {
groupId: 'string',
panelId: 'string',
},
});
this.registerAction(
'getGroupLobbyConverseId',
this.getGroupLobbyConverseId,
{
params: {
groupId: 'string',
},
}
);
this.registerAction('createGroupRole', this.createGroupRole, {
params: {
groupId: 'string',
roleName: 'string',
permissions: { type: 'array', items: 'string' },
},
});
this.registerAction('deleteGroupRole', this.deleteGroupRole, {
params: {
groupId: 'string',
roleId: 'string',
},
});
this.registerAction('updateGroupRoleName', this.updateGroupRoleName, {
params: {
groupId: 'string',
roleId: 'string',
roleName: 'string',
},
});
this.registerAction(
'updateGroupRolePermission',
this.updateGroupRolePermission,
{
params: {
groupId: 'string',
roleId: 'string',
permissions: {
type: 'array',
items: 'string',
},
},
}
);
this.registerAction('getPermissions', this.getPermissions, {
params: {
groupId: 'string',
},
});
this.registerAction('getUserAllPermissions', this.getUserAllPermissions, {
params: {
groupId: 'string',
userId: 'string',
},
visibility: 'public',
cache: {
keys: ['groupId', 'userId'],
ttl: 60 * 60, // 1 hour
},
});
this.registerAction('muteGroupMember', this.muteGroupMember, {
params: {
groupId: 'string',
memberId: 'string',
muteMs: 'number',
},
});
this.registerAction('deleteGroupMember', this.deleteGroupMember, {
params: {
groupId: 'string',
memberId: 'string',
},
});
}
/**
* 获取会被订阅的群组面板id列表
*
* 订阅即加入socket房间
*/
private getSubscribedGroupPanelIds(group: GroupStruct): {
textPanelIds: string[];
subscribeFeaturePanelIds: string[];
} {
const textPanelIds = this.getGroupTextPanelIds(group);
const subscribeFeaturePanelIds = this.getGroupPanelIdsWithFeature(
group,
'subscribe'
);
return {
textPanelIds,
subscribeFeaturePanelIds,
};
}
/**
* 获取群组所有的文字面板id列表
* 用于加入房间
*/
private getGroupTextPanelIds(group: GroupStruct): string[] {
// TODO: 先无视权限, 把所有的信息全部显示
const textPanelIds = group.panels
.filter((p) => p.type === GroupPanelType.TEXT)
.map((p) => p.id);
return textPanelIds;
}
/**
* 获取群组中拥有某些特性的面板
* @param group
*/
private getGroupPanelIdsWithFeature(
group: GroupStruct,
feature: PanelFeature
): string[] {
const featureAllPanelNames = this.getPanelNamesWithFeature(feature);
const matchedPanels = group.panels.filter((p) =>
featureAllPanelNames.includes(p.pluginPanelName)
);
return matchedPanels.map((p) => p.id);
}
/**
* 创建群组
*/
async createGroup(
ctx: TcContext<{
name: string;
panels: GroupPanel[];
}>
) {
const name = ctx.params.name;
const panels = ctx.params.panels;
const userId = ctx.meta.userId;
const t = ctx.meta.t;
if (
config.feature.disableCreateGroup === true &&
userId !== SYSTEM_USERID
) {
// 环境变量禁止创建群组
throw new NoPermissionError(t('创建群组功能已被管理员禁用'));
}
const doc = await this.adapter.model.createGroup({
name,
panels,
owner: userId,
});
const group = await this.transformDocuments(ctx, {}, doc);
const { textPanelIds, subscribeFeaturePanelIds } =
this.getSubscribedGroupPanelIds(group);
await call(ctx).joinSocketIORoom(
[String(group._id), ...textPanelIds, ...subscribeFeaturePanelIds],
userId
);
return group;
}
async getUserGroups(ctx: TcContext): Promise<GroupStruct[]> {
const userId = ctx.meta.userId;
const groups = await this.adapter.model.getUserGroups(userId);
return this.transformDocuments(ctx, {}, groups);
}
/**
* 获取用户所有加入群组的群组id列表与聊天会话id列表
*/
async getJoinedGroupAndPanelIds(ctx: TcContext): Promise<{
groupIds: string[];
textPanelIds: string[];
subscribeFeaturePanelIds: string[];
}> {
const groups = await this.getUserGroups(ctx); // TODO: 应该使用call而不是直接调用,为了获取tracer和caching支持。目前moleculer的文档没有显式的声明类似localCall的行为,可以花时间看一下
const textPanelIds = _.flatten(
groups.map((g) => this.getSubscribedGroupPanelIds(g).textPanelIds)
);
const subscribeFeaturePanelIds = _.flatten(
groups.map(
(g) => this.getSubscribedGroupPanelIds(g).subscribeFeaturePanelIds
)
);
return {
groupIds: groups.map((g) => String(g._id)),
textPanelIds,
subscribeFeaturePanelIds,
};
}
/**
* 获取所有订阅的群组面板列表
*/
async getGroupSocketRooms(ctx: TcContext<{ groupId: string }>): Promise<{
textPanelIds: string[];
subscribeFeaturePanelIds: string[];
}> {
const groupId = ctx.params.groupId;
const group = await call(ctx).getGroupInfo(groupId);
return this.getSubscribedGroupPanelIds(group);
}
/**
* 获取群组基本信息
*/
async getGroupBasicInfo(
ctx: PureContext<{
groupId: string;
}>
): Promise<GroupBaseInfo> {
const group = await this.adapter.model
.findById(ctx.params.groupId, {
name: 1,
avatar: 1,
owner: 1,
description: 1,
members: 1,
config: 1,
})
.exec();
if (group === null) {
return null;
}
const groupMemberCount = group.members.length;
const backgroundImage = group.config['groupBackgroundImage'];
return {
name: group.name,
avatar: group.avatar,
owner: String(group.owner),
description: group.description ?? '',
memberCount: groupMemberCount,
backgroundImage: backgroundImage,
};
}
/**
* 获取群组完整信息
* 仅内部可以访问
*/
async getGroupInfo(ctx: TcContext<{ groupId: string }>): Promise<Group> {
const groupInfo = await this.adapter.model.findById(ctx.params.groupId);
return await this.transformDocuments(ctx, {}, groupInfo);
}
/**
* 修改群组字段
*/
async updateGroupField(
ctx: TcContext<{
groupId: string;
fieldName: string;
fieldValue: unknown;
}>
) {
const { groupId, fieldName, fieldValue } = ctx.params;
const userId = ctx.meta.userId;
const t = ctx.meta.t;
if (
![
'name',
'avatar',
'description',
'panels',
'roles',
'fallbackPermissions',
].includes(fieldName)
) {
throw new EntityError(t('该数据不允许修改'));
}
const [isGroupOwner, hasRolePermission] = await call(
ctx
).checkUserPermissions(groupId, userId, [
PERMISSION.core.owner,
PERMISSION.core.manageRoles,
]);
if (fieldName === 'fallbackPermissions') {
if (!hasRolePermission) {
throw new NoPermissionError(t('没有操作权限'));
}
} else if (!isGroupOwner) {
throw new NoPermissionError(t('不是群组管理员无法编辑'));
}
const group = await this.adapter.model.findById(groupId).exec();
group[fieldName] = fieldValue;
await group.save();
if (fieldName === 'fallbackPermissions') {
await this.cleanGroupAllUserPermissionCache(groupId);
}
this.notifyGroupInfoUpdate(ctx, group);
}
/**
* 修改群组配置
*/
async updateGroupConfig(
ctx: TcContext<{
groupId: string;
configName: string;
configValue: unknown;
}>
) {
const { groupId, configName, configValue } = ctx.params;
const userId = ctx.meta.userId;
const t = ctx.meta.t;
const [hasPermission] = await call(ctx).checkUserPermissions(
groupId,
userId,
[PERMISSION.core.groupConfig]
);
if (!hasPermission) {
throw new NoPermissionError(t('没有操作权限'));
}
const group = await this.adapter.model.findOneAndUpdate(
{
_id: String(groupId),
},
{
$set: {
[`config.${configName}`]: configValue,
},
},
{
new: true,
}
);
this.notifyGroupInfoUpdate(ctx, group);
}
/**
* 检测用户是否为群组所有者
*/
async isGroupOwner(
ctx: TcContext<{
groupId: string;
}>
): Promise<boolean> {
const t = ctx.meta.t;
const group = await this.adapter.model.findById(ctx.params.groupId);
if (!group) {
throw new DataNotFoundError(t('没有找到群组'));
}
return String(group.owner) === ctx.meta.userId;
}
/**
* 群组添加成员
*/
async addMember(
ctx: TcContext<{
groupId: string;
userId: string;
}>
) {
const { groupId, userId } = ctx.params;
if (!isValidStr(userId)) {
throw new EntityError('用户id为空');
}
if (!isValidStr(groupId)) {
throw new EntityError('群组id为空');
}
const { members } = await this.adapter.model.findById(groupId, {
members: 1,
});
if (members.findIndex((m) => String(m.userId) === userId) >= 0) {
throw new Error('已加入该群组');
}
const doc = await this.adapter.model
.findByIdAndUpdate(
groupId,
{
$addToSet: {
members: {
userId: new Types.ObjectId(userId),
},
},
},
{
new: true,
}
)
.exec();
const group: GroupStruct = await this.transformDocuments(ctx, {}, doc);
this.notifyGroupInfoUpdate(ctx, group); // 推送变更
this.unicastNotify(ctx, userId, 'add', group);
const { textPanelIds, subscribeFeaturePanelIds } =
this.getSubscribedGroupPanelIds(group);
await call(ctx).joinSocketIORoom(
[String(group._id), ...textPanelIds, ...subscribeFeaturePanelIds],
userId
);
return group;
}
/**
* 加入群组
* @deprecated 请尽量使用 addMember
*/
async joinGroup(
ctx: TcContext<{
groupId: string;
}>
) {
const groupId = ctx.params.groupId;
const userId = ctx.meta.userId;
return this.localCall('addMember', {
groupId,
userId,
});
}
/**
* 退出群组
*/
async quitGroup(
ctx: TcContext<{
groupId: string;
}>
) {
const groupId = ctx.params.groupId;
const userId = ctx.meta.userId;
const group = await this.adapter.findById(groupId);
if (String(group.owner) === userId) {
// 是群组所有人
await this.adapter.removeById(groupId); // TODO: 后续可以考虑改为软删除
await this.roomcastNotify(ctx, groupId, 'remove', { groupId });
await ctx.call('gateway.leaveRoom', {
roomIds: [groupId],
});
} else {
// 是普通群组成员
const doc = await this.adapter.model
.findByIdAndUpdate(
groupId,
{
$pull: {
members: {
userId: new Types.ObjectId(userId),
},
},
},
{
new: true,
}
)
.exec();
const group: Group = await this.transformDocuments(ctx, {}, doc);
await this.memberLeaveGroup(ctx, group, userId);
}
}
/**
* 检查是否为群组成员
*/
async isMember(ctx: TcContext<{ groupId: string }>) {
const groupId = ctx.params.groupId;
const userId = ctx.meta.userId;
const groupInfo = await call(ctx).getGroupInfo(groupId);
if (!groupInfo) {
// 没有找到群组信息
return false;
}
const members = groupInfo.members;
return members.some((m) => String(m.userId) === userId);
}
/**
* 追加群组成员的角色
*/
async appendGroupMemberRoles(
ctx: TcContext<{
groupId: string;
memberIds: string[];
roles: string[];
}>
) {
const { groupId, memberIds, roles } = ctx.params;
await this.adapter.model.checkGroupFieldPermission(ctx, groupId, 'roles');
// 更新内容
await this.adapter.model.updateMany(
{
_id: new db.Types.ObjectId(groupId),
'members.userId': {
$in: [...memberIds],
},
},
{
$addToSet: {
'members.$[elem].roles': {
$each: roles,
},
},
},
{ arrayFilters: [{ 'elem.userId': { $in: [...memberIds] } }] }
);
const group = await this.adapter.model.findById(groupId);
await this.notifyGroupInfoUpdate(ctx, group);
await Promise.all(
memberIds.map((memberId) =>
this.cleanGroupUserPermission(groupId, memberId)
)
);
}
/**
* 移除群组成员的角色
*/
async removeGroupMemberRoles(
ctx: TcContext<{
groupId: string;
memberIds: string[];
roles: string[];
}>
) {
const { groupId, memberIds, roles } = ctx.params;
await this.adapter.model.checkGroupFieldPermission(ctx, groupId, 'roles');
// 更新内容
await this.adapter.model.updateMany(
{
_id: new db.Types.ObjectId(groupId),
'members.userId': {
$in: [...memberIds],
},
},
{
$pull: {
'members.$[elem].roles': {
$in: roles,
},
},
},
{ arrayFilters: [{ 'elem.userId': { $in: [...memberIds] } }] }
);
const group = await this.adapter.model.findById(groupId);
await this.notifyGroupInfoUpdate(ctx, group);
await Promise.all(
memberIds.map((memberId) =>
this.cleanGroupUserPermission(groupId, memberId)
)
);
}
/**
* 创建群组面板
*/
async createGroupPanel(
ctx: TcContext<{
groupId: string;
name: string;
type: number;
parentId?: string;
provider?: string;
pluginPanelName?: string;
meta?: object;
}>
) {
const { groupId, name, type, parentId, provider, pluginPanelName, meta } =
ctx.params;
const { t, userId } = ctx.meta;
const [hasPermission] = await call(ctx).checkUserPermissions(
groupId,
userId,
[PERMISSION.core.managePanel]
);
if (!hasPermission) {
throw new NoPermissionError(t('没有操作权限'));
}
const panelId = String(new Types.ObjectId());
const group = await this.adapter.model
.findOneAndUpdate(
{
_id: new Types.ObjectId(groupId),
},
{
$push: {
panels: {
id: panelId,
name,
type,
parentId,
provider,
pluginPanelName,
meta,
},
},
},
{
new: true,
}
)
.exec();
if (
type === GroupPanelType.TEXT ||
this.getPanelNamesWithFeature('subscribe').includes(name)
) {
/**
* 如果为订阅的面板
* 则所有群组成员加入房间
*/
const groupInfo = await call(ctx).getGroupInfo(groupId);
(groupInfo?.members ?? []).map((m) =>
call(ctx).joinSocketIORoom([panelId], m.userId)
);
}
this.notifyGroupInfoUpdate(ctx, group);
}
/**
* 修改群组面板
*/
async modifyGroupPanel(
ctx: TcContext<{
groupId: string;
panelId: string;
name: string;
type: number;
provider?: string;
pluginPanelName?: string;
meta?: object;
}>
) {
const { groupId, panelId, name, type, provider, pluginPanelName, meta } =
ctx.params;
const { t, userId } = ctx.meta;
const [hasPermission] = await call(ctx).checkUserPermissions(
groupId,
userId,
[PERMISSION.core.managePanel]
);
if (!hasPermission) {
throw new NoPermissionError(t('没有操作权限'));
}
const res = await this.adapter.model
.updateOne(
{
_id: new Types.ObjectId(groupId),
},
{
$set: {
'panels.$[element].name': name,
'panels.$[element].type': type,
'panels.$[element].provider': provider,
'panels.$[element].pluginPanelName': pluginPanelName,
'panels.$[element].meta': meta,
},
},
{
new: true,
arrayFilters: [{ 'element.id': panelId }],
}
)
.exec();
if (res.modifiedCount === 0) {
throw new Error(t('没有找到该面板'));
}
const group = await this.adapter.model.findById(String(groupId));
const json = await this.notifyGroupInfoUpdate(ctx, group);
return json;
}
/**
* 删除群组面板
*/
async deleteGroupPanel(ctx: TcContext<{ groupId: string; panelId: string }>) {
const { groupId, panelId } = ctx.params;
const { t, userId } = ctx.meta;
const [hasPermission] = await call(ctx).checkUserPermissions(
groupId,
userId,
[PERMISSION.core.managePanel]
);
if (!hasPermission) {
throw new NoPermissionError(t('没有操作权限'));
}
const group = await this.adapter.model
.findOneAndUpdate(
{
_id: new Types.ObjectId(groupId),
},
{
$pull: {
panels: {
$or: [
{
id: new Types.ObjectId(panelId),
},
{
parentId: new Types.ObjectId(panelId),
},
],
} as any,
},
},
{
new: true,
}
)
.exec();
const json = await this.notifyGroupInfoUpdate(ctx, group);
return json;
}
/**
* 获取群组大厅的会话ID()
*/
async getGroupLobbyConverseId(ctx: TcContext<{ groupId: string }>) {
const groupId = ctx.params.groupId;
const t = ctx.meta.t;
const group = await this.adapter.model.findById(groupId);
if (!group) {
throw new DataNotFoundError(t('群组未找到'));
}
const firstTextPanel = group.panels.find(
(panel) => panel.type === GroupPanelType.TEXT
);
if (!firstTextPanel) {
return null;
}
return firstTextPanel.id;
}
/**
* 创建群组角色
*/
async createGroupRole(
ctx: TcContext<{ groupId: string; roleName: string; permissions: string[] }>
) {
const { groupId, roleName, permissions } = ctx.params;
const { userId, t } = ctx.meta;
const [hasPermission] = await call(ctx).checkUserPermissions(
groupId,
userId,
[PERMISSION.core.managePanel]
);
if (!hasPermission) {
throw new NoPermissionError(t('没有操作权限'));
}
const group = await this.adapter.model
.findOneAndUpdate(
{
_id: new Types.ObjectId(groupId),
},
{
$push: {
roles: {
name: roleName,
permissions,
},
},
},
{
new: true,
}
)
.exec();
this.cleanGroupInfoCache(groupId);
const json = await this.notifyGroupInfoUpdate(ctx, group);
return json;
}
/**
* 删除群组角色
*/
async deleteGroupRole(ctx: TcContext<{ groupId: string; roleId: string }>) {
const { groupId, roleId } = ctx.params;
const { userId, t } = ctx.meta;
const [hasPermission] = await call(ctx).checkUserPermissions(
groupId,
userId,
[PERMISSION.core.manageRoles]
);