From 2ff3069d23aa688cea5a3bd204236f2f2f64c201 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 12 Apr 2019 01:52:25 +0900 Subject: トランザクションを使うようにしたり MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/remote/activitypub/models/person.ts | 71 +++++++++++++++++---------------- 1 file changed, 37 insertions(+), 34 deletions(-) (limited to 'src/remote/activitypub') diff --git a/src/remote/activitypub/models/person.ts b/src/remote/activitypub/models/person.ts index b2edf39734..58aca48eb0 100644 --- a/src/remote/activitypub/models/person.ts +++ b/src/remote/activitypub/models/person.ts @@ -25,6 +25,7 @@ import { isDuplicateKeyValueError } from '../../../misc/is-duplicate-key-value-e import { toPuny } from '../../../misc/convert-host'; import { UserProfile } from '../../../models/entities/user-profile'; import { validActor } from '../../../remote/activitypub/type'; +import { getConnection } from 'typeorm'; const logger = apLogger; /** @@ -136,27 +137,42 @@ export async function createPerson(uri: string, resolver?: Resolver): Promise) as IRemoteUser; + // Start transaction + await getConnection().transaction(async transactionalEntityManager => { + user = await transactionalEntityManager.save(new User({ + id: genId(), + avatarId: null, + bannerId: null, + createdAt: new Date(person.published) || new Date(), + lastFetchedAt: new Date(), + name: person.name, + isLocked: person.manuallyApprovesFollowers, + username: person.preferredUsername, + usernameLower: person.preferredUsername.toLowerCase(), + host, + inbox: person.inbox, + sharedInbox: person.sharedInbox || (person.endpoints ? person.endpoints.sharedInbox : undefined), + featured: person.featured, + uri: person.id, + tags, + isBot, + isCat: (person as any).isCat === true + })) as IRemoteUser; + + await transactionalEntityManager.save(new UserProfile({ + userId: user.id, + description: fromHtml(person.summary), + url: person.url, + fields, + userHost: host + })); + + await transactionalEntityManager.save(new UserPublickey({ + userId: user.id, + keyId: person.publicKey.id, + keyPem: person.publicKey.publicKeyPem + })); + }); } catch (e) { // duplicate key error if (isDuplicateKeyValueError(e)) { @@ -167,19 +183,6 @@ export async function createPerson(uri: string, resolver?: Resolver): Promise); - - await UserPublickeys.save({ - userId: user.id, - keyId: person.publicKey.id, - keyPem: person.publicKey.publicKeyPem - } as UserPublickey); - // Register host registerOrFetchInstanceDoc(host).then(i => { Instances.increment({ id: i.id }, 'usersCount', 1); -- cgit v1.2.3-freya