summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/endpoints/i/known-as.ts
blob: 964704d82b057dbe9f8f1b28bd89f19de2922318 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { Injectable } from '@nestjs/common';
import ms from 'ms';

import { User } from '@/models/entities/User.js';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { ApiError } from '@/server/api/error.js';

import { AccountMoveService } from '@/core/AccountMoveService.js';
import { RemoteUserResolveService } from '@/core/RemoteUserResolveService.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { ApiLoggerService } from '@/server/api/ApiLoggerService.js';

export const meta = {
	tags: ['users'],

	secure: true,
	requireCredential: true,

	limit: {
		duration: ms('1day'),
		max: 30,
	},

	errors: {
		noSuchUser: {
			message: 'No such user.',
			code: 'NO_SUCH_USER',
			id: 'fcd2eef9-a9b2-4c4f-8624-038099e90aa5',
		},
		notRemote: {
			message: 'User is not remote. You can only migrate from other instances.',
			code: 'NOT_REMOTE',
			id: '4362f8dc-731f-4ad8-a694-be2a88922a24',
		},
		uriNull: {
			message: 'User ActivityPup URI is null.',
			code: 'URI_NULL',
			id: 'bf326f31-d430-4f97-9933-5d61e4d48a23',
		},
	},
} as const;

export const paramDef = {
	type: 'object',
	properties: {
		alsoKnownAs: { type: 'string' },
	},
	required: ['alsoKnownAs'],
} as const;

@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> {
	constructor(
		private userEntityService: UserEntityService,
		private remoteUserResolveService: RemoteUserResolveService,
		private apiLoggerService: ApiLoggerService,
		private accountMoveService: AccountMoveService,
	) {
		super(meta, paramDef, async (ps, me) => {
			// Check parameter
			if (!ps.alsoKnownAs) throw new ApiError(meta.errors.noSuchUser);

			let unfiltered = ps.alsoKnownAs;
			const updates = {} as Partial<User>;

			if (!unfiltered) {
				updates.alsoKnownAs = null;
			} else {
				// Parse user's input into the old account
				if (unfiltered.startsWith('acct:')) unfiltered = unfiltered.substring(5);
				if (unfiltered.startsWith('@')) unfiltered = unfiltered.substring(1);
				if (!unfiltered.includes('@')) throw new ApiError(meta.errors.notRemote);

				const userAddress = unfiltered.split('@');
				// Retrieve the old account
				const knownAs = await this.remoteUserResolveService.resolveUser(userAddress[0], userAddress[1]).catch((e) => {
					this.apiLoggerService.logger.warn(`failed to resolve remote user: ${e}`);
					throw new ApiError(meta.errors.noSuchUser);
				});

				const toUrl: string | null = knownAs.uri;
				if (!toUrl) throw new ApiError(meta.errors.uriNull);
				// Only allow moving from a remote account
				if (this.userEntityService.isLocalUser(knownAs)) throw new ApiError(meta.errors.notRemote);

				updates.alsoKnownAs = updates.alsoKnownAs?.concat([toUrl]) ?? [toUrl];
			}

			return await this.accountMoveService.createAlias(me, updates);
		});
	}
}