added filter handling to query.go and sort "created_by" descending

This commit is contained in:
Dylan Cant
2023-01-19 10:54:11 -05:00
committed by fiatjaf
parent 0442b02394
commit 82e4174073
3 changed files with 8 additions and 4 deletions

View File

@@ -39,7 +39,7 @@ CREATE TABLE IF NOT EXISTS event (
CREATE UNIQUE INDEX IF NOT EXISTS ididx ON event USING btree (id text_pattern_ops);
CREATE INDEX IF NOT EXISTS pubkeyprefix ON event USING btree (pubkey text_pattern_ops);
CREATE INDEX IF NOT EXISTS timeidx ON event (created_at);
CREATE INDEX IF NOT EXISTS timeidx ON event (created_at DESC);
CREATE INDEX IF NOT EXISTS kindidx ON event (kind);
CREATE INDEX IF NOT EXISTS arbitrarytagvalues ON event USING gin (tagvalues);
`)

View File

@@ -128,11 +128,17 @@ func (b PostgresBackend) QueryEvents(filter *nostr.Filter) (events []nostr.Event
conditions = append(conditions, "true")
}
if filter.Limit < 1 || filter.Limit > 100 {
params = append(params, 100)
} else {
params = append(params, filter.Limit)
}
query := b.DB.Rebind(`SELECT
id, pubkey, created_at, kind, tags, content, sig
FROM event WHERE ` +
strings.Join(conditions, " AND ") +
" ORDER BY created_at LIMIT 100")
" ORDER BY created_at DESC LIMIT ?")
rows, err := b.DB.Query(query, params...)
if err != nil && err != sql.ErrNoRows {