merge: Protect featured timeline endpoint (!979)

View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/979

Approved-by: Marie <github@yuugi.dev>
This commit is contained in:
dakkar 2025-05-04 18:04:46 +00:00
commit 75b022cef9

View file

@ -12,6 +12,8 @@ import { FeaturedService } from '@/core/FeaturedService.js';
import { isUserRelated } from '@/misc/is-user-related.js'; import { isUserRelated } from '@/misc/is-user-related.js';
import { CacheService } from '@/core/CacheService.js'; import { CacheService } from '@/core/CacheService.js';
import { QueryService } from '@/core/QueryService.js'; import { QueryService } from '@/core/QueryService.js';
import { ApiError } from '@/server/api/error.js';
import { RoleService } from '@/core/RoleService.js';
export const meta = { export const meta = {
tags: ['notes'], tags: ['notes'],
@ -30,10 +32,19 @@ export const meta = {
}, },
}, },
// 10 calls per 5 seconds errors: {
ltlDisabled: {
message: 'Local timeline has been disabled.',
code: 'LTL_DISABLED',
id: '45a6eb02-7695-4393-b023-dd3be9aaaefd',
},
},
// Burst of 10 calls to handle tab reload, then 4/second for refresh
limit: { limit: {
duration: 1000 * 5, type: 'bucket',
max: 10, size: 10,
dripSize: 4,
}, },
} as const; } as const;
@ -60,8 +71,14 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
private noteEntityService: NoteEntityService, private noteEntityService: NoteEntityService,
private featuredService: FeaturedService, private featuredService: FeaturedService,
private queryService: QueryService, private queryService: QueryService,
private readonly roleService: RoleService,
) { ) {
super(meta, paramDef, async (ps, me) => { super(meta, paramDef, async (ps, me) => {
const policies = await this.roleService.getUserPolicies(me ? me.id : null);
if (!policies.ltlAvailable) {
throw new ApiError(meta.errors.ltlDisabled);
}
let noteIds: string[]; let noteIds: string[];
if (ps.channelId) { if (ps.channelId) {
noteIds = await this.featuredService.getInChannelNotesRanking(ps.channelId, 50); noteIds = await this.featuredService.getInChannelNotesRanking(ps.channelId, 50);