refactor public keys add

This commit is contained in:
tamaina 2024-03-04 00:34:37 +00:00
parent 83f635835e
commit 25cc9e0bf1

View File

@ -50,7 +50,7 @@ import type { ApResolverService, Resolver } from '../ApResolverService.js';
import type { ApLoggerService } from '../ApLoggerService.js'; import type { ApLoggerService } from '../ApLoggerService.js';
// eslint-disable-next-line @typescript-eslint/consistent-type-imports // eslint-disable-next-line @typescript-eslint/consistent-type-imports
import type { ApImageService } from './ApImageService.js'; import type { ApImageService } from './ApImageService.js';
import type { IActor, IObject } from '../type.js'; import type { IActor, IKey, IObject } from '../type.js';
const nameLength = 128; const nameLength = 128;
const summaryLength = 2048; const summaryLength = 2048;
@ -396,20 +396,17 @@ export class ApPersonService implements OnModuleInit {
})); }));
if (person.publicKey) { if (person.publicKey) {
await transactionalEntityManager.save(new MiUserPublickey({ const keys = new Map<string, IKey>([
keyId: person.publicKey.id, ...(person.additionalPublicKeys ? person.additionalPublicKeys.map(key => [key.id, key] as const) : []),
userId: user.id, [person.publicKey.id, person.publicKey],
keyPem: person.publicKey.publicKeyPem, ]);
}));
if (person.additionalPublicKeys) { for (const key of keys.values()) {
for (const key of person.additionalPublicKeys) { await transactionalEntityManager.save(new MiUserPublickey({
await transactionalEntityManager.save(new MiUserPublickey({ keyId: key.id,
keyId: key.id, userId: user.id,
userId: user.id, keyPem: key.publicKeyPem,
keyPem: key.publicKeyPem, }));
}));
}
} }
} }
}); });
@ -556,27 +553,21 @@ export class ApPersonService implements OnModuleInit {
// Update user // Update user
await this.usersRepository.update(exist.id, updates); await this.usersRepository.update(exist.id, updates);
const availablePublicKeys = new Set<string>(); const publicKeys = new Map<string, IKey>();
if (person.publicKey) { if (person.publicKey) {
await this.userPublickeysRepository.update({ keyId: person.publicKey.id }, { (person.additionalPublicKeys ?? []).forEach(key => publicKeys.set(key.id, key));
userId: exist.id, publicKeys.set(person.publicKey.id, person.publicKey);
keyPem: person.publicKey.publicKeyPem,
});
availablePublicKeys.add(person.publicKey.id);
if (person.additionalPublicKeys) { for (const key of publicKeys.values()) {
for (const key of person.additionalPublicKeys) { await this.userPublickeysRepository.update({ keyId: key.id }, {
await this.userPublickeysRepository.update({ keyId: key.id }, { userId: exist.id,
userId: exist.id, keyPem: key.publicKeyPem,
keyPem: key.publicKeyPem, });
});
availablePublicKeys.add(key.id);
}
} }
} }
this.userPublickeysRepository.delete({ this.userPublickeysRepository.delete({
keyId: Not(In(Array.from(availablePublicKeys))), keyId: Not(In(Array.from(publicKeys.keys()))),
userId: exist.id, userId: exist.id,
}); });