Merge branch 'develop' into fetch-outbox

This commit is contained in:
Kagami Sascha Rosylight
2023-07-09 15:04:12 +02:00
committed by GitHub
23 changed files with 203 additions and 80 deletions

View File

@@ -177,7 +177,7 @@ export class ApNoteService {
// リプライ
const reply: Note | null = note.inReplyTo
? await this.resolveNote(note.inReplyTo, resolver)
? await this.resolveNote(note.inReplyTo, { resolver })
.then(x => {
if (x == null) {
this.logger.warn('Specified inReplyTo, but not found');
@@ -293,9 +293,8 @@ export class ApNoteService {
* リモートサーバーからフェッチしてMisskeyに登録しそれを返します。
*/
@bindThis
public async resolveNote(value: string | IObject, resolver?: Resolver): Promise<Note | null> {
const uri = typeof value === 'string' ? value : value.id;
if (uri == null) throw new Error('missing uri');
public async resolveNote(value: string | IObject, options: { sentFrom?: URL, resolver?: Resolver } = {}): Promise<Note | null> {
const uri = getApId(value);
// ブロックしていたら中断
const meta = await this.metaService.fetch();
@@ -318,7 +317,8 @@ export class ApNoteService {
// リモートサーバーからフェッチしてきて登録
// ここでuriの代わりに添付されてきたNote Objectが指定されていると、サーバーフェッチを経ずにートが生成されるが
// 添付されてきたNote Objectは偽装されている可能性があるため、常にuriを指定してサーバーフェッチを行う。
return await this.createNote(uri, resolver, true);
const createFrom = options.sentFrom?.origin === new URL(uri).origin ? value : uri;
return await this.createNote(createFrom, options.resolver, true);
} finally {
unlock();
}

View File

@@ -637,7 +637,10 @@ export class ApPersonService implements OnModuleInit {
const featuredNotes = await Promise.all(items
.filter(item => getApType(item) === 'Note') // TODO: Noteでなくてもいいかも
.slice(0, 5)
.map(item => limit(() => this.apNoteService.resolveNote(item, _resolver))));
.map(item => limit(() => this.apNoteService.resolveNote(item, {
resolver: _resolver,
sentFrom: new URL(user.uri),
}))));
await this.db.transaction(async transactionalEntityManager => {
await transactionalEntityManager.delete(UserNotePining, { userId: user.id });

View File

@@ -18,7 +18,8 @@ type MockResponse = {
};
export class MockResolver extends Resolver {
private _rs = new Map<string, MockResponse>();
#responseMap = new Map<string, MockResponse>();
#remoteGetTrials: string[] = [];
constructor(loggerService: LoggerService) {
super(
@@ -38,22 +39,28 @@ export class MockResolver extends Resolver {
);
}
public _register(uri: string, content: string | Record<string, any>, type = 'application/activity+json') {
this._rs.set(uri, {
public register(uri: string, content: string | Record<string, any>, type = 'application/activity+json'): void {
this.#responseMap.set(uri, {
type,
content: typeof content === 'string' ? content : JSON.stringify(content),
});
}
public clear() {
this._rs.clear();
public clear(): void {
this.#responseMap.clear();
this.#remoteGetTrials.length = 0;
}
public remoteGetTrials(): string[] {
return this.#remoteGetTrials;
}
@bindThis
public async resolve(value: string | IObject): Promise<IObject> {
if (typeof value !== 'string') return value;
const r = this._rs.get(value);
this.#remoteGetTrials.push(value);
const r = this.#responseMap.get(value);
if (!r) {
throw new Error('Not registered for mock');

View File

@@ -18,9 +18,12 @@ import { MockResolver } from '../misc/mock-resolver.js';
const host = 'https://host1.test';
function createRandomActor(): IActor & { id: string } {
type NonTransientIActor = IActor & { id: string };
type NonTransientIPost = IPost & { id: string };
function createRandomActor({ actorHost = host } = {}): NonTransientIActor {
const preferredUsername = secureRndstr(8);
const actorId = `${host}/users/${preferredUsername.toLowerCase()}`;
const actorId = `${actorHost}/users/${preferredUsername.toLowerCase()}`;
return {
'@context': 'https://www.w3.org/ns/activitystreams',
@@ -32,26 +35,48 @@ function createRandomActor(): IActor & { id: string } {
};
}
function createRandomCreateActivity(actor: IActor, length: number): ICreate[] {
function createRandomNote(actor: NonTransientIActor): NonTransientIPost {
const id = secureRndstr(8);
const noteId = `${new URL(actor.id).origin}/notes/${id}`;
return {
id: noteId,
type: 'Note',
attributedTo: actor.id,
content: 'test test foo',
};
}
function createRandomNotes(actor: NonTransientIActor, length: number): NonTransientIPost[] {
return new Array(length).fill(null).map(() => createRandomNote(actor));
}
function createRandomFeaturedCollection(actor: NonTransientIActor, length: number): ICollection {
const items = createRandomNotes(actor, length);
return {
'@context': 'https://www.w3.org/ns/activitystreams',
type: 'Collection',
id: actor.outbox as string,
totalItems: items.length,
items,
};
}
function createRandomCreateActivity(actor: NonTransientIActor, length: number): ICreate[] {
return new Array(length).fill(null).map((): ICreate => {
const id = secureRndstr(8);
const noteId = `${host}/notes/${id}`;
const note = reateRandomNote(actor);
return {
type: 'Create',
id: `${noteId}/activity`,
id: `${note.id}/activity`,
actor,
object: {
id: noteId,
type: 'Note',
attributedTo: actor.id,
content: 'test test foo',
} satisfies IPost,
object: note,
};
});
}
function createRandomNonPagedOutbox(actor: IActor, length: number): IOrderedCollection {
function createRandomNonPagedOutbox(actor: NonTransientIActor, length: number): IOrderedCollection {
const orderedItems = createRandomCreateActivity(actor, length);
return {
@@ -63,7 +88,7 @@ function createRandomNonPagedOutbox(actor: IActor, length: number): IOrderedColl
};
}
function createRandomOutboxPage(actor: IActor, id: string, length: number): IOrderedCollectionPage {
function createRandomOutboxPage(actor: NonTransientIActor, id: string, length: number): IOrderedCollectionPage {
const orderedItems = createRandomCreateActivity(actor, length);
return {
@@ -75,14 +100,14 @@ function createRandomOutboxPage(actor: IActor, id: string, length: number): IOrd
};
}
function createRandomPagedOutbox(actor: IActor): IOrderedCollection {
function createRandomPagedOutbox(actor: NonTransientIActor): IOrderedCollection {
return {
'@context': 'https://www.w3.org/ns/activitystreams',
type: 'OrderedCollection',
id: actor.outbox as string,
totalItems: 10,
first: `${actor.outbox}?first`,
};
};
}
describe('ActivityPub', () => {
@@ -126,7 +151,7 @@ describe('ActivityPub', () => {
};
test('Minimum Actor', async () => {
resolver._register(actor.id, actor);
resolver.register(actor.id, actor);
const user = await personService.createPerson(actor.id, resolver);
@@ -136,8 +161,8 @@ describe('ActivityPub', () => {
});
test('Minimum Note', async () => {
resolver._register(actor.id, actor);
resolver._register(post.id, post);
resolver.register(actor.id, actor);
resolver.register(post.id, post);
const note = await noteService.createNote(post.id, resolver, true);
@@ -154,7 +179,7 @@ describe('ActivityPub', () => {
name: secureRndstr(129),
};
resolver._register(actor.id, actor);
resolver.register(actor.id, actor);
const user = await personService.createPerson(actor.id, resolver);
@@ -167,7 +192,7 @@ describe('ActivityPub', () => {
name: '',
};
resolver._register(actor.id, actor);
resolver.register(actor.id, actor);
const user = await personService.createPerson(actor.id, resolver);
@@ -184,13 +209,70 @@ describe('ActivityPub', () => {
});
});
describe('Featured', () => {
test('Fetch featured notes from IActor', async () => {
const actor = createRandomActor();
actor.featured = `${actor.id}/collections/featured`;
const featured = createRandomFeaturedCollection(actor, 5);
resolver.register(actor.id, actor);
resolver.register(actor.featured, featured);
await personService.createPerson(actor.id, resolver);
// All notes in `featured` are same-origin, no need to fetch notes again
assert.deepStrictEqual(resolver.remoteGetTrials(), [actor.id, actor.featured]);
// Created notes without resolving anything
for (const item of featured.items as IPost[]) {
const note = await noteService.fetchNote(item);
assert.ok(note);
assert.strictEqual(note.text, 'test test foo');
assert.strictEqual(note.uri, item.id);
}
});
test('Fetch featured notes from IActor pointing to another remote server', async () => {
const actor1 = createRandomActor();
actor1.featured = `${actor1.id}/collections/featured`;
const actor2 = createRandomActor({ actorHost: 'https://host2.test' });
const actor2Note = createRandomNote(actor2);
const featured = createRandomFeaturedCollection(actor1, 0);
(featured.items as IPost[]).push({
...actor2Note,
content: 'test test bar', // fraud!
});
resolver.register(actor1.id, actor1);
resolver.register(actor1.featured, featured);
resolver.register(actor2.id, actor2);
resolver.register(actor2Note.id, actor2Note);
await personService.createPerson(actor1.id, resolver);
// actor2Note is from a different server and needs to be fetched again
assert.deepStrictEqual(
resolver.remoteGetTrials(),
[actor1.id, actor1.featured, actor2Note.id, actor2.id],
);
const note = await noteService.fetchNote(actor2Note.id);
assert.ok(note);
// Reflects the original content instead of the fraud
assert.strictEqual(note.text, 'test test foo');
assert.strictEqual(note.uri, actor2Note.id);
});
describe('Outbox', () => {
test('Fetch non-paged outbox from IActor', async () => {
const actor = createRandomActor();
const outbox = createRandomNonPagedOutbox(actor, 10);
resolver._register(actor.id, actor);
resolver._register(actor.outbox as string, outbox);
resolver.register(actor.id, actor);
resolver.register(actor.outbox as string, outbox);
await personService.createPerson(actor.id, resolver);
@@ -207,9 +289,9 @@ describe('ActivityPub', () => {
const outbox = createRandomPagedOutbox(actor);
const page = createRandomOutboxPage(actor, outbox.id!, 10);
resolver._register(actor.id, actor);
resolver._register(actor.outbox as string, outbox);
resolver._register(outbox.first as string, page);
resolver.register(actor.id, actor);
resolver.register(actor.outbox as string, outbox);
resolver.register(outbox.first as string, page);
await personService.createPerson(actor.id, resolver);
@@ -221,18 +303,18 @@ describe('ActivityPub', () => {
}
});
test('Fetch only the first 100 items', async () => {
test('Fetch only the first 20 items', async () => {
const actor = createRandomActor();
const outbox = createRandomNonPagedOutbox(actor, 200);
resolver._register(actor.id, actor);
resolver._register(actor.outbox as string, outbox);
resolver.register(actor.id, actor);
resolver.register(actor.outbox as string, outbox);
await personService.createPerson(actor.id, resolver);
const items = outbox.orderedItems as ICreate[];
assert.ok(await noteService.fetchNote(items[19].object));
assert.ok(!await noteService.fetchNote(items[20].object));
});
});
});
});

View File

@@ -1,7 +1,10 @@
import fs from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import path from 'node:path';
import micromatch from 'micromatch';
import main from './main';
import main from './main.js';
const __dirname = fileURLToPath(new URL('.', import.meta.url));
interface Stats {
readonly modules: readonly {
@@ -13,8 +16,8 @@ interface Stats {
}[];
}
fs.readFile(
path.resolve(__dirname, '../storybook-static/preview-stats.json')
await fs.readFile(
new URL('../storybook-static/preview-stats.json', import.meta.url)
).then((buffer) => {
const stats: Stats = JSON.parse(buffer.toString());
const keys = new Set(stats.modules.map((stat) => stat.id));

View File

@@ -1,7 +1,11 @@
import { resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import type { StorybookConfig } from '@storybook/vue3-vite';
import { type Plugin, mergeConfig } from 'vite';
import turbosnap from 'vite-plugin-turbosnap';
const dirname = fileURLToPath(new URL('.', import.meta.url));
const config = {
stories: ['../src/**/*.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'],
addons: [
@@ -9,7 +13,7 @@ const config = {
'@storybook/addon-interactions',
'@storybook/addon-links',
'@storybook/addon-storysource',
resolve(__dirname, '../node_modules/storybook-addon-misskey-theme'),
resolve(dirname, '../node_modules/storybook-addon-misskey-theme'),
],
framework: {
name: '@storybook/vue3-vite',
@@ -28,7 +32,8 @@ const config = {
}
return mergeConfig(config, {
plugins: [
turbosnap({
// XXX: https://github.com/IanVS/vite-plugin-turbosnap/issues/8
(turbosnap as any as typeof turbosnap['default'])({
rootDir: config.root ?? process.cwd(),
}),
],

View File

@@ -0,0 +1,3 @@
{
"type": "module"
}

View File

@@ -1,9 +1,8 @@
import { writeFile } from 'node:fs/promises';
import { resolve } from 'node:path';
import * as locales from '../../../locales';
import * as locales from '../../../locales/index.js';
writeFile(
resolve(__dirname, 'locale.ts'),
await writeFile(
new URL('locale.ts', import.meta.url),
`export default ${JSON.stringify(locales['ja-JP'], undefined, 2)} as const;`,
'utf8',
)

View File

@@ -1,6 +1,5 @@
import { readFile, writeFile } from 'node:fs/promises';
import { resolve } from 'node:path';
import * as JSON5 from 'json5';
import JSON5 from 'json5';
const keys = [
'_dark',
@@ -26,9 +25,9 @@ const keys = [
'd-u0',
]
Promise.all(keys.map((key) => readFile(resolve(__dirname, `../src/themes/${key}.json5`), 'utf8'))).then((sources) => {
await Promise.all(keys.map((key) => readFile(new URL(`../src/themes/${key}.json5`, import.meta.url), 'utf8'))).then((sources) => {
writeFile(
resolve(__dirname, './themes.ts'),
new URL('./themes.ts', import.meta.url),
`export default ${JSON.stringify(
Object.fromEntries(sources.map((source, i) => [keys[i], JSON5.parse(source)])),
undefined,

View File

@@ -3,10 +3,10 @@ import { FORCE_REMOUNT } from '@storybook/core-events';
import { type Preview, setup } from '@storybook/vue3';
import isChromatic from 'chromatic/isChromatic';
import { initialize, mswDecorator } from 'msw-storybook-addon';
import { userDetailed } from './fakes';
import locale from './locale';
import { commonHandlers, onUnhandledRequest } from './mocks';
import themes from './themes';
import { userDetailed } from './fakes.js';
import locale from './locale.js';
import { commonHandlers, onUnhandledRequest } from './mocks.js';
import themes from './themes.js';
import '../src/style.scss';
const appInitialized = Symbol();

View File

@@ -1,5 +1,7 @@
{
"compilerOptions": {
"target": "es2022",
"module": "Node16",
"strict": true,
"allowUnusedLabels": false,
"allowUnreachableCode": false,

View File

@@ -2,13 +2,14 @@ import { defineAsyncComponent } from 'vue';
import * as misskey from 'misskey-js';
import { i18n } from '@/i18n';
import copyToClipboard from '@/scripts/copy-to-clipboard';
import { host } from '@/config';
import { host, url } from '@/config';
import * as os from '@/os';
import { defaultStore, userActions } from '@/store';
import { $i, iAmModerator } from '@/account';
import { mainRouter } from '@/router';
import { Router } from '@/nirax';
import { rolesCache, userListsCache } from '@/cache';
import { toUnicode } from 'punycode';
export function getUserMenu(user: misskey.entities.UserDetailed, router: Router = mainRouter) {
const meId = $i ? $i.id : null;
@@ -137,6 +138,13 @@ export function getUserMenu(user: misskey.entities.UserDetailed, router: Router
action: () => {
copyToClipboard(`${user.host ?? host}/@${user.username}.atom`);
},
}, {
icon: 'ti ti-share',
text: i18n.ts.copyProfileUrl,
action: () => {
const canonical = user.host === null ? `@${user.username}` : `@${user.username}@${toUnicode(user.host)}`
copyToClipboard(`${url}/${canonical}`);
},
}, {
icon: 'ti ti-mail',
text: i18n.ts.sendMessage,

View File

@@ -72,7 +72,7 @@ html {
}
&.useSystemFont {
font-family: 'Hiragino Maru Gothic Pro', sans-serif;
font-family: system-ui;
}
}

View File

@@ -1,10 +1,13 @@
// @ts-check
const esbuild = require('esbuild');
const locales = require('../../locales');
const meta = require('../../package.json');
import { fileURLToPath } from 'node:url';
import * as esbuild from 'esbuild';
import locales from '../../locales/index.js';
import meta from '../../package.json' assert { type: "json" };
const watch = process.argv[2]?.includes('watch');
const __dirname = fileURLToPath(new URL('.', import.meta.url))
console.log('Starting SW building...');
/** @type {esbuild.BuildOptions} */

View File

@@ -19,5 +19,6 @@
"eslint": "8.44.0",
"eslint-plugin-import": "2.27.5",
"typescript": "5.1.6"
}
},
"type": "module"
}