Skip to content

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

Fix GET `/subscribers` calls not accepting multiple `list_id`s.

Kailash Nadh3386de40c747
diff --git a/cmd/subscribers.go b/cmd/subscribers.go
index 57780831..ccb95b97 100644
--- a/cmd/subscribers.go
+++ b/cmd/subscribers.go
@@ -106,9 +106,6 @@ func handleQuerySubscribers(c echo.Context) error {
app = c.Get("app").(*App)
pg = getPagination(c.QueryParams(), 30)
- // Limit the subscribers to a particular list?
- listID, _ = strconv.Atoi(c.FormValue("list_id"))
-
// The "WHERE ?" bit.
query = sanitizeSQLExp(c.FormValue("query"))
orderBy = c.FormValue("order_by")
@@ -116,11 +113,10 @@ func handleQuerySubscribers(c echo.Context) error {
out = subsWrap{Results: make([]models.Subscriber, 0, 1)}
)
- listIDs := pq.Int64Array{}
- if listID < 0 {
- return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.errorID"))
- } else if listID > 0 {
- listIDs = append(listIDs, int64(listID))
+ // Limit the subscribers to sepcific lists?
+ listIDs, err := getQueryListIDs(c.QueryParams())
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
// There's an arbitrary query condition.
@@ -196,18 +192,14 @@ func handleExportSubscribers(c echo.Context) error {
var (
app = c.Get("app").(*App)
- // Limit the subscribers to a particular list?
- listID, _ = strconv.Atoi(c.FormValue("list_id"))
-
// The "WHERE ?" bit.
query = sanitizeSQLExp(c.FormValue("query"))
)
- listIDs := pq.Int64Array{}
- if listID < 0 {
- return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.errorID"))
- } else if listID > 0 {
- listIDs = append(listIDs, int64(listID))
+ // Limit the subscribers to sepcific lists?
+ listIDs, err := getQueryListIDs(c.QueryParams())
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
// There's an arbitrary query condition.
@@ -866,3 +858,22 @@ func sanitizeSQLExp(q string) string {
}
return q
}
+
+func getQueryListIDs(qp url.Values) (pq.Int64Array, error) {
+ out := pq.Int64Array{}
+ if vals, ok := qp["list_id"]; ok {
+ for _, v := range vals {
+ if v == "" {
+ continue
+ }
+
+ listID, err := strconv.Atoi(v)
+ if err != nil {
+ return nil, err
+ }
+ out = append(out, int64(listID))
+ }
+ }
+
+ return out, nil
+}
diff --git a/frontend/src/views/Subscribers.vue b/frontend/src/views/Subscribers.vue
index 0f24b922..3d76e799 100644
--- a/frontend/src/views/Subscribers.vue
+++ b/frontend/src/views/Subscribers.vue
@@ -399,7 +399,10 @@ export default Vue.extend({
this.$utils.confirm(this.$t('subscribers.confirmExport', { num: this.subscribers.total }), () => {
const q = new URLSearchParams();
q.append('query', this.queryParams.queryExp);
- q.append('list_id', this.queryParams.listID);
+
+ if (this.queryParams.listID) {
+ q.append('list_id', this.queryParams.listID);
+ }
document.location.href = `${uris.exportSubscribers}?${q.toString()}`;
});
},
diff --git a/queries.sql b/queries.sql
index 108abf6d..9160cd29 100644
--- a/queries.sql
+++ b/queries.sql
@@ -241,7 +241,7 @@ SELECT subscribers.* FROM subscribers
(CASE WHEN CARDINALITY($1::INT[]) > 0 THEN true ELSE false END)
AND subscriber_lists.subscriber_id = subscribers.id
)
- WHERE subscriber_lists.list_id = ALL($1::INT[])
+ WHERE (CARDINALITY($1) = 0 OR subscriber_lists.list_id = ANY($1::INT[]))
%s
ORDER BY %s %s OFFSET $2 LIMIT (CASE WHEN $3 = 0 THEN NULL ELSE $3 END);
@@ -254,7 +254,7 @@ SELECT COUNT(*) AS total FROM subscribers
(CASE WHEN CARDINALITY($1::INT[]) > 0 THEN true ELSE false END)
AND subscriber_lists.subscriber_id = subscribers.id
)
- WHERE subscriber_lists.list_id = ALL($1::INT[]) %s;
+ WHERE (CARDINALITY($1) = 0 OR subscriber_lists.list_id = ANY($1::INT[])) %s;
-- name: query-subscribers-for-export
-- raw: true