nanka iroiro

This commit is contained in:
tamaina 2023-04-20 19:07:33 +00:00
parent f27038448c
commit c115137796
4 changed files with 56 additions and 10 deletions

View file

@ -369,7 +369,7 @@ export class UserEntityService implements OnModuleInit {
...(opts.detail ? { ...(opts.detail ? {
url: profile!.url, url: profile!.url,
uri: user.uri, uri: user.uri,
movedToUri: user.movedToUri ? await this.apPersonService.resolvePerson(user.movedToUri) : null, movedToUri: user.movedToUri ? this.apPersonService.resolvePerson(user.movedToUri).then(user => user.uri).catch(() => null) : null,
alsoKnownAs: user.alsoKnownAs, alsoKnownAs: user.alsoKnownAs,
createdAt: user.createdAt.toISOString(), createdAt: user.createdAt.toISOString(),
updatedAt: user.updatedAt ? user.updatedAt.toISOString() : null, updatedAt: user.updatedAt ? user.updatedAt.toISOString() : null,

View file

@ -32,6 +32,11 @@ export const meta = {
code: 'URI_NULL', code: 'URI_NULL',
id: 'bf326f31-d430-4f97-9933-5d61e4d48a23', id: 'bf326f31-d430-4f97-9933-5d61e4d48a23',
}, },
forbiddenToSetYourself: {
message: 'You can\'t set yourself as your own alias.',
code: 'FORBIDDEN_TO_SET_YOURSELF',
id: '25c90186-4ab0-49c8-9bba-a1fa6c202ba4',
},
}, },
} as const; } as const;
@ -51,9 +56,6 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
private accountMoveService: AccountMoveService, private accountMoveService: AccountMoveService,
) { ) {
super(meta, paramDef, async (ps, me) => { super(meta, paramDef, async (ps, me) => {
// Check parameter
if (!ps.alsoKnownAs) throw new ApiError(meta.errors.noSuchUser);
let unfiltered = ps.alsoKnownAs; let unfiltered = ps.alsoKnownAs;
const updates = {} as Partial<User>; const updates = {} as Partial<User>;
@ -71,6 +73,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> {
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);
}); });
if (knownAs.id === me.id) throw new ApiError(meta.errors.forbiddenToSetYourself);
const toUrl = this.accountMoveService.getUserUri(knownAs); const toUrl = this.accountMoveService.getUserUri(knownAs);
if (!toUrl) throw new ApiError(meta.errors.uriNull); if (!toUrl) throw new ApiError(meta.errors.uriNull);

View file

@ -54,6 +54,49 @@ describe('Account Move', () => {
await Users.update(bob.id, { alsoKnownAs: null }); await Users.update(bob.id, { alsoKnownAs: null });
}, 1000 * 10); }, 1000 * 10);
test('Able to create an alias', async () => {
await api('/i/known-as', {
alsoKnownAs: `@alice@${url.hostname}`,
}, 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 () => {
const res = await api('/i/known-as', {
alsoKnownAs: `@syuilo@example.com`,
}, bob);
assert.strictEqual(res.status, 400);
assert.strictEqual(res.body.error.code, 'NO_SUCH_USER');
assert.strictEqual(res.body.error.id, 'fcd2eef9-a9b2-4c4f-8624-038099e90aa5');
});
test('Nothing happen when alias duplicated', async () => {
await api('/i/known-as', {
alsoKnownAs: `@alice@${url.hostname}`,
}, bob);
await api('/i/known-as', {
alsoKnownAs: `@alice@${url.hostname}`,
}, 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('Unable to add itself', async () => {
const res = await api('/i/known-as', {
alsoKnownAs: `@bob@${url.hostname}`,
}, bob);
assert.strictEqual(res.status, 400);
assert.strictEqual(res.body.error.code, 'FORBIDDEN_TO_SET_YOURSELF');
assert.strictEqual(res.body.error.id, '25c90186-4ab0-49c8-9bba-a1fa6c202ba4');
});
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/known-as', { const res = await api('/i/known-as', {
alsoKnownAs: `@nonexist@${url.hostname}`, alsoKnownAs: `@nonexist@${url.hostname}`,
@ -72,10 +115,10 @@ describe('Account Move', () => {
alsoKnownAs: `@carol@${url.hostname}`, alsoKnownAs: `@carol@${url.hostname}`,
}, bob); }, bob);
const newAlice = await Users.findOneByOrFail({ id: bob.id }); const newBob = await Users.findOneByOrFail({ id: bob.id });
assert.strictEqual(newAlice.alsoKnownAs?.length, 2); assert.strictEqual(newBob.alsoKnownAs?.length, 2);
assert.strictEqual(newAlice.alsoKnownAs[0], `${url.origin}/users/${alice.id}`); assert.strictEqual(newBob.alsoKnownAs[0], `${url.origin}/users/${alice.id}`);
assert.strictEqual(newAlice.alsoKnownAs[1], `${url.origin}/users/${carol.id}`); assert.strictEqual(newBob.alsoKnownAs[1], `${url.origin}/users/${carol.id}`);
}); });
test('Unable to create an alias without the second @', async () => { test('Unable to create an alias without the second @', async () => {
@ -95,7 +138,7 @@ describe('Account Move', () => {
assert.strictEqual(res2.body.error.code, 'NO_SUCH_USER'); assert.strictEqual(res2.body.error.code, 'NO_SUCH_USER');
assert.strictEqual(res2.body.error.id, 'fcd2eef9-a9b2-4c4f-8624-038099e90aa5'); assert.strictEqual(res2.body.error.id, 'fcd2eef9-a9b2-4c4f-8624-038099e90aa5');
}); });
}) });
describe('Local to Local', () => { describe('Local to Local', () => {
let antennaId = ''; let antennaId = '';

View file

@ -15,7 +15,6 @@ export type UserLite = {
avatarUrl: string; avatarUrl: string;
avatarBlurhash: string; avatarBlurhash: string;
alsoKnownAs: string[]; alsoKnownAs: string[];
movedToUri: any;
emojis: { emojis: {
name: string; name: string;
url: string; url: string;
@ -58,6 +57,7 @@ export type UserDetailed = UserLite & {
lang: string | null; lang: string | null;
lastFetchedAt?: DateString; lastFetchedAt?: DateString;
location: string | null; location: string | null;
movedToUri: string;
notesCount: number; notesCount: number;
pinnedNoteIds: ID[]; pinnedNoteIds: ID[];
pinnedNotes: Note[]; pinnedNotes: Note[];