Skip to content

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

Add ability to filter subscriber queries by `?status` field.

Kailash Nadh527dd644922a
diff --git a/cmd/subscribers.go b/cmd/subscribers.go
index 2c9a44f4..7666b3f1 100644
--- a/cmd/subscribers.go
+++ b/cmd/subscribers.go
@@ -32,6 +32,7 @@ var subscribersQueryDefaults = map[string]string{
"order_by": "",
"order": "",
"subscription_status": "",
+ "status": "",
}
// subQueryReq is a "catch all" struct for reading various
@@ -290,6 +291,7 @@ func (a *App) getSubscribers(c echo.Context, listID int) ([]models.Subscriber, m
query,
listIDs,
q.Get("subscription_status"),
+ q.Get("status"),
q.Get("order"),
q.Get("order_by"),
pg.Offset,
@@ -366,15 +368,16 @@ func (a *App) QuerySubscribers(c echo.Context) error {
}
var (
- searchStr = strings.TrimSpace(c.FormValue("search"))
- subStatus = c.FormValue("subscription_status")
- order = c.FormValue("order")
- orderBy = c.FormValue("order_by")
- pg = a.pg.NewFromURL(c.Request().URL.Query())
+ searchStr = strings.TrimSpace(c.FormValue("search"))
+ subStatus = c.FormValue("subscription_status")
+ subscriberStatus = c.FormValue("status")
+ order = c.FormValue("order")
+ orderBy = c.FormValue("order_by")
+ pg = a.pg.NewFromURL(c.Request().URL.Query())
)
// Query subscribers from the DB.
- res, total, err := a.core.QuerySubscribers(searchStr, query, listIDs, subStatus, order, orderBy, pg.Offset, pg.Limit)
+ res, total, err := a.core.QuerySubscribers(searchStr, query, listIDs, subStatus, subscriberStatus, order, orderBy, pg.Offset, pg.Limit)
if err != nil {
return err
}
diff --git a/internal/core/subscribers.go b/internal/core/subscribers.go
index 9e9a1f2b..66852c2e 100644
--- a/internal/core/subscribers.go
+++ b/internal/core/subscribers.go
@@ -103,7 +103,7 @@ func (c *Core) GetSubscribersByEmail(emails []string) (models.Subscribers, error
}
// QuerySubscribers queries and returns paginated subscrribers based on the given params including the total count.
-func (c *Core) QuerySubscribers(searchStr, queryExp string, listIDs []int, subStatus string, order, orderBy string, offset, limit int) (models.Subscribers, int, error) {
+func (c *Core) QuerySubscribers(searchStr, queryExp string, listIDs []int, subStatus, subscriberStatus string, order, orderBy string, offset, limit int) (models.Subscribers, int, error) {
// Sort params.
if !strSliceContains(orderBy, subQuerySortFields) {
orderBy = "subscribers.id"
@@ -128,7 +128,7 @@ func (c *Core) QuerySubscribers(searchStr, queryExp string, listIDs []int, subSt
stmt = strings.ReplaceAll(stmt, "%order%", orderBy+" "+order)
// Validate the tables used in the query.
- if err := validateQueryTables(c.db, stmt, allowedSubQueryTables, pq.Array(listIDs), subStatus, searchStr, offset, limit); err != nil {
+ if err := validateQueryTables(c.db, stmt, allowedSubQueryTables, pq.Array(listIDs), subStatus, searchStr, offset, limit, subscriberStatus); err != nil {
c.log.Printf("error validating query tables: %v", err)
return nil, 0, echo.NewHTTPError(http.StatusBadRequest,
c.i18n.Ts("subscribers.errorPreparingQuery", "error", err.Error()))
@@ -136,7 +136,7 @@ func (c *Core) QuerySubscribers(searchStr, queryExp string, listIDs []int, subSt
// Create a readonly transaction that just does COUNT() to obtain the count of results
// and to ensure that the arbitrary query is indeed readonly.
- total, err := c.getSubscriberCount(searchStr, cond, subStatus, listIDs)
+ total, err := c.getSubscriberCount(searchStr, cond, subStatus, subscriberStatus, listIDs)
if err != nil {
c.log.Printf("error getting subscriber count: %v", err)
return nil, 0, err
@@ -155,7 +155,7 @@ func (c *Core) QuerySubscribers(searchStr, queryExp string, listIDs []int, subSt
defer tx.Rollback()
var out models.Subscribers
- if err := tx.Select(&out, stmt, pq.Array(listIDs), subStatus, searchStr, offset, limit); err != nil {
+ if err := tx.Select(&out, stmt, pq.Array(listIDs), subStatus, searchStr, offset, limit, subscriberStatus); err != nil {
return nil, 0, echo.NewHTTPError(http.StatusInternalServerError,
c.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.subscribers}", "error", pqErrMsg(err)))
}
@@ -258,7 +258,7 @@ func (c *Core) ExportSubscribers(searchStr, query string, subIDs, listIDs []int,
// Create a readonly transaction that just does COUNT() to obtain the count of results
// and to ensure that the arbitrary query is indeed readonly.
- if _, err := c.getSubscriberCount(searchStr, cond, subStatus, listIDs); err != nil {
+ if _, err := c.getSubscriberCount(searchStr, cond, subStatus, "", listIDs); err != nil {
c.log.Printf("error getting subscriber count: %v", err)
return nil, err
}
@@ -567,9 +567,10 @@ func (c *Core) DeleteBlocklistedSubscribers() (int, error) {
return int(n), nil
}
-func (c *Core) getSubscriberCount(searchStr, queryExp, subStatus string, listIDs []int) (int, error) {
+func (c *Core) getSubscriberCount(searchStr, queryExp, subStatus, subscriberStatus string, listIDs []int) (int, error) {
// If there's no condition, it's a "get all" call which can probably be optionally pulled from cache.
- if queryExp == "" {
+ // The cached stats don't track the subscriber status, so skip the cache when there's a query filter.
+ if queryExp == "" && subscriberStatus == "" {
_ = c.refreshCache(matListSubStats, false)
total := 0
@@ -593,7 +594,7 @@ func (c *Core) getSubscriberCount(searchStr, queryExp, subStatus string, listIDs
// Execute the readonly query and get the count of results.
total := 0
- if err := tx.Get(&total, stmt, pq.Array(listIDs), subStatus, searchStr); err != nil {
+ if err := tx.Get(&total, stmt, pq.Array(listIDs), subStatus, searchStr, subscriberStatus); err != nil {
return 0, echo.NewHTTPError(http.StatusInternalServerError,
c.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.subscribers}", "error", pqErrMsg(err)))
}
diff --git a/queries/subscribers.sql b/queries/subscribers.sql
index fe59db40..dfb48d63 100644
--- a/queries/subscribers.sql
+++ b/queries/subscribers.sql
@@ -299,6 +299,7 @@ SELECT subscribers.* FROM subscribers
)
WHERE (CARDINALITY($1) = 0 OR subscriber_lists.list_id = ANY($1::INT[]))
AND (CASE WHEN $3 != '' THEN name ~* $3 OR email ~* $3 ELSE TRUE END)
+ AND ($6 = '' OR subscribers.status = $6::subscriber_status)
AND %query%
ORDER BY %order% OFFSET $4 LIMIT (CASE WHEN $5 < 1 THEN NULL ELSE $5 END);
@@ -314,6 +315,7 @@ SELECT COUNT(*) AS total FROM subscribers
)
WHERE (CARDINALITY($1) = 0 OR subscriber_lists.list_id = ANY($1::INT[]))
AND (CASE WHEN $3 != '' THEN name ~* $3 OR email ~* $3 ELSE TRUE END)
+ AND ($4 = '' OR subscribers.status = $4::subscriber_status)
AND %query%;
-- name: query-subscribers-count-all
diff --git a/static/admin/views/subscribers.html b/static/admin/views/subscribers.html
index b08de2c6..a1bd122b 100644
--- a/static/admin/views/subscribers.html
+++ b/static/admin/views/subscribers.html
@@ -2,6 +2,7 @@
{{ template "admin-header" . }}
{{- $d := .Data -}}
{{- $subStatus := $d.Page.Param "subscription_status" -}}
+{{- $status := $d.Page.Param "status" -}}
{{- $base := URI "/admin/subscribers" -}}
{{- if $d.CurrentList }}{{ $base = printf "%s/lists/%d" (URI "/admin/subscribers") $d.CurrentList.ID }}{{ end -}}
@@ -23,6 +24,7 @@
<span>({{ $d.Page.Total }})</span>
{{- if $d.CurrentList }} {{ template "filter-badge" dict "Label" $d.CurrentList.Name "Class" "outline" "URL" (URI "/admin/subscribers") }}{{ end }}
{{- if $subStatus }} {{ template "filter-badge" dict "Label" (.L.T (printf "subscribers.status.%s" $subStatus)) "Class" (printf "status-%s" $subStatus) "URL" ($d.Page.FilterURL $base "subscription_status") }}{{ end }}
+ {{- if $status }} {{ template "filter-badge" dict "Label" (.L.T (printf "subscribers.status.%s" $status)) "Class" (printf "status-%s" $status) "URL" ($d.Page.FilterURL $base "status") }}{{ end }}
</h1>
</div>
<div class="col-6 col-end hstack justify-end">
@@ -101,11 +103,7 @@
<td class="email">
<a href="{{ URI "/admin/subscribers" }}/{{ .ID }}">{{ .Email }}</a>
{{- if ne .Status "enabled" }}
- {{- if $d.Can "subscribers:sql_query" }}
- <a href="{{ $base }}?query={{ printf "subscribers.status = '%s'" .Status | urlquery }}">{{ Badge ($.L.T (printf "subscribers.status.%s" .Status)) (printf "status-%s" .Status) }}</a>
- {{- else }}
- {{ Badge ($.L.T (printf "subscribers.status.%s" .Status)) (printf "status-%s" .Status) }}
- {{- end }}
+ <a href="{{ $base }}?status={{ .Status }}">{{ Badge ($.L.T (printf "subscribers.status.%s" .Status)) (printf "status-%s" .Status) }}</a>
{{- end }}
<div class="badge-list hstack gap-1">
{{- range .ParsedLists }}