summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/endpoints
diff options
context:
space:
mode:
authorHazelnoot <acomputerdog@gmail.com>2024-10-15 21:50:26 +0000
committerHazelnoot <acomputerdog@gmail.com>2024-10-15 21:50:26 +0000
commitde9b99c937ff76e44bc3988fc58fcc2151d85e74 (patch)
treefb92a7724bb10da11fc44ae5dd59de9a8ce95c3e /packages/backend/src/server/api/endpoints
parentmerge: Restore report forwarding to Pleroma (resolves #641) (!690) (diff)
parentmove upgrade notes to separate file (diff)
downloadsharkey-de9b99c937ff76e44bc3988fc58fcc2151d85e74.tar.gz
sharkey-de9b99c937ff76e44bc3988fc58fcc2151d85e74.tar.bz2
sharkey-de9b99c937ff76e44bc3988fc58fcc2151d85e74.zip
merge: Add filter options to following feed (resolves #726) (!671)
View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/671 Closes #726 Approved-by: dakkar <dakkar@thenautilus.net> Approved-by: Marie <github@yuugi.dev>
Diffstat (limited to 'packages/backend/src/server/api/endpoints')
-rw-r--r--packages/backend/src/server/api/endpoints/notes/following.ts38
-rw-r--r--packages/backend/src/server/api/endpoints/users/notes.ts59
2 files changed, 90 insertions, 7 deletions
diff --git a/packages/backend/src/server/api/endpoints/notes/following.ts b/packages/backend/src/server/api/endpoints/notes/following.ts
index 436160f250..83e8f404e9 100644
--- a/packages/backend/src/server/api/endpoints/notes/following.ts
+++ b/packages/backend/src/server/api/endpoints/notes/following.ts
@@ -4,7 +4,7 @@
*/
import { Inject, Injectable } from '@nestjs/common';
-import { LatestNote, MiFollowing } from '@/models/_.js';
+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';
@@ -33,6 +33,12 @@ export const paramDef = {
type: 'object',
properties: {
mutualsOnly: { type: 'boolean', default: false },
+ filesOnly: { type: 'boolean', default: false },
+ includeNonPublic: { type: 'boolean', default: false },
+ includeReplies: { type: 'boolean', default: false },
+ includeQuotes: { type: 'boolean', default: false },
+ includeBots: { type: 'boolean', default: true },
+
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
sinceId: { type: 'string', format: 'misskey:id' },
untilId: { type: 'string', format: 'misskey:id' },
@@ -52,12 +58,12 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
private queryService: QueryService,
) {
super(meta, paramDef, async (ps, me) => {
- let query = this.notesRepository
+ const query = this.notesRepository
.createQueryBuilder('note')
.setParameter('me', me.id)
// Limit to latest notes
- .innerJoin(LatestNote, 'latest', 'note.id = latest.note_id')
+ .innerJoin(SkLatestNote, 'latest', 'note.id = latest.note_id')
// Avoid N+1 queries from the "pack" method
.innerJoinAndSelect('note.user', 'user')
@@ -73,8 +79,28 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
// Limit to mutuals, if requested
if (ps.mutualsOnly) {
- query = query
- .innerJoin(MiFollowing, 'mutuals', 'latest.user_id = mutuals."followerId" AND mutuals."followeeId" = :me');
+ query.innerJoin(MiFollowing, 'mutuals', 'latest.user_id = mutuals."followerId" AND mutuals."followeeId" = :me');
+ }
+
+ // Limit to files, if requested
+ if (ps.filesOnly) {
+ 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');
}
// Respect blocks and mutes
@@ -82,7 +108,7 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
this.queryService.generateMutedUserQuery(query, me);
// Support pagination
- query = this.queryService
+ this.queryService
.makePaginationQuery(query, ps.sinceId, ps.untilId, ps.sinceDate, ps.untilDate)
.orderBy('note.id', 'DESC')
.take(ps.limit);
diff --git a/packages/backend/src/server/api/endpoints/users/notes.ts b/packages/backend/src/server/api/endpoints/users/notes.ts
index cc76c12f1d..efea15ca80 100644
--- a/packages/backend/src/server/api/endpoints/users/notes.ts
+++ b/packages/backend/src/server/api/endpoints/users/notes.ts
@@ -17,6 +17,7 @@ import { MiLocalUser } from '@/models/User.js';
import { FanoutTimelineEndpointService } from '@/core/FanoutTimelineEndpointService.js';
import { FanoutTimelineName } from '@/core/FanoutTimelineService.js';
import { ApiError } from '@/server/api/error.js';
+import { isQuote, isRenote } from '@/misc/is-renote.js';
export const meta = {
tags: ['users', 'notes'],
@@ -51,7 +52,11 @@ export const paramDef = {
properties: {
userId: { type: 'string', format: 'misskey:id' },
withReplies: { type: 'boolean', default: false },
+ withRepliesToSelf: { type: 'boolean', default: true },
+ withQuotes: { type: 'boolean', default: true },
withRenotes: { type: 'boolean', default: true },
+ withBots: { type: 'boolean', default: true },
+ withNonPublic: { type: 'boolean', default: true },
withChannelNotes: { type: 'boolean', default: false },
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
sinceId: { type: 'string', format: 'misskey:id' },
@@ -103,6 +108,11 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
withChannelNotes: ps.withChannelNotes,
withFiles: ps.withFiles,
withRenotes: ps.withRenotes,
+ withQuotes: ps.withQuotes,
+ withBots: ps.withBots,
+ withNonPublic: ps.withNonPublic,
+ withRepliesToOthers: ps.withReplies,
+ withRepliesToSelf: ps.withRepliesToSelf,
}, me);
return await this.noteEntityService.packMany(timeline, me);
@@ -127,11 +137,17 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
excludeReplies: ps.withChannelNotes && !ps.withReplies, // userTimelineWithChannel may include replies
excludeNoFiles: ps.withChannelNotes && ps.withFiles, // userTimelineWithChannel may include notes without files
excludePureRenotes: !ps.withRenotes,
+ excludeBots: !ps.withBots,
noteFilter: note => {
if (note.channel?.isSensitive && !isSelf) return false;
if (note.visibility === 'specified' && (!me || (me.id !== note.userId && !note.visibleUserIds.some(v => v === me.id)))) return false;
if (note.visibility === 'followers' && !isFollowing && !isSelf) return false;
+ // These are handled by DB fallback, but we duplicate them here in case a timeline was already populated with notes
+ if (!ps.withRepliesToSelf && note.reply?.userId === note.userId) return false;
+ if (!ps.withQuotes && isRenote(note) && isQuote(note)) return false;
+ if (!ps.withNonPublic && note.visibility !== 'public') return false;
+
return true;
},
dbFallback: async (untilId, sinceId, limit) => await this.getFromDb({
@@ -142,6 +158,11 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
withChannelNotes: ps.withChannelNotes,
withFiles: ps.withFiles,
withRenotes: ps.withRenotes,
+ withQuotes: ps.withQuotes,
+ withBots: ps.withBots,
+ withNonPublic: ps.withNonPublic,
+ withRepliesToOthers: ps.withReplies,
+ withRepliesToSelf: ps.withRepliesToSelf,
}, me),
});
@@ -157,6 +178,11 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
withChannelNotes: boolean,
withFiles: boolean,
withRenotes: boolean,
+ withQuotes: boolean,
+ withBots: boolean,
+ withNonPublic: boolean,
+ withRepliesToOthers: boolean,
+ withRepliesToSelf: boolean,
}, me: MiLocalUser | null) {
const isSelf = me && (me.id === ps.userId);
@@ -188,7 +214,9 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
query.andWhere('note.fileIds != \'{}\'');
}
- if (ps.withRenotes === false) {
+ if (!ps.withRenotes && !ps.withQuotes) {
+ query.andWhere('note.renoteId IS NULL');
+ } else if (!ps.withRenotes) {
query.andWhere(new Brackets(qb => {
qb.orWhere('note.userId != :userId', { userId: ps.userId });
qb.orWhere('note.renoteId IS NULL');
@@ -196,6 +224,35 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
qb.orWhere('note.fileIds != \'{}\'');
qb.orWhere('0 < (SELECT COUNT(*) FROM poll WHERE poll."noteId" = note.id)');
}));
+ } else if (!ps.withQuotes) {
+ query.andWhere(`
+ (
+ note."renoteId" IS NULL
+ OR (
+ note.text IS NULL
+ AND note.cw IS NULL
+ AND note."replyId" IS NULL
+ AND note."hasPoll" IS FALSE
+ AND note."fileIds" = '{}'
+ )
+ )
+ `);
+ }
+
+ if (!ps.withRepliesToOthers && !ps.withRepliesToSelf) {
+ query.andWhere('reply.id IS NULL');
+ } else if (!ps.withRepliesToOthers) {
+ query.andWhere('(reply.id IS NULL OR reply."userId" = note."userId")');
+ } else if (!ps.withRepliesToSelf) {
+ query.andWhere('(reply.id IS NULL OR reply."userId" != note."userId")');
+ }
+
+ if (!ps.withNonPublic) {
+ query.andWhere('note.visibility = \'public\'');
+ }
+
+ if (!ps.withBots) {
+ query.andWhere('"user"."isBot" = false');
}
return await query.limit(ps.limit).getMany();