summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/endpoints/notes/timeline.ts
diff options
context:
space:
mode:
authorMar0xy <marie@kaifa.ch>2023-10-31 19:33:24 +0100
committerMar0xy <marie@kaifa.ch>2023-10-31 19:33:24 +0100
commit4dd23a37931e6e2dc5935b2aa47a1fe51f1a9fc4 (patch)
tree6df74a71fb0cdd479edc1ad1e510a1729e402c0b /packages/backend/src/server/api/endpoints/notes/timeline.ts
parentmerge: fix file sorting on user notes (#122) (diff)
parentMerge branch 'develop' of https://github.com/misskey-dev/misskey into develop (diff)
downloadsharkey-4dd23a37931e6e2dc5935b2aa47a1fe51f1a9fc4.tar.gz
sharkey-4dd23a37931e6e2dc5935b2aa47a1fe51f1a9fc4.tar.bz2
sharkey-4dd23a37931e6e2dc5935b2aa47a1fe51f1a9fc4.zip
merge: upstream
Diffstat (limited to 'packages/backend/src/server/api/endpoints/notes/timeline.ts')
-rw-r--r--packages/backend/src/server/api/endpoints/notes/timeline.ts305
1 files changed, 185 insertions, 120 deletions
diff --git a/packages/backend/src/server/api/endpoints/notes/timeline.ts b/packages/backend/src/server/api/endpoints/notes/timeline.ts
index b98d1d9f91..e58e524988 100644
--- a/packages/backend/src/server/api/endpoints/notes/timeline.ts
+++ b/packages/backend/src/server/api/endpoints/notes/timeline.ts
@@ -5,7 +5,7 @@
import { Brackets } from 'typeorm';
import { Inject, Injectable } from '@nestjs/common';
-import type { NotesRepository } from '@/models/_.js';
+import type { MiNote, NotesRepository, ChannelFollowingsRepository } from '@/models/_.js';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { QueryService } from '@/core/QueryService.js';
import ActiveUsersChart from '@/core/chart/charts/active-users.js';
@@ -16,6 +16,8 @@ import { CacheService } from '@/core/CacheService.js';
import { isUserRelated } from '@/misc/is-user-related.js';
import { FunoutTimelineService } from '@/core/FunoutTimelineService.js';
import { UserFollowingService } from '@/core/UserFollowingService.js';
+import { MiLocalUser } from '@/models/User.js';
+import { MetaService } from '@/core/MetaService.js';
export const meta = {
tags: ['notes'],
@@ -57,6 +59,9 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
@Inject(DI.notesRepository)
private notesRepository: NotesRepository,
+ @Inject(DI.channelFollowingsRepository)
+ private channelFollowingsRepository: ChannelFollowingsRepository,
+
private noteEntityService: NoteEntityService,
private activeUsersChart: ActiveUsersChart,
private idService: IdService,
@@ -64,154 +69,214 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
private funoutTimelineService: FunoutTimelineService,
private userFollowingService: UserFollowingService,
private queryService: QueryService,
+ private metaService: MetaService,
) {
super(meta, paramDef, async (ps, me) => {
const untilId = ps.untilId ?? (ps.untilDate ? this.idService.gen(ps.untilDate!) : null);
const sinceId = ps.sinceId ?? (ps.sinceDate ? this.idService.gen(ps.sinceDate!) : null);
- 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);
+ const serverSettings = await this.metaService.fetch();
- 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');
+ 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),
+ ]);
- if (!ps.withBots) query.andWhere('user.isBot = FALSE');
+ let noteIds = await this.funoutTimelineService.get(ps.withFiles ? `homeTimelineWithFiles:${me.id}` : `homeTimeline:${me.id}`, untilId, sinceId);
+ noteIds = noteIds.slice(0, ps.limit);
- let timeline = await query.getMany();
+ let redisTimeline: MiNote[] = [];
- 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;
- }
- }
- if (note.reply && note.reply.visibility === 'followers') {
- if (!Object.hasOwn(followings, note.reply.userId)) return false;
- }
- if (note.user?.isSilenced && note.userId !== me.id && !followings[note.userId]) return false;
+ 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');
- return true;
- });
+ redisTimeline = await query.getMany();
- // TODO: フィルタした結果件数が足りなかった場合の対応
+ redisTimeline = redisTimeline.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;
+ }
+ }
+ if (note.reply && note.reply.visibility === 'followers') {
+ if (!Object.hasOwn(followings, note.reply.userId)) return false;
+ }
+ if (!ps.withBots && note.user?.isBot) return false;
- timeline.sort((a, b) => a.id > b.id ? -1 : 1);
+ return true;
+ });
- process.nextTick(() => {
- this.activeUsersChart.read(me);
- });
+ redisTimeline.sort((a, b) => a.id > b.id ? -1 : 1);
+ }
- return await this.noteEntityService.packMany(timeline, me);
- } else { // fallback to db
- const followees = await this.userFollowingService.getFollowees(me.id);
+ if (redisTimeline.length > 0) {
+ process.nextTick(() => {
+ this.activeUsersChart.read(me);
+ });
- //#region Construct query
- const query = this.queryService.makePaginationQuery(this.notesRepository.createQueryBuilder('note'), ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate)
- .andWhere('note.channelId IS NULL')
- .innerJoinAndSelect('note.user', 'user')
- .leftJoinAndSelect('note.reply', 'reply')
- .leftJoinAndSelect('note.renote', 'renote')
- .leftJoinAndSelect('reply.user', 'replyUser')
- .leftJoinAndSelect('renote.user', 'renoteUser');
+ return await this.noteEntityService.packMany(redisTimeline, me);
+ } 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,
+ withRenotes: ps.withRenotes,
+ withBots: ps.withBots,
+ }, me);
+ }
+ } else {
+ return await this.getFromDb({
+ untilId,
+ sinceId,
+ limit: ps.limit,
+ includeMyRenotes: ps.includeMyRenotes,
+ includeRenotedMyNotes: ps.includeRenotedMyNotes,
+ includeLocalRenotes: ps.includeLocalRenotes,
+ withFiles: ps.withFiles,
+ withRenotes: ps.withRenotes,
+ withBots: ps.withBots,
+ }, me);
+ }
+ });
+ }
- if (!ps.withBots) query.andWhere('user.isBot = FALSE');
+ private async getFromDb(ps: { untilId: string | null; sinceId: string | null; limit: number; includeMyRenotes: boolean; includeRenotedMyNotes: boolean; includeLocalRenotes: boolean; withFiles: boolean; withRenotes: boolean; withBots: boolean; }, me: MiLocalUser) {
+ const followees = await this.userFollowingService.getFollowees(me.id);
+ const followingChannels = await this.channelFollowingsRepository.find({
+ where: {
+ followerId: me.id,
+ },
+ });
- if (followees.length > 0) {
- const meOrFolloweeIds = [me.id, ...followees.map(f => f.followeeId)];
+ //#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');
- query.andWhere('note.userId IN (:...meOrFolloweeIds)', { meOrFolloweeIds: meOrFolloweeIds });
- } else {
- query.andWhere('note.userId = :meId', { meId: me.id });
- }
+ if (followees.length > 0 && followingChannels.length > 0) {
+ // ユーザー・チャンネルともにフォローあり
+ const meOrFolloweeIds = [me.id, ...followees.map(f => f.followeeId)];
+ const followingChannelIds = followingChannels.map(x => x.followeeId);
+ query.andWhere(new Brackets(qb => {
+ qb
+ .where(new Brackets(qb2 => {
+ qb2
+ .where('note.userId IN (:...meOrFolloweeIds)', { meOrFolloweeIds: meOrFolloweeIds })
+ .andWhere('note.channelId IS NULL');
+ }))
+ .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 });
+ }));
+ } else {
+ // フォローなし
+ query
+ .andWhere('note.channelId IS NULL')
+ .andWhere('note.userId = :meId', { meId: me.id });
+ }
- 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');
- }));
+ 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');
}));
+ }));
- this.queryService.generateVisibilityQuery(query, me);
- this.queryService.generateMutedUserQuery(query, me);
- this.queryService.generateBlockedUserQuery(query, me);
- this.queryService.generateMutedUserRenotesQueryForNotes(query, me);
+ 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.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.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.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 != \'{}\'');
- }
- //#endregion
+ if (ps.withFiles) {
+ query.andWhere('note.fileIds != \'{}\'');
+ }
- let timeline = await query.limit(ps.limit).getMany();
+ if (ps.withRenotes === false) {
+ query.andWhere('note.renoteId IS NULL');
+ }
- timeline = timeline.filter(note => {
- if (note.user?.isSilenced && note.userId !== me.id && !followings[note.userId]) return false;
- return true;
- });
+ if (!ps.withBots) query.andWhere('user.isBot = FALSE');
+ //#endregion
- process.nextTick(() => {
- this.activeUsersChart.read(me);
- });
+ const timeline = await query.limit(ps.limit).getMany();
- return await this.noteEntityService.packMany(timeline, me);
- }
+ process.nextTick(() => {
+ this.activeUsersChart.read(me);
});
+
+ return await this.noteEntityService.packMany(timeline, me);
}
}