Skip to content

High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.

Fix broken sorting lists by `subscriber_count`. Closes #2151.

Kailash Nadh5c0de6ef0b54
diff --git a/internal/core/lists.go b/internal/core/lists.go
index 0b8c7285..98c03a3f 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 51d18e3f..3a8c9a00 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 caf4a576..c5b755dd 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