* 1. ed25519キーペアを発行・Personとして公開鍵を送受信 * validate additionalPublicKeys * getAuthUserFromApIdはmainを選ぶ * ✌️ * fix * signatureAlgorithm * set publicKeyCache lifetime * refresh * httpMessageSignatureAcceptable * ED25519_SIGNED_ALGORITHM * ED25519_PUBLIC_KEY_SIGNATURE_ALGORITHM * remove sign additionalPublicKeys signature requirements * httpMessageSignaturesSupported * httpMessageSignaturesImplementationLevel * httpMessageSignaturesImplementationLevel: '01' * perf(federation): Use hint for getAuthUserFromApId (#13470) * Hint for getAuthUserFromApId * とどのつまりこれでいいのか? * use @misskey-dev/node-http-message-signatures * fix * signedPost, signedGet * ap-request.tsを復活させる * remove digest prerender * fix test? * fix test * add httpMessageSignaturesImplementationLevel to FederationInstance * ManyToOne * fetchPersonWithRenewal * exactKey * ✌️ * use const * use gen-key-pair fn. from '@misskey-dev/node-http-message-signatures' * update node-http-message-signatures * fix * @misskey-dev/node-http-message-signatures@0.0.0-alpha.11 * getAuthUserFromApIdでupdatePersonの頻度を増やす * cacheRaw.date * use requiredInputs https://github.com/misskey-dev/misskey/pull/13464#discussion_r1509964359 * update @misskey-dev/node-http-message-signatures * clean up * err msg * fix(backend): fetchInstanceMetadataのLockが永遠に解除されない問題を修正 Co-authored-by: まっちゃとーにゅ <17376330+u1-liquid@users.noreply.github.com> * fix httpMessageSignaturesImplementationLevel validation * fix test * fix * comment * comment * improve test * fix * use Promise.all in genRSAAndEd25519KeyPair * refreshAndprepareEd25519KeyPair * refreshAndfindKey * commetn * refactor public keys add * digestプリレンダを復活させる RFC実装時にどうするか考える * fix, async * fix * !== true * use save * Deliver update person when new key generated (not tested) https://github.com/misskey-dev/misskey/pull/13464#issuecomment-1977049061 * 循環参照で落ちるのを解消? * fix? * Revert "fix?" This reverts commit 0082f6f8e8c5d5febd14933ba9a1ac643f70ca92. * a * logger * log * change logger * 秘密鍵の変更は、フラグではなく鍵を引き回すようにする * addAllKnowingSharedInboxRecipe * nanka meccha kaeta * delivre * キャッシュ有効チェックはロック取得前に行う * @misskey-dev/node-http-message-signatures@0.0.3 * PrivateKeyPem * getLocalUserPrivateKey * fix test * if * fix ap-request * update node-http-message-signatures * fix type error * update package * fix type * update package * retry no key * @misskey-dev/node-http-message-signatures@0.0.8 * fix type error * log keyid * logger * db-resolver * JSON.stringify * HTTP Signatureがなかったり使えなかったりしそうな場合にLD Signatureを活用するように * inbox-delayed use actor if no signature * ユーザーとキーの同一性チェックはhostの一致にする * log signature parse err * save array * とりあえずtryで囲っておく * fetchPersonWithRenewalでエラーが起きたら古いデータを返す * use transactionalEntityManager * fix spdx * @misskey-dev/node-http-message-signatures@0.0.10 * add comment * fix * publicKeyに配列が入ってもいいようにする https://github.com/misskey-dev/misskey/pull/13950 * define additionalPublicKeys * fix * merge fix * refreshAndprepareEd25519KeyPair → refreshAndPrepareEd25519KeyPair * remove gen-key-pair.ts * defaultMaxListeners = 512 * Revert "defaultMaxListeners = 512" This reverts commit f2c412c18057a9300540794ccbe4dfbf6d259ed6. * genRSAAndEd25519KeyPairではキーを直列に生成する? * maxConcurrency: 8 * maxConcurrency: 16 * maxConcurrency: 8 * Revert "genRSAAndEd25519KeyPairではキーを直列に生成する?" This reverts commit d0aada55c1ed5aa98f18731ec82f3ac5eb5a6c16. * maxWorkers: '90%' * Revert "maxWorkers: '90%'" This reverts commit 9e0a93f110456320d6485a871f014f7cdab29b33. * e2e/timelines.tsで個々のテストに対するtimeoutを削除, maxConcurrency: 32 * better error handling of this.userPublickeysRepository.delete * better comment * set result to keypairEntityCache * deliverJobConcurrency: 16, deliverJobPerSec: 1024, inboxJobConcurrency: 4 * inboxJobPerSec: 64 * delete request.headers['host']; * fix * // node-fetch will generate this for us. if we keep 'Host', it won't change with redirects! * move delete host * modify comment * modify comment * fix correct → collect * refreshAndfindKey → refreshAndFindKey * modify comment * modify attachLdSignature * getApId, InboxProcessorService * TODO * [skip ci] add CHANGELOG --------- Co-authored-by: MeiMei <30769358+mei23@users.noreply.github.com> Co-authored-by: まっちゃとーにゅ <17376330+u1-liquid@users.noreply.github.com>
517 lines
13 KiB
TypeScript
517 lines
13 KiB
TypeScript
/*
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
import { randomUUID } from 'node:crypto';
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
import type { IActivity } from '@/core/activitypub/type.js';
|
|
import type { MiDriveFile } from '@/models/DriveFile.js';
|
|
import type { MiWebhook, webhookEventTypes } from '@/models/Webhook.js';
|
|
import type { MiSystemWebhook, SystemWebhookEventType } from '@/models/SystemWebhook.js';
|
|
import type { Config } from '@/config.js';
|
|
import { DI } from '@/di-symbols.js';
|
|
import { bindThis } from '@/decorators.js';
|
|
import type { Antenna } from '@/server/api/endpoints/i/import-antennas.js';
|
|
import type {
|
|
DbJobData,
|
|
DeliverJobData,
|
|
RelationshipJobData,
|
|
SystemWebhookDeliverJobData,
|
|
ThinUser,
|
|
UserWebhookDeliverJobData,
|
|
} from '../queue/types.js';
|
|
import type {
|
|
DbQueue,
|
|
DeliverQueue,
|
|
EndedPollNotificationQueue,
|
|
InboxQueue,
|
|
ObjectStorageQueue,
|
|
RelationshipQueue,
|
|
SystemQueue,
|
|
UserWebhookDeliverQueue,
|
|
SystemWebhookDeliverQueue,
|
|
} from './QueueModule.js';
|
|
import { genRFC3230DigestHeader, type PrivateKeyWithPem, type ParsedSignature } from '@misskey-dev/node-http-message-signatures';
|
|
import type * as Bull from 'bullmq';
|
|
|
|
@Injectable()
|
|
export class QueueService {
|
|
constructor(
|
|
@Inject(DI.config)
|
|
private config: Config,
|
|
|
|
@Inject('queue:system') public systemQueue: SystemQueue,
|
|
@Inject('queue:endedPollNotification') public endedPollNotificationQueue: EndedPollNotificationQueue,
|
|
@Inject('queue:deliver') public deliverQueue: DeliverQueue,
|
|
@Inject('queue:inbox') public inboxQueue: InboxQueue,
|
|
@Inject('queue:db') public dbQueue: DbQueue,
|
|
@Inject('queue:relationship') public relationshipQueue: RelationshipQueue,
|
|
@Inject('queue:objectStorage') public objectStorageQueue: ObjectStorageQueue,
|
|
@Inject('queue:userWebhookDeliver') public userWebhookDeliverQueue: UserWebhookDeliverQueue,
|
|
@Inject('queue:systemWebhookDeliver') public systemWebhookDeliverQueue: SystemWebhookDeliverQueue,
|
|
) {
|
|
this.systemQueue.add('tickCharts', {
|
|
}, {
|
|
repeat: { pattern: '55 * * * *' },
|
|
removeOnComplete: true,
|
|
});
|
|
|
|
this.systemQueue.add('resyncCharts', {
|
|
}, {
|
|
repeat: { pattern: '0 0 * * *' },
|
|
removeOnComplete: true,
|
|
});
|
|
|
|
this.systemQueue.add('cleanCharts', {
|
|
}, {
|
|
repeat: { pattern: '0 0 * * *' },
|
|
removeOnComplete: true,
|
|
});
|
|
|
|
this.systemQueue.add('aggregateRetention', {
|
|
}, {
|
|
repeat: { pattern: '0 0 * * *' },
|
|
removeOnComplete: true,
|
|
});
|
|
|
|
this.systemQueue.add('clean', {
|
|
}, {
|
|
repeat: { pattern: '0 0 * * *' },
|
|
removeOnComplete: true,
|
|
});
|
|
|
|
this.systemQueue.add('checkExpiredMutings', {
|
|
}, {
|
|
repeat: { pattern: '*/5 * * * *' },
|
|
removeOnComplete: true,
|
|
});
|
|
}
|
|
|
|
@bindThis
|
|
public async deliver(user: ThinUser, content: IActivity | null, to: string | null, isSharedInbox: boolean, privateKey?: PrivateKeyWithPem) {
|
|
if (content == null) return null;
|
|
if (to == null) return null;
|
|
|
|
const contentBody = JSON.stringify(content);
|
|
|
|
const data: DeliverJobData = {
|
|
user: {
|
|
id: user.id,
|
|
},
|
|
content: contentBody,
|
|
digest: await genRFC3230DigestHeader(contentBody, 'SHA-256'),
|
|
to,
|
|
isSharedInbox,
|
|
privateKey: privateKey && { keyId: privateKey.keyId, privateKeyPem: privateKey.privateKeyPem },
|
|
};
|
|
|
|
return this.deliverQueue.add(to, data, {
|
|
attempts: this.config.deliverJobMaxAttempts ?? 12,
|
|
backoff: {
|
|
type: 'custom',
|
|
},
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* ApDeliverManager-DeliverManager.execute()からinboxesを突っ込んでaddBulkしたい
|
|
* @param user `{ id: string; }` この関数ではThinUserに変換しないので前もって変換してください
|
|
* @param content IActivity | null
|
|
* @param inboxes `Map<string, boolean>` / key: to (inbox url), value: isSharedInbox (whether it is sharedInbox)
|
|
* @param forceMainKey boolean | undefined, force to use main (rsa) key
|
|
* @returns void
|
|
*/
|
|
@bindThis
|
|
public async deliverMany(user: ThinUser, content: IActivity | null, inboxes: Map<string, boolean>, privateKey?: PrivateKeyWithPem) {
|
|
if (content == null) return null;
|
|
const contentBody = JSON.stringify(content);
|
|
|
|
const opts = {
|
|
attempts: this.config.deliverJobMaxAttempts ?? 12,
|
|
backoff: {
|
|
type: 'custom',
|
|
},
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
};
|
|
|
|
await this.deliverQueue.addBulk(Array.from(inboxes.entries(), d => ({
|
|
name: d[0],
|
|
data: {
|
|
user,
|
|
content: contentBody,
|
|
to: d[0],
|
|
isSharedInbox: d[1],
|
|
privateKey: privateKey && { keyId: privateKey.keyId, privateKeyPem: privateKey.privateKeyPem },
|
|
} as DeliverJobData,
|
|
opts,
|
|
})));
|
|
|
|
return;
|
|
}
|
|
|
|
@bindThis
|
|
public inbox(activity: IActivity, signature: ParsedSignature | null) {
|
|
const data = {
|
|
activity: activity,
|
|
signature,
|
|
};
|
|
|
|
return this.inboxQueue.add('', data, {
|
|
attempts: this.config.inboxJobMaxAttempts ?? 8,
|
|
backoff: {
|
|
type: 'custom',
|
|
},
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
});
|
|
}
|
|
|
|
@bindThis
|
|
public createDeleteDriveFilesJob(user: ThinUser) {
|
|
return this.dbQueue.add('deleteDriveFiles', {
|
|
user: { id: user.id },
|
|
}, {
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
});
|
|
}
|
|
|
|
@bindThis
|
|
public createExportCustomEmojisJob(user: ThinUser) {
|
|
return this.dbQueue.add('exportCustomEmojis', {
|
|
user: { id: user.id },
|
|
}, {
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
});
|
|
}
|
|
|
|
@bindThis
|
|
public createExportNotesJob(user: ThinUser) {
|
|
return this.dbQueue.add('exportNotes', {
|
|
user: { id: user.id },
|
|
}, {
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
});
|
|
}
|
|
|
|
@bindThis
|
|
public createExportClipsJob(user: ThinUser) {
|
|
return this.dbQueue.add('exportClips', {
|
|
user: { id: user.id },
|
|
}, {
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
});
|
|
}
|
|
|
|
@bindThis
|
|
public createExportFavoritesJob(user: ThinUser) {
|
|
return this.dbQueue.add('exportFavorites', {
|
|
user: { id: user.id },
|
|
}, {
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
});
|
|
}
|
|
|
|
@bindThis
|
|
public createExportFollowingJob(user: ThinUser, excludeMuting = false, excludeInactive = false) {
|
|
return this.dbQueue.add('exportFollowing', {
|
|
user: { id: user.id },
|
|
excludeMuting,
|
|
excludeInactive,
|
|
}, {
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
});
|
|
}
|
|
|
|
@bindThis
|
|
public createExportMuteJob(user: ThinUser) {
|
|
return this.dbQueue.add('exportMuting', {
|
|
user: { id: user.id },
|
|
}, {
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
});
|
|
}
|
|
|
|
@bindThis
|
|
public createExportBlockingJob(user: ThinUser) {
|
|
return this.dbQueue.add('exportBlocking', {
|
|
user: { id: user.id },
|
|
}, {
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
});
|
|
}
|
|
|
|
@bindThis
|
|
public createExportUserListsJob(user: ThinUser) {
|
|
return this.dbQueue.add('exportUserLists', {
|
|
user: { id: user.id },
|
|
}, {
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
});
|
|
}
|
|
|
|
@bindThis
|
|
public createExportAntennasJob(user: ThinUser) {
|
|
return this.dbQueue.add('exportAntennas', {
|
|
user: { id: user.id },
|
|
}, {
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
});
|
|
}
|
|
|
|
@bindThis
|
|
public createImportFollowingJob(user: ThinUser, fileId: MiDriveFile['id'], withReplies?: boolean) {
|
|
return this.dbQueue.add('importFollowing', {
|
|
user: { id: user.id },
|
|
fileId: fileId,
|
|
withReplies,
|
|
}, {
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
});
|
|
}
|
|
|
|
@bindThis
|
|
public createImportFollowingToDbJob(user: ThinUser, targets: string[], withReplies?: boolean) {
|
|
const jobs = targets.map(rel => this.generateToDbJobData('importFollowingToDb', { user, target: rel, withReplies }));
|
|
return this.dbQueue.addBulk(jobs);
|
|
}
|
|
|
|
@bindThis
|
|
public createImportMutingJob(user: ThinUser, fileId: MiDriveFile['id']) {
|
|
return this.dbQueue.add('importMuting', {
|
|
user: { id: user.id },
|
|
fileId: fileId,
|
|
}, {
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
});
|
|
}
|
|
|
|
@bindThis
|
|
public createImportBlockingJob(user: ThinUser, fileId: MiDriveFile['id']) {
|
|
return this.dbQueue.add('importBlocking', {
|
|
user: { id: user.id },
|
|
fileId: fileId,
|
|
}, {
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
});
|
|
}
|
|
|
|
@bindThis
|
|
public createImportBlockingToDbJob(user: ThinUser, targets: string[]) {
|
|
const jobs = targets.map(rel => this.generateToDbJobData('importBlockingToDb', { user, target: rel }));
|
|
return this.dbQueue.addBulk(jobs);
|
|
}
|
|
|
|
@bindThis
|
|
private generateToDbJobData<T extends 'importFollowingToDb' | 'importBlockingToDb', D extends DbJobData<T>>(name: T, data: D): {
|
|
name: string,
|
|
data: D,
|
|
opts: Bull.JobsOptions,
|
|
} {
|
|
return {
|
|
name,
|
|
data,
|
|
opts: {
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
},
|
|
};
|
|
}
|
|
|
|
@bindThis
|
|
public createImportUserListsJob(user: ThinUser, fileId: MiDriveFile['id']) {
|
|
return this.dbQueue.add('importUserLists', {
|
|
user: { id: user.id },
|
|
fileId: fileId,
|
|
}, {
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
});
|
|
}
|
|
|
|
@bindThis
|
|
public createImportCustomEmojisJob(user: ThinUser, fileId: MiDriveFile['id']) {
|
|
return this.dbQueue.add('importCustomEmojis', {
|
|
user: { id: user.id },
|
|
fileId: fileId,
|
|
}, {
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
});
|
|
}
|
|
|
|
@bindThis
|
|
public createImportAntennasJob(user: ThinUser, antenna: Antenna) {
|
|
return this.dbQueue.add('importAntennas', {
|
|
user: { id: user.id },
|
|
antenna,
|
|
}, {
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
});
|
|
}
|
|
|
|
@bindThis
|
|
public createDeleteAccountJob(user: ThinUser, opts: { soft?: boolean; } = {}) {
|
|
return this.dbQueue.add('deleteAccount', {
|
|
user: { id: user.id },
|
|
soft: opts.soft,
|
|
}, {
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
});
|
|
}
|
|
|
|
@bindThis
|
|
public createFollowJob(followings: { from: ThinUser, to: ThinUser, requestId?: string, silent?: boolean, withReplies?: boolean }[]) {
|
|
const jobs = followings.map(rel => this.generateRelationshipJobData('follow', rel));
|
|
return this.relationshipQueue.addBulk(jobs);
|
|
}
|
|
|
|
@bindThis
|
|
public createUnfollowJob(followings: { from: ThinUser, to: ThinUser, requestId?: string }[]) {
|
|
const jobs = followings.map(rel => this.generateRelationshipJobData('unfollow', rel));
|
|
return this.relationshipQueue.addBulk(jobs);
|
|
}
|
|
|
|
@bindThis
|
|
public createDelayedUnfollowJob(followings: { from: ThinUser, to: ThinUser, requestId?: string }[], delay: number) {
|
|
const jobs = followings.map(rel => this.generateRelationshipJobData('unfollow', rel, { delay }));
|
|
return this.relationshipQueue.addBulk(jobs);
|
|
}
|
|
|
|
@bindThis
|
|
public createBlockJob(blockings: { from: ThinUser, to: ThinUser, silent?: boolean }[]) {
|
|
const jobs = blockings.map(rel => this.generateRelationshipJobData('block', rel));
|
|
return this.relationshipQueue.addBulk(jobs);
|
|
}
|
|
|
|
@bindThis
|
|
public createUnblockJob(blockings: { from: ThinUser, to: ThinUser, silent?: boolean }[]) {
|
|
const jobs = blockings.map(rel => this.generateRelationshipJobData('unblock', rel));
|
|
return this.relationshipQueue.addBulk(jobs);
|
|
}
|
|
|
|
@bindThis
|
|
private generateRelationshipJobData(name: 'follow' | 'unfollow' | 'block' | 'unblock', data: RelationshipJobData, opts: Bull.JobsOptions = {}): {
|
|
name: string,
|
|
data: RelationshipJobData,
|
|
opts: Bull.JobsOptions,
|
|
} {
|
|
return {
|
|
name,
|
|
data: {
|
|
from: { id: data.from.id },
|
|
to: { id: data.to.id },
|
|
silent: data.silent,
|
|
requestId: data.requestId,
|
|
withReplies: data.withReplies,
|
|
},
|
|
opts: {
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
...opts,
|
|
},
|
|
};
|
|
}
|
|
|
|
@bindThis
|
|
public createDeleteObjectStorageFileJob(key: string) {
|
|
return this.objectStorageQueue.add('deleteFile', {
|
|
key: key,
|
|
}, {
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
});
|
|
}
|
|
|
|
@bindThis
|
|
public createCleanRemoteFilesJob() {
|
|
return this.objectStorageQueue.add('cleanRemoteFiles', {}, {
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @see UserWebhookDeliverJobData
|
|
* @see WebhookDeliverProcessorService
|
|
*/
|
|
@bindThis
|
|
public userWebhookDeliver(webhook: MiWebhook, type: typeof webhookEventTypes[number], content: unknown) {
|
|
const data: UserWebhookDeliverJobData = {
|
|
type,
|
|
content,
|
|
webhookId: webhook.id,
|
|
userId: webhook.userId,
|
|
to: webhook.url,
|
|
secret: webhook.secret,
|
|
createdAt: Date.now(),
|
|
eventId: randomUUID(),
|
|
};
|
|
|
|
return this.userWebhookDeliverQueue.add(webhook.id, data, {
|
|
attempts: 4,
|
|
backoff: {
|
|
type: 'custom',
|
|
},
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @see SystemWebhookDeliverJobData
|
|
* @see WebhookDeliverProcessorService
|
|
*/
|
|
@bindThis
|
|
public systemWebhookDeliver(webhook: MiSystemWebhook, type: SystemWebhookEventType, content: unknown) {
|
|
const data: SystemWebhookDeliverJobData = {
|
|
type,
|
|
content,
|
|
webhookId: webhook.id,
|
|
to: webhook.url,
|
|
secret: webhook.secret,
|
|
createdAt: Date.now(),
|
|
eventId: randomUUID(),
|
|
};
|
|
|
|
return this.systemWebhookDeliverQueue.add(webhook.id, data, {
|
|
attempts: 4,
|
|
backoff: {
|
|
type: 'custom',
|
|
},
|
|
removeOnComplete: true,
|
|
removeOnFail: true,
|
|
});
|
|
}
|
|
|
|
@bindThis
|
|
public destroy() {
|
|
this.deliverQueue.once('cleaned', (jobs, status) => {
|
|
//deliverLogger.succ(`Cleaned ${jobs.length} ${status} jobs`);
|
|
});
|
|
this.deliverQueue.clean(0, 0, 'delayed');
|
|
|
|
this.inboxQueue.once('cleaned', (jobs, status) => {
|
|
//inboxLogger.succ(`Cleaned ${jobs.length} ${status} jobs`);
|
|
});
|
|
this.inboxQueue.clean(0, 0, 'delayed');
|
|
}
|
|
}
|