Trends: support `limit` param
This commit is contained in:
parent
48b7310d52
commit
cbb294dbc2
|
@ -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,
|
||||
|
|
|
@ -25,7 +25,7 @@ class TrendsDB {
|
|||
}, Time.days(1));
|
||||
}
|
||||
|
||||
getTrendingTags(since: Date, until: Date) {
|
||||
getTrendingTags(since: Date, until: Date, limit = 10) {
|
||||
return this.#db.query<string[]>(
|
||||
`
|
||||
SELECT tag, COUNT(DISTINCT pubkey8), COUNT(*)
|
||||
|
@ -33,9 +33,9 @@ class TrendsDB {
|
|||
WHERE inserted_at >= ? AND inserted_at < ?
|
||||
GROUP BY tag
|
||||
ORDER BY COUNT(DISTINCT pubkey8)
|
||||
DESC LIMIT 10;
|
||||
DESC LIMIT ?;
|
||||
`,
|
||||
[since, until],
|
||||
[since, until, limit],
|
||||
).map((row) => ({
|
||||
name: row[0],
|
||||
accounts: Number(row[1]),
|
||||
|
|
|
@ -81,7 +81,7 @@ async function parseBody(req: Request): Promise<unknown> {
|
|||
const paginationSchema = z.object({
|
||||
since: z.coerce.number().optional().catch(undefined),
|
||||
until: z.lazy(() => z.coerce.number().catch(nostrNow())),
|
||||
limit: z.coerce.number().min(0).max(40).catch(20),
|
||||
limit: z.coerce.number().catch(20).transform((value) => Math.min(Math.max(value, 0), 40)),
|
||||
});
|
||||
|
||||
type PaginationParams = z.infer<typeof paginationSchema>;
|
||||
|
|
Loading…
Reference in New Issue