2023-12-02 10:25:07 +01:00
|
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-12-02 10:25:07 +01:00
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
|
import { DI } from '@/di-symbols.js';
|
|
|
|
|
import { bindThis } from '@/decorators.js';
|
|
|
|
|
import type { MiUser } from '@/models/User.js';
|
|
|
|
|
import type { MiNote } from '@/models/Note.js';
|
|
|
|
|
import { Packed } from '@/misc/json-schema.js';
|
|
|
|
|
import type { NotesRepository } from '@/models/_.js';
|
|
|
|
|
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
2023-12-04 06:38:21 +01:00
|
|
|
|
import { FanoutTimelineName, FanoutTimelineService } from '@/core/FanoutTimelineService.js';
|
|
|
|
|
import { isUserRelated } from '@/misc/is-user-related.js';
|
2024-04-14 03:23:48 +02:00
|
|
|
|
import { isQuote, isRenote } from '@/misc/is-renote.js';
|
2023-12-04 06:38:21 +01:00
|
|
|
|
import { CacheService } from '@/core/CacheService.js';
|
|
|
|
|
import { isReply } from '@/misc/is-reply.js';
|
|
|
|
|
import { isInstanceMuted } from '@/misc/is-instance-muted.js';
|
|
|
|
|
|
|
|
|
|
type TimelineOptions = {
|
|
|
|
|
untilId: string | null,
|
|
|
|
|
sinceId: string | null,
|
|
|
|
|
limit: number,
|
|
|
|
|
allowPartial: boolean,
|
|
|
|
|
me?: { id: MiUser['id'] } | undefined | null,
|
|
|
|
|
useDbFallback: boolean,
|
|
|
|
|
redisTimelines: FanoutTimelineName[],
|
|
|
|
|
noteFilter?: (note: MiNote) => boolean,
|
|
|
|
|
alwaysIncludeMyNotes?: boolean;
|
2023-12-07 10:15:38 +01:00
|
|
|
|
ignoreAuthorFromBlock?: boolean;
|
2023-12-04 06:38:21 +01:00
|
|
|
|
ignoreAuthorFromMute?: boolean;
|
|
|
|
|
excludeNoFiles?: boolean;
|
|
|
|
|
excludeReplies?: boolean;
|
2023-12-23 02:09:23 +01:00
|
|
|
|
excludeBots?: boolean;
|
2023-12-04 06:38:21 +01:00
|
|
|
|
excludePureRenotes: boolean;
|
|
|
|
|
dbFallback: (untilId: string | null, sinceId: string | null, limit: number) => Promise<MiNote[]>,
|
|
|
|
|
};
|
2023-12-02 10:25:07 +01:00
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class FanoutTimelineEndpointService {
|
|
|
|
|
constructor(
|
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
|
|
|
|
|
private noteEntityService: NoteEntityService,
|
2023-12-04 06:38:21 +01:00
|
|
|
|
private cacheService: CacheService,
|
2023-12-02 10:25:07 +01:00
|
|
|
|
private fanoutTimelineService: FanoutTimelineService,
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@bindThis
|
2023-12-04 06:38:21 +01:00
|
|
|
|
async timeline(ps: TimelineOptions): Promise<Packed<'Note'>[]> {
|
2023-12-02 10:25:07 +01:00
|
|
|
|
return await this.noteEntityService.packMany(await this.getMiNotes(ps), ps.me);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@bindThis
|
2023-12-04 06:38:21 +01:00
|
|
|
|
private async getMiNotes(ps: TimelineOptions): Promise<MiNote[]> {
|
2023-12-02 10:25:07 +01:00
|
|
|
|
// 呼び出し元と以下の処理をシンプルにするためにdbFallbackを置き換える
|
|
|
|
|
if (!ps.useDbFallback) ps.dbFallback = () => Promise.resolve([]);
|
|
|
|
|
|
2024-05-20 01:28:28 +02:00
|
|
|
|
const ascending = ps.sinceId && !ps.untilId;
|
|
|
|
|
const idCompare: (a: string, b: string) => number = ascending ? (a, b) => a < b ? -1 : 1 : (a, b) => a > b ? -1 : 1;
|
2023-12-07 09:07:06 +01:00
|
|
|
|
|
2023-12-02 10:25:07 +01:00
|
|
|
|
const redisResult = await this.fanoutTimelineService.getMulti(ps.redisTimelines, ps.untilId, ps.sinceId);
|
|
|
|
|
|
2023-12-07 09:07:06 +01:00
|
|
|
|
// TODO: いい感じにgetMulti内でソート済だからuniqするときにredisResultが全てソート済なのを利用して再ソートを避けたい
|
2024-06-22 05:43:03 +02:00
|
|
|
|
const redisResultIds = Array.from(new Set(redisResult.flat(1))).sort(idCompare);
|
2023-12-02 10:25:07 +01:00
|
|
|
|
|
2024-06-22 05:43:03 +02:00
|
|
|
|
let noteIds = redisResultIds.slice(0, ps.limit);
|
|
|
|
|
const oldestNoteId = ascending ? redisResultIds[0] : redisResultIds[redisResultIds.length - 1];
|
|
|
|
|
const shouldFallbackToDb = noteIds.length === 0 || ps.sinceId != null && ps.sinceId < oldestNoteId;
|
2023-12-02 10:25:07 +01:00
|
|
|
|
|
|
|
|
|
if (!shouldFallbackToDb) {
|
2023-12-04 06:38:21 +01:00
|
|
|
|
let filter = ps.noteFilter ?? (_note => true);
|
|
|
|
|
|
|
|
|
|
if (ps.alwaysIncludeMyNotes && ps.me) {
|
|
|
|
|
const me = ps.me;
|
|
|
|
|
const parentFilter = filter;
|
|
|
|
|
filter = (note) => note.userId === me.id || parentFilter(note);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ps.excludeNoFiles) {
|
|
|
|
|
const parentFilter = filter;
|
|
|
|
|
filter = (note) => note.fileIds.length !== 0 && parentFilter(note);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ps.excludeReplies) {
|
|
|
|
|
const parentFilter = filter;
|
|
|
|
|
filter = (note) => !isReply(note, ps.me?.id) && parentFilter(note);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-23 02:09:23 +01:00
|
|
|
|
if (ps.excludeBots) {
|
|
|
|
|
const parentFilter = filter;
|
|
|
|
|
filter = (note) => !note.user?.isBot && parentFilter(note);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-04 06:38:21 +01:00
|
|
|
|
if (ps.excludePureRenotes) {
|
|
|
|
|
const parentFilter = filter;
|
2024-04-14 03:23:48 +02:00
|
|
|
|
filter = (note) => (!isRenote(note) || isQuote(note)) && parentFilter(note);
|
2023-12-04 06:38:21 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ps.me) {
|
|
|
|
|
const me = ps.me;
|
|
|
|
|
const [
|
|
|
|
|
userIdsWhoMeMuting,
|
|
|
|
|
userIdsWhoMeMutingRenotes,
|
|
|
|
|
userIdsWhoBlockingMe,
|
|
|
|
|
userMutedInstances,
|
|
|
|
|
] = await Promise.all([
|
|
|
|
|
this.cacheService.userMutingsCache.fetch(ps.me.id),
|
|
|
|
|
this.cacheService.renoteMutingsCache.fetch(ps.me.id),
|
|
|
|
|
this.cacheService.userBlockedCache.fetch(ps.me.id),
|
|
|
|
|
this.cacheService.userProfileCache.fetch(me.id).then(p => new Set(p.mutedInstances)),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const parentFilter = filter;
|
|
|
|
|
filter = (note) => {
|
2023-12-07 10:15:38 +01:00
|
|
|
|
if (isUserRelated(note, userIdsWhoBlockingMe, ps.ignoreAuthorFromBlock)) return false;
|
2023-12-04 06:38:21 +01:00
|
|
|
|
if (isUserRelated(note, userIdsWhoMeMuting, ps.ignoreAuthorFromMute)) return false;
|
2024-04-17 07:23:41 +02:00
|
|
|
|
if (!ps.ignoreAuthorFromMute && isRenote(note) && !isQuote(note) && userIdsWhoMeMutingRenotes.has(note.userId)) return false;
|
2023-12-04 06:38:21 +01:00
|
|
|
|
if (isInstanceMuted(note, userMutedInstances)) return false;
|
|
|
|
|
|
|
|
|
|
return parentFilter(note);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 10:25:07 +01:00
|
|
|
|
const redisTimeline: MiNote[] = [];
|
|
|
|
|
let readFromRedis = 0;
|
|
|
|
|
let lastSuccessfulRate = 1; // rateをキャッシュする?
|
|
|
|
|
|
|
|
|
|
while ((redisResultIds.length - readFromRedis) !== 0) {
|
|
|
|
|
const remainingToRead = ps.limit - redisTimeline.length;
|
|
|
|
|
|
|
|
|
|
// DBからの取り直しを減らす初回と同じ割合以上で成功すると仮定するが、クエリの長さを考えて三倍まで
|
2023-12-07 09:07:06 +01:00
|
|
|
|
const countToGet = Math.ceil(remainingToRead * Math.min(1.1 / lastSuccessfulRate, 3));
|
2023-12-02 10:25:07 +01:00
|
|
|
|
noteIds = redisResultIds.slice(readFromRedis, readFromRedis + countToGet);
|
|
|
|
|
|
|
|
|
|
readFromRedis += noteIds.length;
|
|
|
|
|
|
2023-12-07 09:07:06 +01:00
|
|
|
|
const gotFromDb = await this.getAndFilterFromDb(noteIds, filter, idCompare);
|
2023-12-02 10:25:07 +01:00
|
|
|
|
redisTimeline.push(...gotFromDb);
|
|
|
|
|
lastSuccessfulRate = gotFromDb.length / noteIds.length;
|
|
|
|
|
|
|
|
|
|
if (ps.allowPartial ? redisTimeline.length !== 0 : redisTimeline.length >= ps.limit) {
|
|
|
|
|
// 十分Redisからとれた
|
2024-05-20 01:28:28 +02:00
|
|
|
|
return redisTimeline.slice(0, ps.limit);
|
2023-12-02 10:25:07 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// まだ足りない分はDBにフォールバック
|
|
|
|
|
const remainingToRead = ps.limit - redisTimeline.length;
|
2023-12-07 09:07:06 +01:00
|
|
|
|
let dbUntil: string | null;
|
|
|
|
|
let dbSince: string | null;
|
2024-05-20 01:28:28 +02:00
|
|
|
|
if (ascending) {
|
2023-12-07 09:07:06 +01:00
|
|
|
|
dbUntil = ps.untilId;
|
|
|
|
|
dbSince = noteIds[noteIds.length - 1];
|
|
|
|
|
} else {
|
|
|
|
|
dbUntil = noteIds[noteIds.length - 1];
|
|
|
|
|
dbSince = ps.sinceId;
|
|
|
|
|
}
|
|
|
|
|
const gotFromDb = await ps.dbFallback(dbUntil, dbSince, remainingToRead);
|
2024-05-20 01:28:28 +02:00
|
|
|
|
return [...redisTimeline, ...gotFromDb];
|
2023-12-02 10:25:07 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await ps.dbFallback(ps.untilId, ps.sinceId, ps.limit);
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-07 09:07:06 +01:00
|
|
|
|
private async getAndFilterFromDb(noteIds: string[], noteFilter: (note: MiNote) => boolean, idCompare: (a: string, b: string) => number): Promise<MiNote[]> {
|
2023-12-02 10:25:07 +01:00
|
|
|
|
const query = this.notesRepository.createQueryBuilder('note')
|
|
|
|
|
.where('note.id IN (:...noteIds)', { noteIds: noteIds })
|
|
|
|
|
.innerJoinAndSelect('note.user', 'user')
|
|
|
|
|
.leftJoinAndSelect('note.reply', 'reply')
|
|
|
|
|
.leftJoinAndSelect('note.renote', 'renote')
|
|
|
|
|
.leftJoinAndSelect('reply.user', 'replyUser')
|
|
|
|
|
.leftJoinAndSelect('renote.user', 'renoteUser')
|
|
|
|
|
.leftJoinAndSelect('note.channel', 'channel');
|
|
|
|
|
|
|
|
|
|
const notes = (await query.getMany()).filter(noteFilter);
|
|
|
|
|
|
2023-12-07 09:07:06 +01:00
|
|
|
|
notes.sort((a, b) => idCompare(a.id, b.id));
|
2023-12-02 10:25:07 +01:00
|
|
|
|
|
|
|
|
|
return notes;
|
|
|
|
|
}
|
|
|
|
|
}
|