From 6430a191f7d291f67d3fa7788383533fce1558c2 Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Mon, 21 Oct 2024 14:18:34 -0400 Subject: [PATCH 1/4] fix duplicate users in the following feed --- .../server/api/endpoints/notes/following.ts | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/notes/following.ts b/packages/backend/src/server/api/endpoints/notes/following.ts index 83e8f404e9..5df52e4045 100644 --- a/packages/backend/src/server/api/endpoints/notes/following.ts +++ b/packages/backend/src/server/api/endpoints/notes/following.ts @@ -63,7 +63,32 @@ export default class extends Endpoint { // eslint- .setParameter('me', me.id) // Limit to latest notes - .innerJoin(SkLatestNote, 'latest', 'note.id = latest.note_id') + .innerJoin( + (sub: SelectQueryBuilder) => { + sub + .from(SkLatestNote, 'latest') + + // Return only one note per user + .addSelect('latest.user_id', 'user_id') + .addSelect('MAX(latest.note_id)', 'note_id') + .groupBy('latest.user_id'); + + // Match selected note types. + if (!ps.includeNonPublic) { + sub.andWhere('latest.is_public = true'); + } + if (!ps.includeReplies) { + sub.andWhere('latest.is_reply = false'); + } + if (!ps.includeQuotes) { + sub.andWhere('latest.is_quote = false'); + } + + return sub; + }, + 'latest', + 'note.id = latest.note_id', + ) // Avoid N+1 queries from the "pack" method .innerJoinAndSelect('note.user', 'user') @@ -87,17 +112,6 @@ export default class extends Endpoint { // eslint- query.andWhere('note."fileIds" != \'{}\''); } - // Match selected note types. - if (!ps.includeNonPublic) { - query.andWhere('latest.is_public'); - } - if (!ps.includeReplies) { - query.andWhere('latest.is_reply = false'); - } - if (!ps.includeQuotes) { - query.andWhere('latest.is_quote = false'); - } - // Match selected user types. if (!ps.includeBots) { query.andWhere('"user"."isBot" = false'); From 053b47d78a59e79f0ab61baaf87c6562eefc3517 Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Mon, 21 Oct 2024 14:19:26 -0400 Subject: [PATCH 2/4] return error when calling following feed with both includeReplies and filesOnly --- .../src/server/api/endpoints/notes/following.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/backend/src/server/api/endpoints/notes/following.ts b/packages/backend/src/server/api/endpoints/notes/following.ts index 5df52e4045..10c211b1fc 100644 --- a/packages/backend/src/server/api/endpoints/notes/following.ts +++ b/packages/backend/src/server/api/endpoints/notes/following.ts @@ -27,6 +27,14 @@ export const meta = { ref: 'Note', }, }, + + errors: { + bothWithRepliesAndWithFiles: { + message: 'Specifying both includeReplies and filesOnly is not supported', + code: 'BOTH_INCLUDE_REPLIES_AND_FILES_ONLY', + id: '91c8cb9f-36ed-46e7-9ca2-7df96ed6e222', + }, + }, } as const; export const paramDef = { @@ -58,6 +66,8 @@ export default class extends Endpoint { // eslint- private queryService: QueryService, ) { super(meta, paramDef, async (ps, me) => { + if (ps.includeReplies && ps.filesOnly) throw new ApiError(meta.errors.bothWithRepliesAndWithFiles); + const query = this.notesRepository .createQueryBuilder('note') .setParameter('me', me.id) From 04654b2f843b081f31ce8e5fe35b83213c0c014c Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Mon, 21 Oct 2024 14:20:30 -0400 Subject: [PATCH 3/4] add "followers" tab to following feed --- .../server/api/endpoints/notes/following.ts | 49 ++++++++++++++--- .../frontend/src/pages/following-feed.vue | 54 +++++++++++-------- packages/frontend/src/store.ts | 3 +- packages/misskey-js/src/autogen/types.ts | 7 ++- 4 files changed, 81 insertions(+), 32 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/notes/following.ts b/packages/backend/src/server/api/endpoints/notes/following.ts index 10c211b1fc..b6604b9798 100644 --- a/packages/backend/src/server/api/endpoints/notes/following.ts +++ b/packages/backend/src/server/api/endpoints/notes/following.ts @@ -4,12 +4,14 @@ */ import { Inject, Injectable } from '@nestjs/common'; +import { ObjectLiteral, SelectQueryBuilder } from 'typeorm'; import { SkLatestNote, MiFollowing } from '@/models/_.js'; import type { NotesRepository } from '@/models/_.js'; import { Endpoint } from '@/server/api/endpoint-base.js'; import { NoteEntityService } from '@/core/entities/NoteEntityService.js'; import { DI } from '@/di-symbols.js'; import { QueryService } from '@/core/QueryService.js'; +import { ApiError } from '@/server/api/error.js'; export const meta = { tags: ['notes'], @@ -34,13 +36,19 @@ export const meta = { code: 'BOTH_INCLUDE_REPLIES_AND_FILES_ONLY', id: '91c8cb9f-36ed-46e7-9ca2-7df96ed6e222', }, + bothWithFollowersAndIncludeNonPublic: { + message: 'Specifying both list:followers and includeNonPublic is not supported', + code: 'BOTH_LIST_FOLLOWERS_AND_INCLUDE_NON_PUBLIC', + id: '7a1b9cb6-235b-4e58-9c00-32c1796f502c', + }, }, } as const; export const paramDef = { type: 'object', properties: { - mutualsOnly: { type: 'boolean', default: false }, + list: { type: 'string', enum: ['following', 'followers', 'mutuals'], default: 'following' }, + filesOnly: { type: 'boolean', default: false }, includeNonPublic: { type: 'boolean', default: false }, includeReplies: { type: 'boolean', default: false }, @@ -67,6 +75,7 @@ export default class extends Endpoint { // eslint- ) { super(meta, paramDef, async (ps, me) => { if (ps.includeReplies && ps.filesOnly) throw new ApiError(meta.errors.bothWithRepliesAndWithFiles); + if (ps.list === 'followers' && ps.includeNonPublic) throw new ApiError(meta.errors.bothWithFollowersAndIncludeNonPublic); const query = this.notesRepository .createQueryBuilder('note') @@ -107,14 +116,15 @@ export default class extends Endpoint { // eslint- .leftJoinAndSelect('reply.user', 'replyUser') .leftJoinAndSelect('renote.user', 'renoteUser') .leftJoinAndSelect('note.channel', 'channel') + ; - // Limit to followers - .innerJoin(MiFollowing, 'following', 'latest.user_id = following."followeeId"') - .andWhere('following."followerId" = :me'); - - // Limit to mutuals, if requested - if (ps.mutualsOnly) { - query.innerJoin(MiFollowing, 'mutuals', 'latest.user_id = mutuals."followerId" AND mutuals."followeeId" = :me'); + // Select the appropriate collection of users + if (ps.list === 'followers') { + addFollower(query); + } else if (ps.list === 'following') { + addFollowee(query); + } else { + addMutual(query); } // Limit to files, if requested @@ -143,3 +153,26 @@ export default class extends Endpoint { // eslint- }); } } + +/** + * Limit to followers (they follow us) + */ +function addFollower>(query: T): T { + return query.innerJoin(MiFollowing, 'follower', 'follower."followerId" = latest.user_id AND follower."followeeId" = :me'); +} + +/** + * Limit to followees (we follow them) + */ +function addFollowee>(query: T): T { + return query.innerJoin(MiFollowing, 'followee', 'followee."followerId" = :me AND followee."followeeId" = latest.user_id'); +} + +/** + * Limit to mutuals (they follow us AND we follow them) + */ +function addMutual>(query: T): T { + addFollower(query); + addFollowee(query); + return query; +} diff --git a/packages/frontend/src/pages/following-feed.vue b/packages/frontend/src/pages/following-feed.vue index f49cafb52f..886946e867 100644 --- a/packages/frontend/src/pages/following-feed.vue +++ b/packages/frontend/src/pages/following-feed.vue @@ -5,10 +5,10 @@ SPDX-License-Identifier: AGPL-3.0-only