use Acct.parse instead

This commit is contained in:
Namekuji 2023-04-21 08:55:35 -04:00
parent 2c35acbd2e
commit 97e1e971d7
3 changed files with 40 additions and 39 deletions

View file

@ -9,11 +9,12 @@ import { ApiError } from '@/server/api/error.js';
import { AccountMoveService } from '@/core/AccountMoveService.js'; import { AccountMoveService } from '@/core/AccountMoveService.js';
import { RemoteUserResolveService } from '@/core/RemoteUserResolveService.js'; import { RemoteUserResolveService } from '@/core/RemoteUserResolveService.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { ApiLoggerService } from '@/server/api/ApiLoggerService.js'; import { ApiLoggerService } from '@/server/api/ApiLoggerService.js';
import { GetterService } from '@/server/api/GetterService.js'; import { GetterService } from '@/server/api/GetterService.js';
import { ApPersonService } from '@/core/activitypub/models/ApPersonService.js'; import { ApPersonService } from '@/core/activitypub/models/ApPersonService.js';
import * as Acct from '@/misc/acct.js';
export const meta = { export const meta = {
tags: ['users'], tags: ['users'],
@ -75,7 +76,6 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
@Inject(DI.config) @Inject(DI.config)
private config: Config, private config: Config,
private userEntityService: UserEntityService,
private remoteUserResolveService: RemoteUserResolveService, private remoteUserResolveService: RemoteUserResolveService,
private apiLoggerService: ApiLoggerService, private apiLoggerService: ApiLoggerService,
private accountMoveService: AccountMoveService, private accountMoveService: AccountMoveService,
@ -90,17 +90,10 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
// abort if user has already moved // abort if user has already moved
if (me.movedToUri) throw new ApiError(meta.errors.alreadyMoved); if (me.movedToUri) throw new ApiError(meta.errors.alreadyMoved);
let unfiltered = ps.moveToAccount;
if (!unfiltered) throw new ApiError(meta.errors.noSuchUser);
// parse user's input into the destination account // parse user's input into the destination account
if (unfiltered.startsWith('acct:')) unfiltered = unfiltered.substring(5); const { username, host } = Acct.parse(ps.moveToAccount);
if (unfiltered.startsWith('@')) unfiltered = unfiltered.substring(1);
if (!unfiltered.includes('@')) throw new ApiError(meta.errors.noSuchUser);
const userAddress = unfiltered.split('@');
// retrieve the destination account // retrieve the destination account
let moveTo = await this.remoteUserResolveService.resolveUser(userAddress[0], userAddress[1]).catch((e) => { let moveTo = await this.remoteUserResolveService.resolveUser(username, host).catch((e) => {
this.apiLoggerService.logger.warn(`failed to resolve remote user: ${e}`); this.apiLoggerService.logger.warn(`failed to resolve remote user: ${e}`);
throw new ApiError(meta.errors.noSuchUser); throw new ApiError(meta.errors.noSuchUser);
}); });

View file

@ -3,6 +3,7 @@ import * as mfm from 'mfm-js';
import { Inject, Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import { extractCustomEmojisFromMfm } from '@/misc/extract-custom-emojis-from-mfm.js'; import { extractCustomEmojisFromMfm } from '@/misc/extract-custom-emojis-from-mfm.js';
import { extractHashtags } from '@/misc/extract-hashtags.js'; import { extractHashtags } from '@/misc/extract-hashtags.js';
import * as Acct from '@/misc/acct.js';
import type { UsersRepository, DriveFilesRepository, UserProfilesRepository, PagesRepository } from '@/models/index.js'; import type { UsersRepository, DriveFilesRepository, UserProfilesRepository, PagesRepository } from '@/models/index.js';
import type { User } from '@/models/entities/User.js'; import type { User } from '@/models/entities/User.js';
import { birthdaySchema, descriptionSchema, locationSchema, nameSchema } from '@/models/entities/User.js'; import { birthdaySchema, descriptionSchema, locationSchema, nameSchema } from '@/models/entities/User.js';
@ -303,14 +304,11 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
// Parse user's input into the old account // Parse user's input into the old account
const newAlsoKnownAs = new Set<string>(); const newAlsoKnownAs = new Set<string>();
for (const line of ps.alsoKnownAs) { for (const line of ps.alsoKnownAs) {
let unfiltered = line; if (!line) throw new ApiError(meta.errors.noSuchUser);
if (unfiltered.startsWith('acct:')) unfiltered = unfiltered.substring(5); const { username, host } = Acct.parse(line);
if (unfiltered.startsWith('@')) unfiltered = unfiltered.substring(1);
if (!unfiltered.includes('@')) throw new ApiError(meta.errors.noSuchUser);
const userAddress = unfiltered.split('@');
// Retrieve the old account // Retrieve the old account
const knownAs = await this.remoteUserResolveService.resolveUser(userAddress[0], userAddress[1]).catch((e) => { const knownAs = await this.remoteUserResolveService.resolveUser(username, host).catch((e) => {
this.apiLoggerService.logger.warn(`failed to resolve dstination user: ${e}`); this.apiLoggerService.logger.warn(`failed to resolve dstination user: ${e}`);
throw new ApiError(meta.errors.noSuchUser); throw new ApiError(meta.errors.noSuchUser);
}); });

View file

@ -57,6 +57,26 @@ describe('Account Move', () => {
assert.strictEqual(newBob.alsoKnownAs[0], `${url.origin}/users/${alice.id}`); assert.strictEqual(newBob.alsoKnownAs[0], `${url.origin}/users/${alice.id}`);
}); });
test('Able to create a local alias without hostname', async () => {
await api('/i/update', {
alsoKnownAs: ['@alice'],
}, bob);
const newBob = await Users.findOneByOrFail({ id: bob.id });
assert.strictEqual(newBob.alsoKnownAs?.length, 1);
assert.strictEqual(newBob.alsoKnownAs[0], `${url.origin}/users/${alice.id}`);
});
test('Able to create a local alias without @', async () => {
await api('/i/update', {
alsoKnownAs: ['alice'],
}, bob);
const newBob = await Users.findOneByOrFail({ id: bob.id });
assert.strictEqual(newBob.alsoKnownAs?.length, 1);
assert.strictEqual(newBob.alsoKnownAs[0], `${url.origin}/users/${alice.id}`);
});
test('Able to set remote user (but may fail)', async () => { test('Able to set remote user (but may fail)', async () => {
const res = await api('/i/update', { const res = await api('/i/update', {
alsoKnownAs: ['@syuilo@example.com'], alsoKnownAs: ['@syuilo@example.com'],
@ -88,13 +108,21 @@ describe('Account Move', () => {
}); });
test('Unable to add a nonexisting local account to alsoKnownAs', async () => { test('Unable to add a nonexisting local account to alsoKnownAs', async () => {
const res = await api('/i/update', { const res1 = await api('/i/update', {
alsoKnownAs: [`@nonexist@${url.hostname}`], alsoKnownAs: [`@nonexist@${url.hostname}`],
}, bob); }, bob);
assert.strictEqual(res.status, 400); assert.strictEqual(res1.status, 400);
assert.strictEqual(res.body.error.code, 'NO_SUCH_USER'); assert.strictEqual(res1.body.error.code, 'NO_SUCH_USER');
assert.strictEqual(res.body.error.id, 'fcd2eef9-a9b2-4c4f-8624-038099e90aa5'); assert.strictEqual(res1.body.error.id, 'fcd2eef9-a9b2-4c4f-8624-038099e90aa5');
const res2 = await api('/i/update', {
alsoKnownAs: ['@alice', 'nonexist'],
}, bob);
assert.strictEqual(res2.status, 400);
assert.strictEqual(res2.body.error.code, 'NO_SUCH_USER');
assert.strictEqual(res2.body.error.id, 'fcd2eef9-a9b2-4c4f-8624-038099e90aa5');
}); });
test('Able to add two existing local account to alsoKnownAs', async () => { test('Able to add two existing local account to alsoKnownAs', async () => {
@ -121,24 +149,6 @@ describe('Account Move', () => {
assert.strictEqual(newBob.alsoKnownAs[0], `${url.origin}/users/${carol.id}`); assert.strictEqual(newBob.alsoKnownAs[0], `${url.origin}/users/${carol.id}`);
assert.strictEqual(newBob.alsoKnownAs[1], `${url.origin}/users/${dave.id}`); assert.strictEqual(newBob.alsoKnownAs[1], `${url.origin}/users/${dave.id}`);
}); });
test('Unable to create an alias without the second @', async () => {
const res1 = await api('/i/update', {
alsoKnownAs: ['@alice'],
}, bob);
assert.strictEqual(res1.status, 400);
assert.strictEqual(res1.body.error.code, 'NO_SUCH_USER');
assert.strictEqual(res1.body.error.id, 'fcd2eef9-a9b2-4c4f-8624-038099e90aa5');
const res2 = await api('/i/update', {
alsoKnownAs: ['alice'],
}, bob);
assert.strictEqual(res2.status, 400);
assert.strictEqual(res2.body.error.code, 'NO_SUCH_USER');
assert.strictEqual(res2.body.error.id, 'fcd2eef9-a9b2-4c4f-8624-038099e90aa5');
});
}); });
describe('Local to Local', () => { describe('Local to Local', () => {