2023-07-27 07:31:52 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-06-14 11:01:23 +02:00
|
|
|
import { Brackets } from 'typeorm';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-10-29 06:16:36 +01:00
|
|
|
import type { MiNote, NotesRepository, ChannelFollowingsRepository } from '@/models/_.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { QueryService } from '@/core/QueryService.js';
|
|
|
|
import ActiveUsersChart from '@/core/chart/charts/active-users.js';
|
|
|
|
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-03-19 08:52:38 +01:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
2023-10-03 13:26:11 +02:00
|
|
|
import { CacheService } from '@/core/CacheService.js';
|
|
|
|
import { isUserRelated } from '@/misc/is-user-related.js';
|
2023-10-18 05:07:18 +02:00
|
|
|
import { FunoutTimelineService } from '@/core/FunoutTimelineService.js';
|
2023-10-18 05:26:16 +02:00
|
|
|
import { UserFollowingService } from '@/core/UserFollowingService.js';
|
2023-10-23 08:17:25 +02:00
|
|
|
import { MiLocalUser } from '@/models/User.js';
|
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-07-06 13:40:44 +02:00
|
|
|
export const meta = {
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['notes'],
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: true,
|
2018-07-15 23:58:45 +02:00
|
|
|
|
2019-02-23 03:20:58 +01:00
|
|
|
res: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2019-02-23 03:20:58 +01:00
|
|
|
items: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 15:35:26 +02:00
|
|
|
ref: 'Note',
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2019-02-23 03:20:58 +01:00
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2018-07-06 13:40:44 +02:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2022-02-19 06:05:32 +01: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 05:16:37 +02:00
|
|
|
withFiles: { type: 'boolean', default: false },
|
2023-09-28 08:32:47 +02:00
|
|
|
withRenotes: { type: 'boolean', default: true },
|
2022-02-19 06:05:32 +01:00
|
|
|
},
|
|
|
|
required: [],
|
|
|
|
} as const;
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
@Injectable()
|
2023-08-17 14:20:58 +02:00
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
2022-09-17 20:27:08 +02:00
|
|
|
constructor(
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
2018-04-21 04:42:38 +02:00
|
|
|
|
2023-10-29 06:16:36 +01:00
|
|
|
@Inject(DI.channelFollowingsRepository)
|
|
|
|
private channelFollowingsRepository: ChannelFollowingsRepository,
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
private noteEntityService: NoteEntityService,
|
|
|
|
private activeUsersChart: ActiveUsersChart,
|
2023-03-19 08:52:38 +01:00
|
|
|
private idService: IdService,
|
2023-10-03 13:26:11 +02:00
|
|
|
private cacheService: CacheService,
|
2023-10-18 05:07:18 +02:00
|
|
|
private funoutTimelineService: FunoutTimelineService,
|
2023-10-18 05:26:16 +02:00
|
|
|
private userFollowingService: UserFollowingService,
|
|
|
|
private queryService: QueryService,
|
2023-10-23 08:17:25 +02:00
|
|
|
private metaService: MetaService,
|
2022-09-17 20:27:08 +02:00
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
2023-10-16 03:45:22 +02:00
|
|
|
const untilId = ps.untilId ?? (ps.untilDate ? this.idService.gen(ps.untilDate!) : null);
|
|
|
|
const sinceId = ps.sinceId ?? (ps.sinceDate ? this.idService.gen(ps.sinceDate!) : null);
|
2023-10-09 13:31:39 +02:00
|
|
|
|
2023-10-23 08:17:25 +02:00
|
|
|
const serverSettings = await this.metaService.fetch();
|
|
|
|
|
|
|
|
if (serverSettings.enableFanoutTimeline) {
|
|
|
|
const [
|
|
|
|
followings,
|
|
|
|
userIdsWhoMeMuting,
|
|
|
|
userIdsWhoMeMutingRenotes,
|
|
|
|
userIdsWhoBlockingMe,
|
|
|
|
] = await Promise.all([
|
|
|
|
this.cacheService.userFollowingsCache.fetch(me.id),
|
|
|
|
this.cacheService.userMutingsCache.fetch(me.id),
|
|
|
|
this.cacheService.renoteMutingsCache.fetch(me.id),
|
|
|
|
this.cacheService.userBlockedCache.fetch(me.id),
|
|
|
|
]);
|
|
|
|
|
|
|
|
let noteIds = await this.funoutTimelineService.get(ps.withFiles ? `homeTimelineWithFiles:${me.id}` : `homeTimeline:${me.id}`, untilId, sinceId);
|
|
|
|
noteIds = noteIds.slice(0, ps.limit);
|
|
|
|
|
2023-10-24 07:34:32 +02:00
|
|
|
let redisTimeline: MiNote[] = [];
|
|
|
|
|
2023-10-23 08:17:25 +02:00
|
|
|
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');
|
|
|
|
|
2023-10-24 07:34:32 +02:00
|
|
|
redisTimeline = await query.getMany();
|
2023-10-23 08:17:25 +02:00
|
|
|
|
2023-10-24 07:34:32 +02:00
|
|
|
redisTimeline = redisTimeline.filter(note => {
|
2023-10-23 08:17:25 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (note.reply && note.reply.visibility === 'followers') {
|
|
|
|
if (!Object.hasOwn(followings, note.reply.userId)) return false;
|
2023-10-18 05:26:16 +02:00
|
|
|
}
|
|
|
|
|
2023-10-23 08:17:25 +02:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2023-10-24 07:34:32 +02:00
|
|
|
redisTimeline.sort((a, b) => a.id > b.id ? -1 : 1);
|
|
|
|
}
|
2023-10-23 08:17:25 +02:00
|
|
|
|
2023-10-24 07:34:32 +02:00
|
|
|
if (redisTimeline.length > 0) {
|
2023-10-23 08:17:25 +02:00
|
|
|
process.nextTick(() => {
|
|
|
|
this.activeUsersChart.read(me);
|
|
|
|
});
|
|
|
|
|
2023-10-24 07:34:32 +02:00
|
|
|
return await this.noteEntityService.packMany(redisTimeline, me);
|
2023-10-23 08:17:25 +02:00
|
|
|
} else { // fallback to db
|
|
|
|
return await this.getFromDb({
|
|
|
|
untilId,
|
|
|
|
sinceId,
|
|
|
|
limit: ps.limit,
|
|
|
|
includeMyRenotes: ps.includeMyRenotes,
|
|
|
|
includeRenotedMyNotes: ps.includeRenotedMyNotes,
|
|
|
|
includeLocalRenotes: ps.includeLocalRenotes,
|
|
|
|
withFiles: ps.withFiles,
|
2023-10-24 07:34:32 +02:00
|
|
|
withRenotes: ps.withRenotes,
|
2023-10-23 08:17:25 +02:00
|
|
|
}, me);
|
2023-10-03 13:26:11 +02:00
|
|
|
}
|
2023-10-23 08:17:25 +02:00
|
|
|
} else {
|
|
|
|
return await this.getFromDb({
|
|
|
|
untilId,
|
|
|
|
sinceId,
|
|
|
|
limit: ps.limit,
|
|
|
|
includeMyRenotes: ps.includeMyRenotes,
|
|
|
|
includeRenotedMyNotes: ps.includeRenotedMyNotes,
|
|
|
|
includeLocalRenotes: ps.includeLocalRenotes,
|
|
|
|
withFiles: ps.withFiles,
|
2023-10-24 07:34:32 +02:00
|
|
|
withRenotes: ps.withRenotes,
|
2023-10-23 08:17:25 +02:00
|
|
|
}, me);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2023-10-18 05:26:16 +02:00
|
|
|
|
2023-10-24 07:34:32 +02:00
|
|
|
private async getFromDb(ps: { untilId: string | null; sinceId: string | null; limit: number; includeMyRenotes: boolean; includeRenotedMyNotes: boolean; includeLocalRenotes: boolean; withFiles: boolean; withRenotes: boolean; }, me: MiLocalUser) {
|
2023-10-23 08:17:25 +02:00
|
|
|
const followees = await this.userFollowingService.getFollowees(me.id);
|
2023-10-29 06:16:36 +01:00
|
|
|
const followingChannels = await this.channelFollowingsRepository.find({
|
|
|
|
where: {
|
|
|
|
followerId: me.id,
|
|
|
|
},
|
|
|
|
});
|
2023-10-23 08:17:25 +02:00
|
|
|
|
|
|
|
//#region Construct query
|
|
|
|
const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note'), ps.sinceId, ps.untilId)
|
|
|
|
.innerJoinAndSelect('note.user', 'user')
|
|
|
|
.leftJoinAndSelect('note.reply', 'reply')
|
|
|
|
.leftJoinAndSelect('note.renote', 'renote')
|
|
|
|
.leftJoinAndSelect('reply.user', 'replyUser')
|
|
|
|
.leftJoinAndSelect('renote.user', 'renoteUser');
|
|
|
|
|
2023-10-29 06:16:36 +01:00
|
|
|
if (followees.length > 0 && followingChannels.length > 0) {
|
|
|
|
// ユーザー・チャンネルともにフォローあり
|
2023-10-23 08:17:25 +02:00
|
|
|
const meOrFolloweeIds = [me.id, ...followees.map(f => f.followeeId)];
|
2023-10-29 06:16:36 +01:00
|
|
|
const followingChannelIds = followingChannels.map(x => x.followeeId);
|
|
|
|
query.andWhere(new Brackets(qb => {
|
|
|
|
qb
|
|
|
|
.where('note.userId IN (:...meOrFolloweeIds)', { meOrFolloweeIds: meOrFolloweeIds })
|
|
|
|
.orWhere('note.channelId IN (:...followingChannelIds)', { followingChannelIds });
|
|
|
|
}));
|
|
|
|
} else if (followees.length > 0) {
|
|
|
|
// ユーザーフォローのみ(チャンネルフォローなし)
|
|
|
|
const meOrFolloweeIds = [me.id, ...followees.map(f => f.followeeId)];
|
|
|
|
query
|
|
|
|
.andWhere('note.channelId IS NULL')
|
|
|
|
.andWhere('note.userId IN (:...meOrFolloweeIds)', { meOrFolloweeIds: meOrFolloweeIds });
|
|
|
|
} else if (followingChannels.length > 0) {
|
|
|
|
// チャンネルフォローのみ(ユーザーフォローなし)
|
|
|
|
const followingChannelIds = followingChannels.map(x => x.followeeId);
|
|
|
|
query.andWhere(new Brackets(qb => {
|
|
|
|
qb
|
|
|
|
.where('note.channelId IN (:...followingChannelIds)', { followingChannelIds })
|
|
|
|
.orWhere('note.userId = :meId', { meId: me.id });
|
|
|
|
}));
|
2023-10-23 08:17:25 +02:00
|
|
|
} else {
|
2023-10-29 06:16:36 +01:00
|
|
|
// フォローなし
|
|
|
|
query
|
|
|
|
.andWhere('note.channelId IS NULL')
|
|
|
|
.andWhere('note.userId = :meId', { meId: me.id });
|
2023-10-23 08:17:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
query.andWhere(new Brackets(qb => {
|
|
|
|
qb
|
|
|
|
.where('note.replyId IS NULL') // 返信ではない
|
|
|
|
.orWhere(new Brackets(qb => {
|
|
|
|
qb // 返信だけど投稿者自身への返信
|
|
|
|
.where('note.replyId IS NOT NULL')
|
|
|
|
.andWhere('note.replyUserId = note.userId');
|
2023-10-18 10:17:56 +02:00
|
|
|
}));
|
2023-10-23 08:17:25 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
this.queryService.generateVisibilityQuery(query, me);
|
|
|
|
this.queryService.generateMutedUserQuery(query, me);
|
|
|
|
this.queryService.generateBlockedUserQuery(query, me);
|
|
|
|
this.queryService.generateMutedUserRenotesQueryForNotes(query, me);
|
|
|
|
|
|
|
|
if (ps.includeMyRenotes === false) {
|
|
|
|
query.andWhere(new Brackets(qb => {
|
|
|
|
qb.orWhere('note.userId != :meId', { meId: me.id });
|
|
|
|
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)');
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ps.includeRenotedMyNotes === false) {
|
|
|
|
query.andWhere(new Brackets(qb => {
|
|
|
|
qb.orWhere('note.renoteUserId != :meId', { meId: me.id });
|
|
|
|
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)');
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ps.includeLocalRenotes === false) {
|
|
|
|
query.andWhere(new Brackets(qb => {
|
|
|
|
qb.orWhere('note.renoteUserHost IS NOT NULL');
|
|
|
|
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)');
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ps.withFiles) {
|
|
|
|
query.andWhere('note.fileIds != \'{}\'');
|
|
|
|
}
|
2023-10-24 07:34:32 +02:00
|
|
|
|
|
|
|
if (ps.withRenotes === false) {
|
|
|
|
query.andWhere('note.renoteId IS NULL');
|
|
|
|
}
|
2023-10-23 08:17:25 +02:00
|
|
|
//#endregion
|
|
|
|
|
|
|
|
const timeline = await query.limit(ps.limit).getMany();
|
|
|
|
|
|
|
|
process.nextTick(() => {
|
|
|
|
this.activeUsersChart.read(me);
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
2023-10-23 08:17:25 +02:00
|
|
|
|
|
|
|
return await this.noteEntityService.packMany(timeline, me);
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
}
|