2023-07-27 14:31:52 +09:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-06-14 18:01:23 +09:00
|
|
|
import { Brackets } from 'typeorm';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-10-03 20:26:11 +09:00
|
|
|
import * as Redis from 'ioredis';
|
|
|
|
import type { NotesRepository, FollowingsRepository, MiNote } from '@/models/_.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import ActiveUsersChart from '@/core/chart/charts/active-users.js';
|
|
|
|
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-01-12 21:02:26 +09:00
|
|
|
import { RoleService } from '@/core/RoleService.js';
|
2023-03-19 16:52:38 +09:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
2023-10-03 20:26:11 +09:00
|
|
|
import { isUserRelated } from '@/misc/is-user-related.js';
|
|
|
|
import { CacheService } from '@/core/CacheService.js';
|
2022-02-27 11:07:39 +09:00
|
|
|
import { ApiError } from '../../error.js';
|
2018-07-11 14:03:21 +09:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 11:20:58 +09:00
|
|
|
tags: ['notes'],
|
|
|
|
|
2022-01-18 22:27:10 +09:00
|
|
|
requireCredential: true,
|
2019-04-18 14:58:43 +09:00
|
|
|
|
2019-02-23 11:20:58 +09:00
|
|
|
res: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2019-02-23 11:20:58 +09:00
|
|
|
items: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 22:35:26 +09:00
|
|
|
ref: 'Note',
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2019-02-23 11:20:58 +09:00
|
|
|
},
|
|
|
|
|
2019-02-22 11:46:58 +09:00
|
|
|
errors: {
|
|
|
|
stlDisabled: {
|
2019-04-08 23:05:41 +09:00
|
|
|
message: 'Hybrid timeline has been disabled.',
|
2019-02-22 11:46:58 +09:00
|
|
|
code: 'STL_DISABLED',
|
2021-12-09 23:58:30 +09:00
|
|
|
id: '620763f4-f621-4533-ab33-0577a1a3c342',
|
2019-02-22 11:46:58 +09:00
|
|
|
},
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2022-01-18 22:27:10 +09:00
|
|
|
} as const;
|
2019-01-16 02:30:55 +09:00
|
|
|
|
2022-02-20 13:15:40 +09:00
|
|
|
export const paramDef = {
|
2022-02-19 14:05:32 +09:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
|
|
|
|
sinceId: { type: 'string', format: 'misskey:id' },
|
|
|
|
untilId: { type: 'string', format: 'misskey:id' },
|
|
|
|
sinceDate: { type: 'integer' },
|
|
|
|
untilDate: { type: 'integer' },
|
|
|
|
includeMyRenotes: { type: 'boolean', default: true },
|
|
|
|
includeRenotedMyNotes: { type: 'boolean', default: true },
|
|
|
|
includeLocalRenotes: { type: 'boolean', default: true },
|
2023-05-16 12:16:37 +09:00
|
|
|
withFiles: { type: 'boolean', default: false },
|
2023-09-28 15:32:47 +09:00
|
|
|
withRenotes: { type: 'boolean', default: true },
|
2022-02-19 14:05:32 +09:00
|
|
|
},
|
|
|
|
required: [],
|
|
|
|
} as const;
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
@Injectable()
|
2023-08-17 21:20:58 +09:00
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
2022-09-18 03:27:08 +09:00
|
|
|
constructor(
|
2023-10-03 20:26:11 +09:00
|
|
|
@Inject(DI.redisForTimelines)
|
|
|
|
private redisForTimelines: Redis.Redis,
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
|
|
|
private noteEntityService: NoteEntityService,
|
2023-01-12 21:02:26 +09:00
|
|
|
private roleService: RoleService,
|
2022-09-18 03:27:08 +09:00
|
|
|
private activeUsersChart: ActiveUsersChart,
|
2023-03-19 16:52:38 +09:00
|
|
|
private idService: IdService,
|
2023-10-03 20:26:11 +09:00
|
|
|
private cacheService: CacheService,
|
2022-09-18 03:27:08 +09:00
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
2023-01-15 20:52:53 +09:00
|
|
|
const policies = await this.roleService.getUserPolicies(me.id);
|
|
|
|
if (!policies.ltlAvailable) {
|
2022-09-18 03:27:08 +09:00
|
|
|
throw new ApiError(meta.errors.stlDisabled);
|
|
|
|
}
|
|
|
|
|
2023-10-03 20:26:11 +09:00
|
|
|
const [
|
|
|
|
userIdsWhoMeMuting,
|
|
|
|
userIdsWhoMeMutingRenotes,
|
|
|
|
userIdsWhoBlockingMe,
|
|
|
|
] = await Promise.all([
|
|
|
|
this.cacheService.userMutingsCache.fetch(me.id),
|
|
|
|
this.cacheService.renoteMutingsCache.fetch(me.id),
|
|
|
|
this.cacheService.userBlockedCache.fetch(me.id),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let timeline: MiNote[] = [];
|
|
|
|
|
2023-10-05 10:23:58 +09:00
|
|
|
const limit = ps.limit + (ps.untilId ? 1 : 0) + (ps.sinceId ? 1 : 0); // untilIdに指定したものも含まれるため+1
|
2022-09-18 03:27:08 +09:00
|
|
|
|
2023-10-08 15:22:43 +09:00
|
|
|
const redisPipeline = this.redisForTimelines.pipeline();
|
|
|
|
redisPipeline.xrevrange(
|
|
|
|
ps.withFiles ? `homeTimelineWithFiles:${me.id}` : `homeTimeline:${me.id}`,
|
|
|
|
ps.untilId ? this.idService.parse(ps.untilId).date.getTime() : ps.untilDate ?? '+',
|
|
|
|
ps.sinceId ? this.idService.parse(ps.sinceId).date.getTime() : ps.sinceDate ?? '-',
|
|
|
|
'COUNT', limit,
|
|
|
|
);
|
|
|
|
redisPipeline.xrevrange(
|
|
|
|
ps.withFiles ? 'localTimelineWithFiles' : 'localTimeline',
|
|
|
|
ps.untilId ? this.idService.parse(ps.untilId).date.getTime() : ps.untilDate ?? '+',
|
|
|
|
ps.sinceId ? this.idService.parse(ps.sinceId).date.getTime() : ps.sinceDate ?? '-',
|
|
|
|
'COUNT', limit,
|
|
|
|
);
|
|
|
|
const [htlNoteIds, ltlNoteIds] = await redisPipeline.exec().then(res => res ? [
|
|
|
|
(res[0][1] as string[][]).map(x => x[1][1]).filter(x => x !== ps.untilId && x !== ps.sinceId),
|
|
|
|
(res[1][1] as string[][]).map(x => x[1][1]).filter(x => x !== ps.untilId && x !== ps.sinceId),
|
|
|
|
] : []);
|
2023-10-07 17:48:10 +09:00
|
|
|
|
2023-10-03 20:26:11 +09:00
|
|
|
let noteIds = Array.from(new Set([...htlNoteIds, ...ltlNoteIds]));
|
|
|
|
noteIds.sort((a, b) => a > b ? -1 : 1);
|
|
|
|
noteIds = noteIds.slice(0, ps.limit);
|
2022-09-18 03:27:08 +09:00
|
|
|
|
2023-10-03 20:26:11 +09:00
|
|
|
if (noteIds.length === 0) {
|
|
|
|
return [];
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
|
|
|
|
2023-10-03 20:26:11 +09: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');
|
|
|
|
|
|
|
|
timeline = await query.getMany();
|
|
|
|
|
|
|
|
timeline = timeline.filter(note => {
|
|
|
|
if (note.userId === me.id) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (isUserRelated(note, userIdsWhoBlockingMe)) return false;
|
|
|
|
if (isUserRelated(note, userIdsWhoMeMuting)) return false;
|
|
|
|
if (note.renoteId) {
|
|
|
|
if (note.text == null && note.fileIds.length === 0 && !note.hasPoll) {
|
|
|
|
if (isUserRelated(note, userIdsWhoMeMutingRenotes)) return false;
|
|
|
|
if (ps.withRenotes === false) return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
2023-09-28 15:32:47 +09:00
|
|
|
|
2023-10-03 20:26:11 +09:00
|
|
|
// TODO: フィルタした結果件数が足りなかった場合の対応
|
2022-09-18 03:27:08 +09:00
|
|
|
|
2023-10-03 20:26:11 +09:00
|
|
|
timeline.sort((a, b) => a.id > b.id ? -1 : 1);
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
process.nextTick(() => {
|
|
|
|
this.activeUsersChart.read(me);
|
|
|
|
});
|
|
|
|
|
|
|
|
return await this.noteEntityService.packMany(timeline, me);
|
|
|
|
});
|
2018-07-11 14:03:21 +09:00
|
|
|
}
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|