mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
Implements trends for SQLite
This commit is contained in:
parent
e6b18e7d95
commit
8db5489350
1 changed files with 35 additions and 22 deletions
|
|
@ -12,7 +12,7 @@ const console = new Stickynotes('ditto:trends');
|
|||
/** Get trending tag values for a given tag in the given time frame. */
|
||||
export async function getTrendingTagValues(
|
||||
/** Kysely instance to execute queries on. */
|
||||
db: DittoDatabase,
|
||||
{ dialect, kysely }: DittoDatabase,
|
||||
/** Tag name to filter by, eg `t` or `r`. */
|
||||
tagNames: string[],
|
||||
/** Filter of eligible events. */
|
||||
|
|
@ -39,28 +39,41 @@ export async function getTrendingTagValues(
|
|||
LIMIT 20;
|
||||
*/
|
||||
|
||||
if (dialect === 'sqlite') {
|
||||
let query = kysely
|
||||
.selectFrom('nostr_tags')
|
||||
.select(({ fn }) => [
|
||||
'nostr_tags.value',
|
||||
fn.agg<number>('count', ['nostr_tags.pubkey']).distinct().as('authors'),
|
||||
fn.countAll<number>().as('uses'),
|
||||
])
|
||||
.where('nostr_tags.name', 'in', tagNames)
|
||||
.groupBy('nostr_tags.value')
|
||||
.orderBy((c) => c.fn.agg('count', ['nostr_tags.pubkey']).distinct(), 'desc');
|
||||
|
||||
if (filter.kinds) {
|
||||
query = query.where('nostr_tags.kind', 'in', filter.kinds);
|
||||
}
|
||||
if (typeof filter.since === 'number') {
|
||||
query = query.where('nostr_tags.created_at', '>=', filter.since);
|
||||
}
|
||||
if (typeof filter.until === 'number') {
|
||||
query = query.where('nostr_tags.created_at', '<=', filter.until);
|
||||
}
|
||||
if (typeof filter.limit === 'number') {
|
||||
query = query.limit(filter.limit);
|
||||
}
|
||||
|
||||
const rows = await query.execute();
|
||||
|
||||
return rows.map((row) => ({
|
||||
value: row.value,
|
||||
authors: Number(row.authors),
|
||||
uses: Number(row.uses),
|
||||
}));
|
||||
}
|
||||
|
||||
return [];
|
||||
|
||||
// if (filter.kinds) {
|
||||
// query = query.where('kind', 'in', filter.kinds);
|
||||
// }
|
||||
// if (typeof filter.since === 'number') {
|
||||
// query = query.where('created_at', '>=', filter.since);
|
||||
// }
|
||||
// if (typeof filter.until === 'number') {
|
||||
// query = query.where('created_at', '<=', filter.until);
|
||||
// }
|
||||
// if (typeof filter.limit === 'number') {
|
||||
// query = query.limit(filter.limit);
|
||||
// }
|
||||
|
||||
// const rows = await query.execute();
|
||||
|
||||
// return rows.map((row) => ({
|
||||
// value: row.value,
|
||||
// authors: Number(row.authors),
|
||||
// uses: Number(row.uses),
|
||||
// }));
|
||||
}
|
||||
|
||||
/** Get trending tags and publish an event with them. */
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue