Skip to content

Commit

Permalink
Fix GET /subscribers calls not accepting multiple list_ids.
Browse files Browse the repository at this point in the history
Closes #585
  • Loading branch information
knadh committed Nov 29, 2021
1 parent d32c11a commit 3386de4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 19 deletions.
43 changes: 27 additions & 16 deletions cmd/subscribers.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,17 @@ 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")
order = c.FormValue("order")
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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
5 changes: 4 additions & 1 deletion frontend/src/views/Subscribers.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`;
});
},
Expand Down
4 changes: 2 additions & 2 deletions queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
Expand Down

0 comments on commit 3386de4

Please sign in to comment.