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';
|
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';
|
2023-10-07 18:00:56 +09:00
|
|
|
import { QueryService } from '@/core/QueryService.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,
|
2023-10-07 18:00:56 +09:00
|
|
|
private queryService: QueryService,
|
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-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-07 17:57:15 +09:00
|
|
|
const [noteIdsRes, repliesNoteIdsRes, channelNoteIdsRes] = await Promise.all([
|
|
|
|
this.redisForTimelines.xrevrange(
|
|
|
|
ps.withFiles ? `userTimelineWithFiles:${ps.userId}` : `userTimeline:${ps.userId}`,
|
|
|
|
ps.untilId ? this.idService.parse(ps.untilId).date.getTime() : ps.untilDate ?? '+',
|
|
|
|
ps.sinceId ? this.idService.parse(ps.sinceId).date.getTime() : ps.sinceDate ?? '-',
|
|
|
|
'COUNT', limit,
|
|
|
|
).then(res => res.map(x => x[1][1]).filter(x => x !== ps.untilId && x !== ps.sinceId)),
|
|
|
|
ps.withReplies
|
|
|
|
? this.redisForTimelines.xrevrange(
|
|
|
|
`userTimelineWithReplies:${ps.userId}`,
|
2023-10-04 11:24:46 +09:00
|
|
|
ps.untilId ? this.idService.parse(ps.untilId).date.getTime() : ps.untilDate ?? '+',
|
2023-10-05 10:23:58 +09:00
|
|
|
ps.sinceId ? this.idService.parse(ps.sinceId).date.getTime() : ps.sinceDate ?? '-',
|
2023-10-07 17:57:15 +09:00
|
|
|
'COUNT', limit,
|
|
|
|
).then(res => res.map(x => x[1][1]).filter(x => x !== ps.untilId && x !== ps.sinceId))
|
|
|
|
: Promise.resolve([]),
|
|
|
|
ps.withChannelNotes
|
|
|
|
? this.redisForTimelines.xrevrange(
|
|
|
|
`userTimelineWithChannel:${ps.userId}`,
|
|
|
|
ps.untilId ? this.idService.parse(ps.untilId).date.getTime() : ps.untilDate ?? '+',
|
|
|
|
ps.sinceId ? this.idService.parse(ps.sinceId).date.getTime() : ps.sinceDate ?? '-',
|
|
|
|
'COUNT', limit,
|
|
|
|
).then(res => res.map(x => x[1][1]).filter(x => x !== ps.untilId && x !== ps.sinceId))
|
|
|
|
: Promise.resolve([]),
|
|
|
|
]);
|
2019-01-08 21:02:00 +09:00
|
|
|
|
2023-10-04 11:24:46 +09:00
|
|
|
let noteIds = Array.from(new Set([
|
2023-10-07 17:57:15 +09:00
|
|
|
...noteIdsRes,
|
|
|
|
...repliesNoteIdsRes,
|
|
|
|
...channelNoteIdsRes,
|
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
|
|
|
|
2023-10-07 20:27:35 +09:00
|
|
|
if (noteIds.length > 0) {
|
2023-10-09 08:54:57 +09:00
|
|
|
const isFollowing = me ? me.id === ps.userId || Object.hasOwn(await this.cacheService.userFollowingsCache.fetch(me.id), ps.userId) : false;
|
2022-09-18 03:27:08 +09:00
|
|
|
|
2023-10-07 20:27:35 +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-07 20:27:35 +09:00
|
|
|
let timeline = await query.getMany();
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2023-10-07 20:27:35 +09:00
|
|
|
timeline = timeline.filter(note => {
|
|
|
|
if (me && isUserRelated(note, userIdsWhoMeMuting, true)) return false;
|
2023-10-04 16:43:24 +09:00
|
|
|
|
2023-10-07 20:27:35 +09:00
|
|
|
if (note.renoteId) {
|
|
|
|
if (note.text == null && note.fileIds.length === 0 && !note.hasPoll) {
|
|
|
|
if (ps.withRenotes === false) return false;
|
|
|
|
}
|
2023-10-03 20:26:11 +09:00
|
|
|
}
|
2023-09-28 11:02:01 +09:00
|
|
|
|
2023-10-09 12:36:25 +09:00
|
|
|
if (note.visibility === 'specified' && (!me || (me.id !== note.userId && !note.visibleUserIds.some(v => v === me.id)))) return false;
|
2023-10-07 20:27:35 +09:00
|
|
|
if (note.visibility === 'followers' && !isFollowing) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
2018-12-26 23:11:51 +09:00
|
|
|
|
2023-10-07 20:27:35 +09:00
|
|
|
timeline.sort((a, b) => a.id > b.id ? -1 : 1);
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2023-10-09 16:54:27 +09:00
|
|
|
if (timeline.length > 0) {
|
|
|
|
return await this.noteEntityService.packMany(timeline, me);
|
2023-10-08 08:25:37 +09:00
|
|
|
}
|
2023-10-09 16:54:27 +09:00
|
|
|
}
|
2023-10-07 18:00:56 +09:00
|
|
|
|
2023-10-09 16:54:27 +09:00
|
|
|
// fallback to database
|
2023-10-07 18:00:56 +09:00
|
|
|
|
2023-10-09 16:54:27 +09:00
|
|
|
//#region Construct query
|
|
|
|
const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note'), ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate)
|
|
|
|
.andWhere('note.userId = :userId', { userId: ps.userId })
|
|
|
|
.innerJoinAndSelect('note.user', 'user')
|
|
|
|
.leftJoinAndSelect('note.reply', 'reply')
|
|
|
|
.leftJoinAndSelect('note.renote', 'renote')
|
|
|
|
.leftJoinAndSelect('note.channel', 'channel')
|
|
|
|
.leftJoinAndSelect('reply.user', 'replyUser')
|
|
|
|
.leftJoinAndSelect('renote.user', 'renoteUser');
|
2023-10-07 18:00:56 +09:00
|
|
|
|
2023-10-09 16:54:27 +09:00
|
|
|
if (!ps.withChannelNotes) {
|
|
|
|
query.andWhere('note.channelId IS NULL');
|
|
|
|
}
|
2023-10-07 18:00:56 +09:00
|
|
|
|
2023-10-09 16:54:27 +09:00
|
|
|
this.queryService.generateVisibilityQuery(query, me);
|
2023-10-07 18:00:56 +09:00
|
|
|
|
2023-10-09 16:54:27 +09:00
|
|
|
if (ps.withFiles) {
|
|
|
|
query.andWhere('note.fileIds != \'{}\'');
|
2023-10-07 20:27:35 +09:00
|
|
|
}
|
2023-10-09 16:54:27 +09:00
|
|
|
|
|
|
|
if (ps.includeMyRenotes === false) {
|
|
|
|
query.andWhere(new Brackets(qb => {
|
|
|
|
qb.orWhere('note.userId != :userId', { userId: ps.userId });
|
|
|
|
qb.orWhere('note.renoteId IS NULL');
|
|
|
|
qb.orWhere('note.text IS NOT NULL');
|
|
|
|
qb.orWhere('note.fileIds != \'{}\'');
|
|
|
|
qb.orWhere('0 < (SELECT COUNT(*) FROM poll WHERE poll."noteId" = note.id)');
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
|
|
|
const timeline = await query.limit(ps.limit).getMany();
|
|
|
|
|
|
|
|
return await this.noteEntityService.packMany(timeline, me);
|
2022-09-18 03:27:08 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|