Skip to content

Commit

Permalink
database: add rank join
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-dionysos committed Feb 17, 2025
1 parent c8f8abb commit e0ab749
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 84 deletions.
24 changes: 18 additions & 6 deletions database/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,21 @@ func (db *dbClient) generateSelectEventsSQL(ctx context.Context, filters model.F
if strings.Contains(filters.String(), discoverContentCreatorsToFollow) {
orderBy = " order by random()"
}

var JoinString string
if v, ok := params["rank"]; ok && v.(rank) != rankUndef {
switch v.(rank) {
case rankTOP:
// All time top.
JoinString = ` inner join ranked_events r on e.rid = r.event_rid`
orderBy = ` order by r.score desc, e.system_created_at desc`
case rankTrending:
// 24h trending.
JoinString = ` inner join ranked_events r on e.rid = r.event_rid and ((unixepoch() - min(unixepoch(), e.created_at)) < 86400)`
orderBy = ` order by r.score desc, e.system_created_at desc`
}
}

if depClause == "" {
if whereSearch != "" {
sql, err := db.searchWithoutDepsSQL(whereMain, whereSearch, systemCreatedAtFilter, limitQuery)
Expand All @@ -638,7 +653,7 @@ func (db *dbClient) generateSelectEventsSQL(ctx context.Context, filters model.F
e.content,
tags as jtags
from
events e
events e` + JoinString + `
where ` + systemCreatedAtFilter + `(` + whereMain + `)` + orderBy + limitQuery, params, nil
}
if whereSearch != "" {
Expand All @@ -665,11 +680,8 @@ with eventsmain as (
e.h_tag,
tags as jtags
from
events e
where ` + systemCreatedAtFilter + `(` + whereMain + `)
order by
system_created_at desc
` + limitQuery + `
events e` + JoinString + `
where ` + systemCreatedAtFilter + `(` + whereMain + `)` + orderBy + limitQuery + `
)
select
*
Expand Down
Loading

0 comments on commit e0ab749

Please sign in to comment.