diff --git a/packages/plugin-common/src/authorize.ts b/packages/plugin-common/src/authorize.ts index ed1a09d5df..b625448c8c 100644 --- a/packages/plugin-common/src/authorize.ts +++ b/packages/plugin-common/src/authorize.ts @@ -117,12 +117,12 @@ export default function apply (ctx: Context, config: AuthorizeOptions = {}) { await Promise.all(tasks.map(task => task.catch(logger.warn))) - let totalInsert = 0, totalUpdate = 0 + let insertTotal = 0, updateTotal = 0 for (const key in authorizeInfoList) { const authority = +key const { insert, update } = authorizeInfoList[key] - totalInsert += insert.size - totalUpdate += update.size + insertTotal += insert.size + updateTotal += update.size for (const id of insert) { await database.getUser(id, authority) logger.debug(`inserted ${id} with authority ${authority}`) @@ -133,6 +133,13 @@ export default function apply (ctx: Context, config: AuthorizeOptions = {}) { } } - logger.info(`inserted ${totalInsert} user(s) and updated ${totalUpdate} user(s)`) + const output: string[] = [] + if (insertTotal) output.push(`inserted ${insertTotal} user${insertTotal > 1 ? 's' : ''}`) + if (updateTotal) output.push(`updated ${updateTotal} user${updateTotal > 1 ? 's' : ''}`) + if (!output.length) { + logger.info('all users are up to date') + } else { + logger.info(output.join(' and ')) + } }) } diff --git a/packages/plugin-common/tests/authorize.spec.ts b/packages/plugin-common/tests/authorize.spec.ts index a60eb9a015..9e14c957da 100644 --- a/packages/plugin-common/tests/authorize.spec.ts +++ b/packages/plugin-common/tests/authorize.spec.ts @@ -25,7 +25,7 @@ test('authorize user', async () => { app.receiver.on('connect', () => { app.database.memory.store.user[231] = createUser(231, 3) - app.database.memory.store.user[312] = createUser(312, 2) + app.database.memory.store.user[312] = createUser(312, 1) }) await app.start() @@ -47,11 +47,13 @@ test('authorize group 1', async () => { app.receiver.on('connect', () => { app.database.memory.store.user[231] = createUser(231, 3) + app.database.memory.store.user[312] = createUser(312, 1) }) app.setResponse('get_group_member_list', [ { userId: 123, role: 'member' }, { userId: 231, role: 'member' }, + { userId: 312, role: 'member' }, ]) await app.start() @@ -60,6 +62,7 @@ test('authorize group 1', async () => { await expect(app.database.getGroup(456)).resolves.toHaveProperty('assignee', app.selfId) await expect(app.database.getUser(123)).resolves.toHaveProperty('authority', 2) await expect(app.database.getUser(231)).resolves.toHaveProperty('authority', 3) + await expect(app.database.getUser(312)).resolves.toHaveProperty('authority', 2) }) test('authorize group 2', async () => { @@ -190,11 +193,13 @@ test('mixed usage', async () => { app.receiver.on('connect', () => { app.database.memory.store.user[231] = createUser(231, 2) + app.database.memory.store.user[312] = createUser(312, 1) }) app.setResponse('get_group_member_list', [ { userId: 123, role: 'member' }, { userId: 231, role: 'member' }, + { userId: 312, role: 'member' }, ]) await app.start() @@ -202,4 +207,5 @@ test('mixed usage', async () => { await expect(app.database.getUser(123)).resolves.toHaveProperty('authority', 3) await expect(app.database.getUser(231)).resolves.toHaveProperty('authority', 3) + await expect(app.database.getUser(312)).resolves.toHaveProperty('authority', 3) })