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 { MiNote, NotesRepository } from '@/models/_.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-24 07:15:16 +09:00
|
|
|
import { GetterService } from '@/server/api/GetterService.js';
|
2023-10-03 20:26:11 +09:00
|
|
|
import { CacheService } from '@/core/CacheService.js';
|
|
|
|
import { IdService } from '@/core/IdService.js';
|
2023-10-04 16:43:24 +09:00
|
|
|
import { isUserRelated } from '@/misc/is-user-related.js';
|
2022-02-27 11:07:39 +09:00
|
|
|
import { ApiError } from '../../error.js';
|
2018-09-05 23:55:51 +09:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 11:20:58 +09:00
|
|
|
tags: ['users', 'notes'],
|
|
|
|
|
|
|
|
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: {
|
|
|
|
noSuchUser: {
|
|
|
|
message: 'No such user.',
|
|
|
|
code: 'NO_SUCH_USER',
|
2021-12-09 23:58:30 +09:00
|
|
|
id: '27e494ba-2ac2-48e8-893b-10d4d8c2387b',
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 22:27:10 +09:00
|
|
|
} as const;
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2022-02-20 13:15:40 +09:00
|
|
|
export const paramDef = {
|
2022-02-19 14:05:32 +09:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
userId: { type: 'string', format: 'misskey:id' },
|
2023-09-28 11:41:41 +09:00
|
|
|
withReplies: { type: 'boolean', default: false },
|
|
|
|
withRenotes: { type: 'boolean', default: true },
|
2023-10-05 09:48:45 +09:00
|
|
|
withChannelNotes: { type: 'boolean', default: false },
|
2022-02-19 14:05:32 +09:00
|
|
|
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 },
|
|
|
|
withFiles: { type: 'boolean', default: false },
|
|
|
|
excludeNsfw: { type: 'boolean', default: false },
|
|
|
|
},
|
|
|
|
required: ['userId'],
|
|
|
|
} 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,
|
|
|
|
private getterService: GetterService,
|
2023-10-03 20:26:11 +09:00
|
|
|
private cacheService: CacheService,
|
|
|
|
private idService: IdService,
|
2022-09-18 03:27:08 +09:00
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
2023-10-04 16:43:24 +09:00
|
|
|
const [
|
|
|
|
userIdsWhoMeMuting,
|
|
|
|
] = me ? await Promise.all([
|
|
|
|
this.cacheService.userMutingsCache.fetch(me.id),
|
|
|
|
]) : [new Set<string>()];
|
|
|
|
|
2023-10-03 20:26:11 +09:00
|
|
|
let timeline: MiNote[] = [];
|
2022-09-18 03:27:08 +09:00
|
|
|
|
2023-10-03 20:26:11 +09:00
|
|
|
const limit = ps.limit + (ps.untilId ? 1 : 0); // untilIdに指定したものも含まれるため+1
|
|
|
|
let noteIdsRes: [string, string[]][] = [];
|
2023-10-04 11:24:46 +09:00
|
|
|
let repliesNoteIdsRes: [string, string[]][] = [];
|
2023-10-05 09:48:45 +09:00
|
|
|
let channelNoteIdsRes: [string, string[]][] = [];
|
2022-09-18 03:27:08 +09:00
|
|
|
|
2023-10-03 20:26:11 +09:00
|
|
|
if (!ps.sinceId && !ps.sinceDate) {
|
2023-10-05 09:48:45 +09:00
|
|
|
[noteIdsRes, repliesNoteIdsRes, channelNoteIdsRes] = await Promise.all([
|
2023-10-04 11:24:46 +09:00
|
|
|
this.redisForTimelines.xrevrange(
|
|
|
|
ps.withFiles ? `userTimelineWithFiles:${ps.userId}` : `userTimeline:${ps.userId}`,
|
|
|
|
ps.untilId ? this.idService.parse(ps.untilId).date.getTime() : ps.untilDate ?? '+',
|
|
|
|
'-',
|
|
|
|
'COUNT', limit),
|
|
|
|
ps.withReplies
|
|
|
|
? this.redisForTimelines.xrevrange(
|
|
|
|
`userTimelineWithReplies:${ps.userId}`,
|
|
|
|
ps.untilId ? this.idService.parse(ps.untilId).date.getTime() : ps.untilDate ?? '+',
|
|
|
|
'-',
|
|
|
|
'COUNT', limit)
|
|
|
|
: Promise.resolve([]),
|
2023-10-05 09:48:45 +09:00
|
|
|
ps.withChannelNotes
|
|
|
|
? this.redisForTimelines.xrevrange(
|
|
|
|
`userTimelineWithChannel:${ps.userId}`,
|
|
|
|
ps.untilId ? this.idService.parse(ps.untilId).date.getTime() : ps.untilDate ?? '+',
|
|
|
|
'-',
|
|
|
|
'COUNT', limit)
|
|
|
|
: Promise.resolve([]),
|
2023-10-04 11:24:46 +09:00
|
|
|
]);
|
2022-09-18 03:27:08 +09:00
|
|
|
}
|
2019-01-08 21:02:00 +09:00
|
|
|
|
2023-10-04 11:24:46 +09:00
|
|
|
let noteIds = Array.from(new Set([
|
|
|
|
...noteIdsRes.map(x => x[1][1]).filter(x => x !== ps.untilId),
|
|
|
|
...repliesNoteIdsRes.map(x => x[1][1]).filter(x => x !== ps.untilId),
|
2023-10-05 09:48:45 +09:00
|
|
|
...channelNoteIdsRes.map(x => x[1][1]).filter(x => x !== ps.untilId),
|
2023-10-04 11:24:46 +09:00
|
|
|
]));
|
|
|
|
noteIds.sort((a, b) => a > b ? -1 : 1);
|
|
|
|
noteIds = noteIds.slice(0, ps.limit);
|
2023-10-03 20:26:11 +09:00
|
|
|
|
|
|
|
if (noteIds.length === 0) {
|
|
|
|
return [];
|
2019-04-07 21:50:36 +09:00
|
|
|
}
|
2017-11-14 08:21:19 +09:00
|
|
|
|
2023-10-03 20:26:11 +09:00
|
|
|
const isFollowing = me ? Object.hasOwn(await this.cacheService.userFollowingsCache.fetch(me.id), ps.userId) : false;
|
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');
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2023-10-03 20:26:11 +09:00
|
|
|
timeline = await query.getMany();
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2023-10-03 20:26:11 +09:00
|
|
|
timeline = timeline.filter(note => {
|
2023-10-04 16:43:24 +09:00
|
|
|
if (me && isUserRelated(note, userIdsWhoMeMuting, true)) return false;
|
|
|
|
|
2023-10-03 20:26:11 +09:00
|
|
|
if (note.renoteId) {
|
|
|
|
if (note.text == null && note.fileIds.length === 0 && !note.hasPoll) {
|
|
|
|
if (ps.withRenotes === false) return false;
|
|
|
|
}
|
|
|
|
}
|
2023-09-28 11:02:01 +09:00
|
|
|
|
2023-10-03 20:26:11 +09:00
|
|
|
if (note.visibility === 'followers' && !isFollowing) return false;
|
2018-12-26 23:11:51 +09:00
|
|
|
|
2023-10-03 20:26:11 +09:00
|
|
|
return true;
|
|
|
|
});
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2023-10-03 20:26:11 +09:00
|
|
|
timeline.sort((a, b) => a.id > b.id ? -1 : 1);
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
return await this.noteEntityService.packMany(timeline, me);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|