Trends: support limit param

This commit is contained in:
Alex Gleason
2023-07-25 17:44:38 -05:00
parent 48b7310d52
commit cbb294dbc2
3 changed files with 11 additions and 5 deletions

View File

@@ -1,13 +1,19 @@
import { type AppController } from '@/app.ts';
import { Conf } from '@/config.ts';
import { z } from '@/deps.ts';
import { trends } from '@/trends.ts';
import { Time } from '@/utils.ts';
const limitSchema = z.coerce.number().catch(10).transform((value) => Math.min(Math.max(value, 0), 20));
const trendingTagsController: AppController = (c) => {
const limit = limitSchema.parse(c.req.query('limit'));
if (limit < 1) return c.json([]);
const now = new Date();
const yesterday = new Date(now.getTime() - Time.days(1));
const tags = trends.getTrendingTags(yesterday, now);
const tags = trends.getTrendingTags(yesterday, now, limit);
return c.json(tags.map(({ name, accounts, uses }) => ({
name,