mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
trends: maybe make postgres trends work?
This commit is contained in:
parent
8db5489350
commit
8dc3b5d5a7
1 changed files with 29 additions and 20 deletions
|
|
@ -1,5 +1,6 @@
|
||||||
import { NostrFilter } from '@nostrify/nostrify';
|
import { NostrFilter } from '@nostrify/nostrify';
|
||||||
import { Stickynotes } from '@soapbox/stickynotes';
|
import { Stickynotes } from '@soapbox/stickynotes';
|
||||||
|
import { sql } from 'kysely';
|
||||||
|
|
||||||
import { Conf } from '@/config.ts';
|
import { Conf } from '@/config.ts';
|
||||||
import { DittoDatabase, DittoDB } from '@/db/DittoDB.ts';
|
import { DittoDatabase, DittoDB } from '@/db/DittoDB.ts';
|
||||||
|
|
@ -18,26 +19,34 @@ export async function getTrendingTagValues(
|
||||||
/** Filter of eligible events. */
|
/** Filter of eligible events. */
|
||||||
filter: NostrFilter,
|
filter: NostrFilter,
|
||||||
): Promise<{ value: string; authors: number; uses: number }[]> {
|
): Promise<{ value: string; authors: number; uses: number }[]> {
|
||||||
/*
|
if (dialect === 'postgres') {
|
||||||
SELECT
|
const { rows } = await sql<{ value: string; authors: number; uses: number }>`
|
||||||
LOWER(element.value) AS value,
|
SELECT
|
||||||
COUNT(DISTINCT nostr_events.pubkey) AS authors,
|
LOWER(element.value) AS value,
|
||||||
COUNT(*) as "uses"
|
COUNT(DISTINCT nostr_events.pubkey) AS authors,
|
||||||
FROM
|
COUNT(*) as "uses"
|
||||||
nostr_events,
|
FROM
|
||||||
jsonb_each_text(nostr_events.tags_index) kv,
|
nostr_events,
|
||||||
jsonb_array_elements_text(kv.value::jsonb) element
|
jsonb_each_text(nostr_events.tags_index) kv,
|
||||||
WHERE
|
jsonb_array_elements_text(kv.value::jsonb) element
|
||||||
kv.key = 't'
|
WHERE
|
||||||
AND nostr_events.kind = 1
|
kv.key = ANY(${tagNames})
|
||||||
AND nostr_events.created_at >= 1723325796
|
${filter.kinds ? sql`AND nostr_events.kind = ANY(${filter.kinds})` : sql``}
|
||||||
AND nostr_events.created_at <= 1723412196
|
${typeof filter.since === 'number' ? sql`AND nostr_events.created_at >= ${filter.since}` : sql``}
|
||||||
GROUP BY
|
${typeof filter.until === 'number' ? sql`AND nostr_events.created_at <= ${filter.until}` : sql``}
|
||||||
LOWER(element.value)
|
GROUP BY
|
||||||
ORDER BY
|
LOWER(element.value)
|
||||||
COUNT(DISTINCT nostr_events.pubkey) DESC
|
ORDER BY
|
||||||
LIMIT 20;
|
COUNT(DISTINCT nostr_events.pubkey) DESC
|
||||||
*/
|
${typeof filter.limit === 'number' ? sql`LIMIT ${filter.limit}` : sql``};`
|
||||||
|
.execute(kysely);
|
||||||
|
|
||||||
|
return rows.map((row) => ({
|
||||||
|
value: row.value,
|
||||||
|
authors: Number(row.authors),
|
||||||
|
uses: Number(row.uses),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
if (dialect === 'sqlite') {
|
if (dialect === 'sqlite') {
|
||||||
let query = kysely
|
let query = kysely
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue