From 5c0de6ef0b548f5722a9b0bec6d81159dc331395 Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Wed, 4 Dec 2024 22:40:13 +0530 Subject: [PATCH] Fix broken sorting lists by `subscriber_count`. Closes #2151. --- internal/core/lists.go | 5 ----- models/models.go | 2 +- queries.sql | 7 ++++--- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/internal/core/lists.go b/internal/core/lists.go index 0b8c7285b..98c03a3fa 100644 --- a/internal/core/lists.go +++ b/internal/core/lists.go @@ -62,11 +62,6 @@ func (c *Core) QueryLists(searchStr, typ, optin string, tags []string, orderBy, if l.Tags == nil { out[i].Tags = []string{} } - - // Total counts. - for _, c := range l.SubscriberCounts { - out[i].SubscriberCount += c - } } } diff --git a/models/models.go b/models/models.go index 51d18e3f0..3a8c9a000 100644 --- a/models/models.go +++ b/models/models.go @@ -293,7 +293,7 @@ type List struct { Optin string `db:"optin" json:"optin"` Tags pq.StringArray `db:"tags" json:"tags"` Description string `db:"description" json:"description"` - SubscriberCount int `db:"-" json:"subscriber_count"` + SubscriberCount int `db:"subscriber_count" json:"subscriber_count"` SubscriberCounts StringIntMap `db:"subscriber_statuses" json:"subscriber_statuses"` SubscriberID int `db:"subscriber_id" json:"-"` diff --git a/queries.sql b/queries.sql index caf4a5765..c5b755dde 100644 --- a/queries.sql +++ b/queries.sql @@ -432,7 +432,7 @@ SELECT * FROM lists WHERE (CASE WHEN $1 = '' THEN 1=1 ELSE type=$1::list_type EN -- name: query-lists WITH ls AS ( - SELECT COUNT(*) OVER () AS total, lists.* FROM lists WHERE + SELECT COUNT(*) OVER () AS total, lists.* FROM lists WHERE CASE WHEN $1 > 0 THEN id = $1 WHEN $2 != '' THEN uuid = $2::UUID @@ -451,11 +451,12 @@ WITH ls AS ( statuses AS ( SELECT list_id, - COALESCE(JSONB_OBJECT_AGG(status, subscriber_count) FILTER (WHERE status IS NOT NULL), '{}') AS subscriber_statuses + COALESCE(JSONB_OBJECT_AGG(status, subscriber_count) FILTER (WHERE status IS NOT NULL), '{}') AS subscriber_statuses, + SUM(subscriber_count) AS subscriber_count FROM mat_list_subscriber_stats GROUP BY list_id ) -SELECT ls.*, COALESCE(ss.subscriber_statuses, '{}') AS subscriber_statuses +SELECT ls.*, COALESCE(ss.subscriber_statuses, '{}') AS subscriber_statuses, COALESCE(ss.subscriber_count, 0) AS subscriber_count FROM ls LEFT JOIN statuses ss ON (ls.id = ss.list_id) ORDER BY %order%; -- name: get-lists-by-optin