Fix trends cache
This commit is contained in:
parent
5822253796
commit
80344e3c5f
|
@ -194,12 +194,8 @@ app.get('/api/v2/search', searchController);
|
||||||
|
|
||||||
app.get('/api/pleroma/frontend_configurations', frontendConfigController);
|
app.get('/api/pleroma/frontend_configurations', frontendConfigController);
|
||||||
|
|
||||||
app.get(
|
app.get('/api/v1/trends/tags', trendingTagsController);
|
||||||
'/api/v1/trends/tags',
|
app.get('/api/v1/trends', trendingTagsController);
|
||||||
cacheMiddleware({ cacheName: 'web', expires: Time.minutes(15) }),
|
|
||||||
trendingTagsController,
|
|
||||||
);
|
|
||||||
app.get('/api/v1/trends', cacheMiddleware({ cacheName: 'web', expires: Time.minutes(15) }), trendingTagsController);
|
|
||||||
|
|
||||||
app.get('/api/v1/suggestions', suggestionsV1Controller);
|
app.get('/api/v1/suggestions', suggestionsV1Controller);
|
||||||
app.get('/api/v2/suggestions', suggestionsV2Controller);
|
app.get('/api/v2/suggestions', suggestionsV2Controller);
|
||||||
|
|
|
@ -10,10 +10,20 @@ await TrendsWorker.open('data/trends.sqlite3');
|
||||||
|
|
||||||
const limitSchema = z.coerce.number().catch(10).transform((value) => Math.min(Math.max(value, 0), 20));
|
const limitSchema = z.coerce.number().catch(10).transform((value) => Math.min(Math.max(value, 0), 20));
|
||||||
|
|
||||||
|
let cache = getTrends();
|
||||||
|
|
||||||
|
Deno.cron('update trends cache', { minute: { every: 15 } }, async () => {
|
||||||
|
const trends = await getTrends();
|
||||||
|
cache = Promise.resolve(trends);
|
||||||
|
});
|
||||||
|
|
||||||
const trendingTagsController: AppController = async (c) => {
|
const trendingTagsController: AppController = async (c) => {
|
||||||
const limit = limitSchema.parse(c.req.query('limit'));
|
const limit = limitSchema.parse(c.req.query('limit'));
|
||||||
if (limit < 1) return c.json([]);
|
const trends = await cache;
|
||||||
|
return c.json(trends.slice(0, limit));
|
||||||
|
};
|
||||||
|
|
||||||
|
async function getTrends() {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const yesterday = new Date(now.getTime() - Time.days(1));
|
const yesterday = new Date(now.getTime() - Time.days(1));
|
||||||
const lastWeek = new Date(now.getTime() - Time.days(7));
|
const lastWeek = new Date(now.getTime() - Time.days(7));
|
||||||
|
@ -22,36 +32,34 @@ const trendingTagsController: AppController = async (c) => {
|
||||||
const tags = await TrendsWorker.getTrendingTags({
|
const tags = await TrendsWorker.getTrendingTags({
|
||||||
since: yesterday,
|
since: yesterday,
|
||||||
until: now,
|
until: now,
|
||||||
limit,
|
limit: 20,
|
||||||
});
|
});
|
||||||
|
|
||||||
return c.json(
|
return Promise.all(tags.map(async ({ tag, uses, accounts }) => ({
|
||||||
await Promise.all(tags.map(async ({ tag, uses, accounts }) => ({
|
name: tag,
|
||||||
name: tag,
|
url: Conf.local(`/tags/${tag}`),
|
||||||
url: Conf.local(`/tags/${tag}`),
|
history: [
|
||||||
history: [
|
// Use the full 24h query for the current day. Then use `offset: 1` to adjust for this below.
|
||||||
// Use the full 24h query for the current day. Then use `offset: 1` to adjust for this below.
|
// This result is more accurate than what Mastodon returns.
|
||||||
// This result is more accurate than what Mastodon returns.
|
{
|
||||||
{
|
day: String(Math.floor(stripTime(now).getTime() / 1000)),
|
||||||
day: String(Math.floor(stripTime(now).getTime() / 1000)),
|
accounts: String(accounts),
|
||||||
accounts: String(accounts),
|
uses: String(uses),
|
||||||
uses: String(uses),
|
},
|
||||||
},
|
...(await TrendsWorker.getTagHistory({
|
||||||
...(await TrendsWorker.getTagHistory({
|
tag,
|
||||||
tag,
|
since: lastWeek,
|
||||||
since: lastWeek,
|
until: now,
|
||||||
until: now,
|
limit: 6,
|
||||||
limit: 6,
|
offset: 1,
|
||||||
offset: 1,
|
})).map((history) => ({
|
||||||
})).map((history) => ({
|
// For some reason, Mastodon wants these to be strings... oh well.
|
||||||
// For some reason, Mastodon wants these to be strings... oh well.
|
day: String(Math.floor(history.day.getTime() / 1000)),
|
||||||
day: String(Math.floor(history.day.getTime() / 1000)),
|
accounts: String(history.accounts),
|
||||||
accounts: String(history.accounts),
|
uses: String(history.uses),
|
||||||
uses: String(history.uses),
|
})),
|
||||||
})),
|
],
|
||||||
],
|
})));
|
||||||
}))),
|
}
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export { trendingTagsController };
|
export { trendingTagsController };
|
||||||
|
|
Loading…
Reference in New Issue