chore(endpoints/local-timeline): FanoutTimelineEndpointServiceで再実装
This commit is contained in:
parent
5fe66255df
commit
f0e41d7c24
|
|
@ -31,7 +31,7 @@ export class FanoutTimelineEndpointService {
|
||||||
untilId: string | null,
|
untilId: string | null,
|
||||||
sinceId: string | null,
|
sinceId: string | null,
|
||||||
limit: number,
|
limit: number,
|
||||||
me?: { id: MiUser['id'] },
|
me?: { id: MiUser['id'] } | undefined | null,
|
||||||
redisTimelines: (string | { name: string, fallbackIfEmpty: boolean })[],
|
redisTimelines: (string | { name: string, fallbackIfEmpty: boolean })[],
|
||||||
noteFilter: (note: MiNote) => boolean,
|
noteFilter: (note: MiNote) => boolean,
|
||||||
dbFallback: (untilId: string | null, sinceId: string | null, limit: number) => Promise<MiNote[]>,
|
dbFallback: (untilId: string | null, sinceId: string | null, limit: number) => Promise<MiNote[]>,
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ import { QueryService } from '@/core/QueryService.js';
|
||||||
import { MetaService } from '@/core/MetaService.js';
|
import { MetaService } from '@/core/MetaService.js';
|
||||||
import { MiLocalUser } from '@/models/User.js';
|
import { MiLocalUser } from '@/models/User.js';
|
||||||
import { ApiError } from '../../error.js';
|
import { ApiError } from '../../error.js';
|
||||||
|
import {FanoutTimelineEndpointService} from "@/core/FanoutTimelineEndpointService.js";
|
||||||
|
|
||||||
export const meta = {
|
export const meta = {
|
||||||
tags: ['notes'],
|
tags: ['notes'],
|
||||||
|
|
@ -69,7 +70,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
|
||||||
private activeUsersChart: ActiveUsersChart,
|
private activeUsersChart: ActiveUsersChart,
|
||||||
private idService: IdService,
|
private idService: IdService,
|
||||||
private cacheService: CacheService,
|
private cacheService: CacheService,
|
||||||
private fanoutTimelineService: FanoutTimelineService,
|
private fanoutTimelineEndpointService: FanoutTimelineEndpointService,
|
||||||
private queryService: QueryService,
|
private queryService: QueryService,
|
||||||
private metaService: MetaService,
|
private metaService: MetaService,
|
||||||
) {
|
) {
|
||||||
|
|
@ -85,13 +86,21 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
|
||||||
const serverSettings = await this.metaService.fetch();
|
const serverSettings = await this.metaService.fetch();
|
||||||
|
|
||||||
if (!serverSettings.enableFanoutTimeline) {
|
if (!serverSettings.enableFanoutTimeline) {
|
||||||
return await this.getFromDb({
|
const timeline = await this.getFromDb({
|
||||||
untilId,
|
untilId,
|
||||||
sinceId,
|
sinceId,
|
||||||
limit: ps.limit,
|
limit: ps.limit,
|
||||||
withFiles: ps.withFiles,
|
withFiles: ps.withFiles,
|
||||||
withReplies: ps.withReplies,
|
withReplies: ps.withReplies,
|
||||||
}, me);
|
}, me);
|
||||||
|
|
||||||
|
process.nextTick(() => {
|
||||||
|
if (me) {
|
||||||
|
this.activeUsersChart.read(me);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return await this.noteEntityService.packMany(timeline, me);
|
||||||
}
|
}
|
||||||
|
|
||||||
const [
|
const [
|
||||||
|
|
@ -104,36 +113,13 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
|
||||||
this.cacheService.userBlockedCache.fetch(me.id),
|
this.cacheService.userBlockedCache.fetch(me.id),
|
||||||
]) : [new Set<string>(), new Set<string>(), new Set<string>()];
|
]) : [new Set<string>(), new Set<string>(), new Set<string>()];
|
||||||
|
|
||||||
let noteIds: string[];
|
const timeline = await this.fanoutTimelineEndpointService.timeline({
|
||||||
|
untilId,
|
||||||
if (ps.withFiles) {
|
sinceId,
|
||||||
noteIds = await this.fanoutTimelineService.get('localTimelineWithFiles', untilId, sinceId);
|
limit: ps.limit,
|
||||||
} else {
|
me,
|
||||||
const [nonReplyNoteIds, replyNoteIds] = await this.fanoutTimelineService.getMulti([
|
redisTimelines: ps.withFiles ? ['localTimelineWithFiles'] : ['localTimeline', 'localTimelineWithReplies'],
|
||||||
'localTimeline',
|
noteFilter: note => {
|
||||||
'localTimelineWithReplies',
|
|
||||||
], untilId, sinceId);
|
|
||||||
noteIds = Array.from(new Set([...nonReplyNoteIds, ...replyNoteIds]));
|
|
||||||
noteIds.sort((a, b) => a > b ? -1 : 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
noteIds = noteIds.slice(0, ps.limit);
|
|
||||||
|
|
||||||
let redisTimeline: MiNote[] = [];
|
|
||||||
|
|
||||||
if (noteIds.length > 0) {
|
|
||||||
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');
|
|
||||||
|
|
||||||
redisTimeline = await query.getMany();
|
|
||||||
|
|
||||||
redisTimeline = redisTimeline.filter(note => {
|
|
||||||
if (me && (note.userId === me.id)) {
|
if (me && (note.userId === me.id)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -148,32 +134,23 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
});
|
},
|
||||||
|
dbFallback: async (untilId, sinceId, limit) => await this.getFromDb({
|
||||||
|
untilId,
|
||||||
|
sinceId,
|
||||||
|
limit,
|
||||||
|
withFiles: ps.withFiles,
|
||||||
|
withReplies: ps.withReplies,
|
||||||
|
}, me),
|
||||||
|
});
|
||||||
|
|
||||||
redisTimeline.sort((a, b) => a.id > b.id ? -1 : 1);
|
process.nextTick(() => {
|
||||||
}
|
if (me) {
|
||||||
|
this.activeUsersChart.read(me);
|
||||||
if (redisTimeline.length > 0) {
|
|
||||||
process.nextTick(() => {
|
|
||||||
if (me) {
|
|
||||||
this.activeUsersChart.read(me);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return await this.noteEntityService.packMany(redisTimeline, me);
|
|
||||||
} else {
|
|
||||||
if (serverSettings.enableFanoutTimelineDbFallback) { // fallback to db
|
|
||||||
return await this.getFromDb({
|
|
||||||
untilId,
|
|
||||||
sinceId,
|
|
||||||
limit: ps.limit,
|
|
||||||
withFiles: ps.withFiles,
|
|
||||||
withReplies: ps.withReplies,
|
|
||||||
}, me);
|
|
||||||
} else {
|
|
||||||
return [];
|
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
|
return timeline;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -214,14 +191,6 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
const timeline = await query.limit(ps.limit).getMany();
|
return await query.limit(ps.limit).getMany();
|
||||||
|
|
||||||
process.nextTick(() => {
|
|
||||||
if (me) {
|
|
||||||
this.activeUsersChart.read(me);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return await this.noteEntityService.packMany(timeline, me);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue