feat: 增加群组权限获取接口缓存

pull/49/head
moonrailgun 3 years ago
parent 0d01fd1789
commit 7b6d00363e

@ -167,11 +167,22 @@ class GroupService extends TcService {
},
}
);
this.registerAction('getGroupUserPermission', this.getGroupUserPermission, {
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',
@ -489,6 +500,11 @@ class GroupService extends TcService {
const group = await this.adapter.model.findById(groupId);
await this.notifyGroupInfoUpdate(ctx, group);
await Promise.all(
memberIds.map((memberId) =>
this.cleanGroupUserPermission(groupId, memberId)
)
);
}
/**
@ -531,6 +547,11 @@ class GroupService extends TcService {
const group = await this.adapter.model.findById(groupId);
await this.notifyGroupInfoUpdate(ctx, group);
await Promise.all(
memberIds.map((memberId) =>
this.cleanGroupUserPermission(groupId, memberId)
)
);
}
/**
@ -830,16 +851,34 @@ class GroupService extends TcService {
}
/**
*
* ()
*/
async getGroupUserPermission(
async getPermissions(
ctx: TcContext<{
groupId: string;
}>
) {
): Promise<string[]> {
const { groupId } = ctx.params;
const userId = ctx.meta.userId;
const permissions = await this.localCall('getUserAllPermissions', {
groupId,
userId,
});
return permissions;
}
/**
* ()
*/
async getUserAllPermissions(
ctx: TcContext<{
groupId: string;
userId: string;
}>
): Promise<string[]> {
const { groupId, userId } = ctx.params;
const permissions = await this.adapter.model.getGroupUserPermission(
groupId,
userId
@ -915,6 +954,15 @@ class GroupService extends TcService {
private cleanGroupInfoCache(groupId: string) {
this.cleanActionCache('getGroupInfo', [groupId]);
}
/**
*
* @param groupId id
* @param userId id
*/
private cleanGroupUserPermission(groupId: string, userId: string) {
this.cleanActionCache('getUserAllPermissions', [groupId, userId]);
}
}
export default GroupService;

@ -375,7 +375,7 @@ describe('Test "group" service', () => {
]);
});
test('Test "group.getGroupUserPermission"', async () => {
test('Test "group.getPermissions"', async () => {
const userId = new Types.ObjectId();
const role1 = createTestRole('TestRole1', ['permission1', 'permission2']);
const role2 = createTestRole('TestRole2', ['permission2', 'permission3']);
@ -392,7 +392,7 @@ describe('Test "group" service', () => {
);
const res: string[] = await broker.call(
'group.getGroupUserPermission',
'group.getPermissions',
{
groupId: String(testGroup.id),
},

Loading…
Cancel
Save