Fix GET /subscribers calls not accepting multiple list_ids.

Closes #585
This commit is contained in:
Kailash Nadh 2021-11-29 20:38:57 +05:30
parent d32c11a595
commit 3386de40c7
3 changed files with 33 additions and 19 deletions

View file

@ -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
}

View file

@ -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()}`;
});
},

View file

@ -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