summaryrefslogtreecommitdiff
path: root/packages/backend/src/core/AccountMoveService.ts
diff options
context:
space:
mode:
authoranatawa12 <anatawa12@icloud.com>2025-04-03 19:22:49 +0900
committerGitHub <noreply@github.com>2025-04-03 19:22:49 +0900
commitcab82452ecb9aa6f89da22a1177cbf0e01f0462b (patch)
treee0cf3599ec319a2ffeb8cc7233bf834c387e304f /packages/backend/src/core/AccountMoveService.ts
parentrefactor(frontend): refactor MkDateSeparatedList (diff)
downloadsharkey-cab82452ecb9aa6f89da22a1177cbf0e01f0462b.tar.gz
sharkey-cab82452ecb9aa6f89da22a1177cbf0e01f0462b.tar.bz2
sharkey-cab82452ecb9aa6f89da22a1177cbf0e01f0462b.zip
Copy role on move (#15745)
* feat(backend): copyOnMoveAccount * feat(endpoints): copyOnMoveAccount * feat(frontend): copyOnMoveAccount * docs(changelog): アカウントのマイグレーション時に古いアカウントからロールをコピーできるようになりました。 * fix: spdx header for migration * Update locales/ja-JP.yml * copyOnMoveAccount -> preserveAssignmentOnMoveAccount * fix: check for preserveAssignmentOnMoveAccount --------- Co-authored-by: syuilo <4439005+syuilo@users.noreply.github.com>
Diffstat (limited to 'packages/backend/src/core/AccountMoveService.ts')
-rw-r--r--packages/backend/src/core/AccountMoveService.ts29
1 files changed, 29 insertions, 0 deletions
diff --git a/packages/backend/src/core/AccountMoveService.ts b/packages/backend/src/core/AccountMoveService.ts
index 0fbb9bcd80..406563bee8 100644
--- a/packages/backend/src/core/AccountMoveService.ts
+++ b/packages/backend/src/core/AccountMoveService.ts
@@ -24,6 +24,7 @@ import { FederatedInstanceService } from '@/core/FederatedInstanceService.js';
import InstanceChart from '@/core/chart/charts/instance.js';
import PerUserFollowingChart from '@/core/chart/charts/per-user-following.js';
import { SystemAccountService } from '@/core/SystemAccountService.js';
+import { RoleService } from '@/core/RoleService.js';
@Injectable()
export class AccountMoveService {
@@ -61,6 +62,7 @@ export class AccountMoveService {
private relayService: RelayService,
private queueService: QueueService,
private systemAccountService: SystemAccountService,
+ private roleService: RoleService,
) {
}
@@ -119,6 +121,7 @@ export class AccountMoveService {
await Promise.all([
this.copyBlocking(src, dst),
this.copyMutings(src, dst),
+ this.copyRoles(src, dst),
this.updateLists(src, dst),
]);
} catch {
@@ -201,6 +204,32 @@ export class AccountMoveService {
await this.mutingsRepository.insert(arrayToInsert);
}
+ @bindThis
+ public async copyRoles(src: ThinUser, dst: ThinUser): Promise<void> {
+ // Insert new roles with the same values except userId
+ // role service may have cache for roles so retrieve roles from service
+ const [oldRoleAssignments, roles] = await Promise.all([
+ this.roleService.getUserAssigns(src.id),
+ this.roleService.getRoles(),
+ ]);
+
+ if (oldRoleAssignments.length === 0) return;
+
+ // No promise all since the only async operation is writing to the database
+ for (const oldRoleAssignment of oldRoleAssignments) {
+ const role = roles.find(x => x.id === oldRoleAssignment.roleId);
+ if (role == null) continue; // Very unlikely however removing role may cause this case
+ if (!role.preserveAssignmentOnMoveAccount) continue;
+
+ try {
+ await this.roleService.assign(dst.id, role.id, oldRoleAssignment.expiresAt);
+ } catch (e) {
+ if (e instanceof RoleService.AlreadyAssignedError) continue;
+ throw e;
+ }
+ }
+ }
+
/**
* Update lists while moving accounts.
* - No removal of the old account from the lists