From f27038448c886026bfa14b689864eb2a28f95a89 Mon Sep 17 00:00:00 2001 From: tamaina Date: Thu, 20 Apr 2023 18:03:06 +0000 Subject: [PATCH] =?UTF-8?q?=E3=83=A6=E3=83=BC=E3=82=B6=E3=83=BC=E3=83=AA?= =?UTF-8?q?=E3=82=B9=E3=83=88=E3=81=AE=E7=A7=BB=E8=A1=8C=E3=81=AF=E8=BF=BD?= =?UTF-8?q?=E5=8A=A0=E3=81=AE=E3=81=BF=E3=82=92=E8=A1=8C=E3=81=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/src/core/AccountMoveService.ts | 41 +++++++++++++++---- packages/backend/test/e2e/move.ts | 8 ++-- 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/packages/backend/src/core/AccountMoveService.ts b/packages/backend/src/core/AccountMoveService.ts index 2b74bc8afc..4db0f928e5 100644 --- a/packages/backend/src/core/AccountMoveService.ts +++ b/packages/backend/src/core/AccountMoveService.ts @@ -5,10 +5,11 @@ import { bindThis } from '@/decorators.js'; import { DI } from '@/di-symbols.js'; import type { Config } from '@/config.js'; import type { LocalUser } from '@/models/entities/User.js'; -import type { BlockingsRepository, FollowingsRepository, InstancesRepository, Muting, MutingsRepository, UserListJoiningsRepository, UsersRepository } from '@/models/index.js'; +import type { BlockingsRepository, FollowingsRepository, InstancesRepository, MutingsRepository, UserListJoiningsRepository, UsersRepository } from '@/models/index.js'; import type { RelationshipJobData, ThinUser } from '@/queue/types.js'; import type { User } from '@/models/entities/User.js'; +import { IdService } from '@/core/IdService.js'; import { AccountUpdateService } from '@/core/AccountUpdateService.js'; import { GlobalEventService } from '@/core/GlobalEventService.js'; import { QueueService } from '@/core/QueueService.js'; @@ -49,6 +50,7 @@ export class AccountMoveService { private instancesRepository: InstancesRepository, private userEntityService: UserEntityService, + private idService: IdService, private apRendererService: ApRendererService, private apDeliverManagerService: ApDeliverManagerService, private globalEventService: GlobalEventService, @@ -195,20 +197,45 @@ export class AccountMoveService { } } + /** + * Update lists while moving accounts. + * - No removal of the old account from the lists + * - Users number limit is not checked + * + * @param src ThinUser (old account) + * @param dst User (new account) + * @returns Promise + */ @bindThis public async updateLists(src: ThinUser, dst: User): Promise { // Return if there is no list to be updated. - const exists = await this.userListJoiningsRepository.exist({ + const oldJoinings = await this.userListJoiningsRepository.find({ where: { userId: src.id, }, }); - if (!exists) return; + if (oldJoinings.length === 0) return; - await this.userListJoiningsRepository.update( - { userId: src.id }, - { userId: dst.id } - ); + const newJoinings: Map = new Map(); + + // 重複しないようにIDを生成 + const genId = () => { + let id: string; + do { + id = this.idService.genId(); + } while (newJoinings.has(id)); + return id; + }; + for (const joining of oldJoinings) { + newJoinings.set(genId(), { + createdAt: new Date(), + userId: dst.id, + userListId: joining.userListId, + }); + } + + const arrayToInsert = Array.from(newJoinings.entries()).map(entry => ({ ...entry[1], id: entry[0] })); + await this.userListJoiningsRepository.insert(arrayToInsert); // Have the proxy account follow the new account in the same way as UserListService.push if (this.userEntityService.isRemoteUser(dst)) { diff --git a/packages/backend/test/e2e/move.ts b/packages/backend/test/e2e/move.ts index 79d2496aa1..7330d50ef3 100644 --- a/packages/backend/test/e2e/move.ts +++ b/packages/backend/test/e2e/move.ts @@ -45,7 +45,6 @@ describe('Account Move', () => { Mutings = connection.getRepository(Muting); }, 1000 * 60 * 2); - afterAll(async () => { await app.close(); }); @@ -206,7 +205,7 @@ describe('Account Move', () => { const followings = await api('/users/following', { userId: carol.id, - }, carol) + }, carol); assert.strictEqual(followings.status, 200); assert.strictEqual(followings.body.length, 2); assert.strictEqual(followings.body[0].followeeId, bob.id); @@ -225,8 +224,9 @@ describe('Account Move', () => { const lists = await api('/users/lists/list', {}, root); assert.strictEqual(lists.status, 200); - assert.strictEqual(lists.body[0].userIds.length, 1); - assert.strictEqual(lists.body[0].userIds[0], bob.id); + assert.strictEqual(lists.body[0].userIds.length, 2); + assert.ok(lists.body[0].userIds.find((id: string) => id === bob.id)); + assert.ok(lists.body[0].userIds.find((id: string) => id === alice.id)); }); test('Follow and follower counts are properly adjusted', async () => {