2023-07-27 05:31:52 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
enhance: account migration (#10592)
* copy block and mute then create follow and unfollow jobs
* copy block and mute and update lists when detecting an account has moved
* no need to care promise orders
* refactor updating actor and target
* automatically accept if a locked account had accepted an old account
* fix exception format
* prevent the old account from calling some endpoints
* do not unfollow when moving
* adjust following and follower counts
* check movedToUri when receiving a follow request
* skip if no need to adjust
* Revert "disable account migration"
This reverts commit 2321214c98591bcfe1385c1ab5bf0ff7b471ae1d.
* fix translation specifier
* fix checking alsoKnownAs and uri
* fix updating account
* fix refollowing locked account
* decrease followersCount if followed by the old account
* adjust following and followers counts when unfollowing
* fix copying mutings
* prohibit moved account from moving again
* fix move service
* allow app creation after moving
* fix lint
* remove unnecessary field
* fix cache update
* add e2e test
* add e2e test of accepting the new account automatically
* force follow if any error happens
* remove unnecessary joins
* use Array.map instead of for const of
* ユーザーリストの移行は追加のみを行う
* nanka iroiro
* fix misskey-js?
* :v:
* 移行を行ったアカウントからのフォローリクエストの自動許可を調整
* newUriを外に出す
* newUriを外に出す2
* clean up
* fix newUri
* prevent moving if the destination account has already moved
* set alsoKnownAs via /i/update
* fix database initialization
* add return type
* prohibit updating alsoKnownAs after moving
* skip to add to alsoKnownAs if toUrl is known
* skip adding to the list if it already has
* use Acct.parse instead
* rename error code
* :art:
* 制限を5から10に緩和
* movedTo(Uri), alsoKnownAsはユーザーidを返すように
* test api res
* fix
* 元アカウントはミュートし続ける
* :art:
* unfollow
* fix
* getUserUriをUserEntityServiceに
* ?
* job!
* :art:
* instance => server
* accountMovedShort, forbiddenBecauseYouAreMigrated
* accountMovedShort
* fix test
* import, pin禁止
* 実績を凍結する
* clean up
* :v:
* change message
* ブロック, フォロー, ミュート, リストのインポートファイルの制限を32MiBに
* Revert "ブロック, フォロー, ミュート, リストのインポートファイルの制限を32MiBに"
This reverts commit 3bd7be35d8aa455cb01ae58f8172a71a50485db1.
* validateAlsoKnownAs
* 移行後2時間以内はインポート可能なファイルサイズを拡大
* clean up
* どうせactorをupdatePersonで更新するならupdatePersonしか移行処理を発行しないことにする
* handle error?
* リモートからの移行処理の条件を是正
* log, port
* fix
* fix
* enhance(dev): non-production環境でhttpサーバー間でもユーザー、ノートの連合が可能なように
* refactor (use checkHttps)
* MISSKEY_WEBFINGER_USE_HTTP
* Environment Variable readme
* NEVER USE IN PRODUCTION
* fix punyHost
* fix indent
* fix
* experimental
---------
Co-authored-by: tamaina <tamaina@hotmail.co.jp>
Co-authored-by: syuilo <Syuilotan@yahoo.co.jp>
2023-04-29 15:09:29 +00:00
|
|
|
import type { LocalUser, RemoteUser } from '@/models/entities/User.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { InstanceActorService } from '@/core/InstanceActorService.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { NotesRepository, PollsRepository, NoteReactionsRepository, UsersRepository } from '@/models/index.js';
|
|
|
|
import type { Config } from '@/config.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
2023-01-25 03:00:04 +00:00
|
|
|
import { HttpRequestService } from '@/core/HttpRequestService.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
import { UtilityService } from '@/core/UtilityService.js';
|
2022-12-04 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-01-23 23:31:02 +00:00
|
|
|
import { LoggerService } from '@/core/LoggerService.js';
|
|
|
|
import type Logger from '@/logger.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { isCollectionOrOrderedCollection } from './type.js';
|
|
|
|
import { ApDbResolverService } from './ApDbResolverService.js';
|
|
|
|
import { ApRendererService } from './ApRendererService.js';
|
|
|
|
import { ApRequestService } from './ApRequestService.js';
|
|
|
|
import type { IObject, ICollection, IOrderedCollection } from './type.js';
|
|
|
|
|
|
|
|
export class Resolver {
|
|
|
|
private history: Set<string>;
|
2023-02-13 06:50:22 +00:00
|
|
|
private user?: LocalUser;
|
2023-01-12 12:03:02 +00:00
|
|
|
private logger: Logger;
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
private config: Config,
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
private pollsRepository: PollsRepository,
|
|
|
|
private noteReactionsRepository: NoteReactionsRepository,
|
|
|
|
private utilityService: UtilityService,
|
|
|
|
private instanceActorService: InstanceActorService,
|
|
|
|
private metaService: MetaService,
|
|
|
|
private apRequestService: ApRequestService,
|
|
|
|
private httpRequestService: HttpRequestService,
|
|
|
|
private apRendererService: ApRendererService,
|
|
|
|
private apDbResolverService: ApDbResolverService,
|
2023-01-12 12:03:02 +00:00
|
|
|
private loggerService: LoggerService,
|
2022-12-04 06:03:09 +00:00
|
|
|
private recursionLimit = 100,
|
2022-09-17 18:27:08 +00:00
|
|
|
) {
|
|
|
|
this.history = new Set();
|
2023-02-12 09:47:30 +00:00
|
|
|
this.logger = this.loggerService.getLogger('ap-resolve');
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public getHistory(): string[] {
|
|
|
|
return Array.from(this.history);
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async resolveCollection(value: string | IObject): Promise<ICollection | IOrderedCollection> {
|
|
|
|
const collection = typeof value === 'string'
|
|
|
|
? await this.resolve(value)
|
|
|
|
: value;
|
|
|
|
|
|
|
|
if (isCollectionOrOrderedCollection(collection)) {
|
|
|
|
return collection;
|
|
|
|
} else {
|
|
|
|
throw new Error(`unrecognized collection type: ${collection.type}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async resolve(value: string | IObject): Promise<IObject> {
|
|
|
|
if (typeof value !== 'string') {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value.includes('#')) {
|
|
|
|
// URLs with fragment parts cannot be resolved correctly because
|
|
|
|
// the fragment part does not get transmitted over HTTP(S).
|
|
|
|
// Avoid strange behaviour by not trying to resolve these at all.
|
|
|
|
throw new Error(`cannot resolve URL with fragment: ${value}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.history.has(value)) {
|
|
|
|
throw new Error('cannot resolve already resolved one');
|
|
|
|
}
|
|
|
|
|
2022-12-02 21:13:36 +00:00
|
|
|
if (this.history.size > this.recursionLimit) {
|
|
|
|
throw new Error(`hit recursion limit: ${this.utilityService.extractDbHost(value)}`);
|
|
|
|
}
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
this.history.add(value);
|
|
|
|
|
|
|
|
const host = this.utilityService.extractDbHost(value);
|
|
|
|
if (this.utilityService.isSelfHost(host)) {
|
|
|
|
return await this.resolveLocal(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
const meta = await this.metaService.fetch();
|
2023-01-13 09:21:07 +00:00
|
|
|
if (this.utilityService.isBlockedHost(meta.blockedHosts, host)) {
|
2022-09-17 18:27:08 +00:00
|
|
|
throw new Error('Instance is blocked');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.config.signToActivityPubGet && !this.user) {
|
|
|
|
this.user = await this.instanceActorService.getInstanceActor();
|
|
|
|
}
|
|
|
|
|
|
|
|
const object = (this.user
|
2023-01-12 12:03:02 +00:00
|
|
|
? await this.apRequestService.signedGet(value, this.user) as IObject
|
2023-01-25 03:00:04 +00:00
|
|
|
: await this.httpRequestService.getJson(value, 'application/activity+json, application/ld+json')) as IObject;
|
2022-09-17 18:27:08 +00:00
|
|
|
|
2023-07-13 03:48:34 +00:00
|
|
|
if (
|
2022-09-17 18:27:08 +00:00
|
|
|
Array.isArray(object['@context']) ?
|
|
|
|
!(object['@context'] as unknown[]).includes('https://www.w3.org/ns/activitystreams') :
|
|
|
|
object['@context'] !== 'https://www.w3.org/ns/activitystreams'
|
2023-07-13 03:48:34 +00:00
|
|
|
) {
|
2022-09-17 18:27:08 +00:00
|
|
|
throw new Error('invalid response');
|
|
|
|
}
|
|
|
|
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
private resolveLocal(url: string): Promise<IObject> {
|
|
|
|
const parsed = this.apDbResolverService.parseUri(url);
|
|
|
|
if (!parsed.local) throw new Error('resolveLocal: not local');
|
|
|
|
|
|
|
|
switch (parsed.type) {
|
|
|
|
case 'notes':
|
|
|
|
return this.notesRepository.findOneByOrFail({ id: parsed.id })
|
2023-02-12 09:47:30 +00:00
|
|
|
.then(async note => {
|
2022-09-17 18:27:08 +00:00
|
|
|
if (parsed.rest === 'activity') {
|
2022-09-23 23:41:27 +00:00
|
|
|
// this refers to the create activity and not the note itself
|
2023-02-12 09:47:30 +00:00
|
|
|
return this.apRendererService.addContext(this.apRendererService.renderCreate(await this.apRendererService.renderNote(note), note));
|
2022-09-17 18:27:08 +00:00
|
|
|
} else {
|
|
|
|
return this.apRendererService.renderNote(note);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
case 'users':
|
|
|
|
return this.usersRepository.findOneByOrFail({ id: parsed.id })
|
2023-02-13 06:50:22 +00:00
|
|
|
.then(user => this.apRendererService.renderPerson(user as LocalUser));
|
2022-09-17 18:27:08 +00:00
|
|
|
case 'questions':
|
|
|
|
// Polls are indexed by the note they are attached to.
|
|
|
|
return Promise.all([
|
|
|
|
this.notesRepository.findOneByOrFail({ id: parsed.id }),
|
|
|
|
this.pollsRepository.findOneByOrFail({ noteId: parsed.id }),
|
|
|
|
])
|
|
|
|
.then(([note, poll]) => this.apRendererService.renderQuestion({ id: note.userId }, note, poll));
|
|
|
|
case 'likes':
|
2023-02-12 09:47:30 +00:00
|
|
|
return this.noteReactionsRepository.findOneByOrFail({ id: parsed.id }).then(async reaction =>
|
|
|
|
this.apRendererService.addContext(await this.apRendererService.renderLike(reaction, { uri: null })));
|
2022-09-17 18:27:08 +00:00
|
|
|
case 'follows':
|
|
|
|
// rest should be <followee id>
|
|
|
|
if (parsed.rest == null || !/^\w+$/.test(parsed.rest)) throw new Error('resolveLocal: invalid follow URI');
|
|
|
|
|
|
|
|
return Promise.all(
|
|
|
|
[parsed.id, parsed.rest].map(id => this.usersRepository.findOneByOrFail({ id })),
|
|
|
|
)
|
enhance: account migration (#10592)
* copy block and mute then create follow and unfollow jobs
* copy block and mute and update lists when detecting an account has moved
* no need to care promise orders
* refactor updating actor and target
* automatically accept if a locked account had accepted an old account
* fix exception format
* prevent the old account from calling some endpoints
* do not unfollow when moving
* adjust following and follower counts
* check movedToUri when receiving a follow request
* skip if no need to adjust
* Revert "disable account migration"
This reverts commit 2321214c98591bcfe1385c1ab5bf0ff7b471ae1d.
* fix translation specifier
* fix checking alsoKnownAs and uri
* fix updating account
* fix refollowing locked account
* decrease followersCount if followed by the old account
* adjust following and followers counts when unfollowing
* fix copying mutings
* prohibit moved account from moving again
* fix move service
* allow app creation after moving
* fix lint
* remove unnecessary field
* fix cache update
* add e2e test
* add e2e test of accepting the new account automatically
* force follow if any error happens
* remove unnecessary joins
* use Array.map instead of for const of
* ユーザーリストの移行は追加のみを行う
* nanka iroiro
* fix misskey-js?
* :v:
* 移行を行ったアカウントからのフォローリクエストの自動許可を調整
* newUriを外に出す
* newUriを外に出す2
* clean up
* fix newUri
* prevent moving if the destination account has already moved
* set alsoKnownAs via /i/update
* fix database initialization
* add return type
* prohibit updating alsoKnownAs after moving
* skip to add to alsoKnownAs if toUrl is known
* skip adding to the list if it already has
* use Acct.parse instead
* rename error code
* :art:
* 制限を5から10に緩和
* movedTo(Uri), alsoKnownAsはユーザーidを返すように
* test api res
* fix
* 元アカウントはミュートし続ける
* :art:
* unfollow
* fix
* getUserUriをUserEntityServiceに
* ?
* job!
* :art:
* instance => server
* accountMovedShort, forbiddenBecauseYouAreMigrated
* accountMovedShort
* fix test
* import, pin禁止
* 実績を凍結する
* clean up
* :v:
* change message
* ブロック, フォロー, ミュート, リストのインポートファイルの制限を32MiBに
* Revert "ブロック, フォロー, ミュート, リストのインポートファイルの制限を32MiBに"
This reverts commit 3bd7be35d8aa455cb01ae58f8172a71a50485db1.
* validateAlsoKnownAs
* 移行後2時間以内はインポート可能なファイルサイズを拡大
* clean up
* どうせactorをupdatePersonで更新するならupdatePersonしか移行処理を発行しないことにする
* handle error?
* リモートからの移行処理の条件を是正
* log, port
* fix
* fix
* enhance(dev): non-production環境でhttpサーバー間でもユーザー、ノートの連合が可能なように
* refactor (use checkHttps)
* MISSKEY_WEBFINGER_USE_HTTP
* Environment Variable readme
* NEVER USE IN PRODUCTION
* fix punyHost
* fix indent
* fix
* experimental
---------
Co-authored-by: tamaina <tamaina@hotmail.co.jp>
Co-authored-by: syuilo <Syuilotan@yahoo.co.jp>
2023-04-29 15:09:29 +00:00
|
|
|
.then(([follower, followee]) => this.apRendererService.addContext(this.apRendererService.renderFollow(follower as LocalUser | RemoteUser, followee as LocalUser | RemoteUser, url)));
|
2022-09-17 18:27:08 +00:00
|
|
|
default:
|
2022-09-23 23:41:27 +00:00
|
|
|
throw new Error(`resolveLocal: type ${parsed.type} unhandled`);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-04 06:03:09 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class ApResolverService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.pollsRepository)
|
|
|
|
private pollsRepository: PollsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.noteReactionsRepository)
|
|
|
|
private noteReactionsRepository: NoteReactionsRepository,
|
|
|
|
|
|
|
|
private utilityService: UtilityService,
|
|
|
|
private instanceActorService: InstanceActorService,
|
|
|
|
private metaService: MetaService,
|
|
|
|
private apRequestService: ApRequestService,
|
|
|
|
private httpRequestService: HttpRequestService,
|
|
|
|
private apRendererService: ApRendererService,
|
|
|
|
private apDbResolverService: ApDbResolverService,
|
2023-02-12 09:47:30 +00:00
|
|
|
private loggerService: LoggerService,
|
2022-12-04 06:03:09 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public createResolver(): Resolver {
|
|
|
|
return new Resolver(
|
|
|
|
this.config,
|
|
|
|
this.usersRepository,
|
|
|
|
this.notesRepository,
|
|
|
|
this.pollsRepository,
|
|
|
|
this.noteReactionsRepository,
|
|
|
|
this.utilityService,
|
|
|
|
this.instanceActorService,
|
|
|
|
this.metaService,
|
|
|
|
this.apRequestService,
|
|
|
|
this.httpRequestService,
|
|
|
|
this.apRendererService,
|
|
|
|
this.apDbResolverService,
|
2023-02-12 09:47:30 +00:00
|
|
|
this.loggerService,
|
2022-12-04 06:03:09 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|