From 25cc9e0bf10b6f9204ad9d3c8cee2d63b0e9117a Mon Sep 17 00:00:00 2001 From: tamaina Date: Mon, 4 Mar 2024 00:34:37 +0000 Subject: [PATCH] refactor public keys add --- .../activitypub/models/ApPersonService.ts | 49 ++++++++----------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/packages/backend/src/core/activitypub/models/ApPersonService.ts b/packages/backend/src/core/activitypub/models/ApPersonService.ts index e331ea5395..983bf0ebc5 100644 --- a/packages/backend/src/core/activitypub/models/ApPersonService.ts +++ b/packages/backend/src/core/activitypub/models/ApPersonService.ts @@ -50,7 +50,7 @@ import type { ApResolverService, Resolver } from '../ApResolverService.js'; import type { ApLoggerService } from '../ApLoggerService.js'; // eslint-disable-next-line @typescript-eslint/consistent-type-imports 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 summaryLength = 2048; @@ -396,20 +396,17 @@ export class ApPersonService implements OnModuleInit { })); if (person.publicKey) { - await transactionalEntityManager.save(new MiUserPublickey({ - keyId: person.publicKey.id, - userId: user.id, - keyPem: person.publicKey.publicKeyPem, - })); + const keys = new Map([ + ...(person.additionalPublicKeys ? person.additionalPublicKeys.map(key => [key.id, key] as const) : []), + [person.publicKey.id, person.publicKey], + ]); - if (person.additionalPublicKeys) { - for (const key of person.additionalPublicKeys) { - await transactionalEntityManager.save(new MiUserPublickey({ - keyId: key.id, - userId: user.id, - keyPem: key.publicKeyPem, - })); - } + for (const key of keys.values()) { + await transactionalEntityManager.save(new MiUserPublickey({ + keyId: key.id, + userId: user.id, + keyPem: key.publicKeyPem, + })); } } }); @@ -556,27 +553,21 @@ export class ApPersonService implements OnModuleInit { // Update user await this.usersRepository.update(exist.id, updates); - const availablePublicKeys = new Set(); + const publicKeys = new Map(); if (person.publicKey) { - await this.userPublickeysRepository.update({ keyId: person.publicKey.id }, { - userId: exist.id, - keyPem: person.publicKey.publicKeyPem, - }); - availablePublicKeys.add(person.publicKey.id); + (person.additionalPublicKeys ?? []).forEach(key => publicKeys.set(key.id, key)); + publicKeys.set(person.publicKey.id, person.publicKey); - if (person.additionalPublicKeys) { - for (const key of person.additionalPublicKeys) { - await this.userPublickeysRepository.update({ keyId: key.id }, { - userId: exist.id, - keyPem: key.publicKeyPem, - }); - availablePublicKeys.add(key.id); - } + for (const key of publicKeys.values()) { + await this.userPublickeysRepository.update({ keyId: key.id }, { + userId: exist.id, + keyPem: key.publicKeyPem, + }); } } this.userPublickeysRepository.delete({ - keyId: Not(In(Array.from(availablePublicKeys))), + keyId: Not(In(Array.from(publicKeys.keys()))), userId: exist.id, });