Skip to content

Commit b2a5e37

Browse files
committed
Fix incorrect form messages on public re-subscription to double opt-in lists. Closes #1638.
1 parent bce6758 commit b2a5e37

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

cmd/public.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,11 +705,12 @@ func processSubForm(c echo.Context) (bool, error) {
705705
return false, err
706706
}
707707

708-
if _, err := app.core.UpdateSubscriberWithLists(sub.ID, sub, nil, listUUIDs, false, false); err != nil {
708+
_, hasOptin, err := app.core.UpdateSubscriberWithLists(sub.ID, sub, nil, listUUIDs, false, false)
709+
if err != nil {
709710
return false, err
710711
}
711712

712-
return false, nil
713+
return hasOptin, nil
713714
}
714715

715716
return false, echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("%s", err.(*echo.HTTPError).Message))

cmd/subscribers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ func handleUpdateSubscriber(c echo.Context) error {
238238
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("subscribers.invalidName"))
239239
}
240240

241-
out, err := app.core.UpdateSubscriberWithLists(id, req.Subscriber, req.Lists, nil, req.PreconfirmSubs, true)
241+
out, _, err := app.core.UpdateSubscriberWithLists(id, req.Subscriber, req.Lists, nil, req.PreconfirmSubs, true)
242242
if err != nil {
243243
return err
244244
}

internal/core/subscribers.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,12 @@ func (c *Core) InsertSubscriber(sub models.Subscriber, listIDs []int, listUUIDs
272272
pq.Array(listUUIDs),
273273
subStatus); err != nil {
274274
if pqErr, ok := err.(*pq.Error); ok && pqErr.Constraint == "subscribers_email_key" {
275-
return models.Subscriber{}, false, echo.NewHTTPError(http.StatusConflict,
276-
c.i18n.T("subscribers.emailExists"))
275+
return models.Subscriber{}, false, echo.NewHTTPError(http.StatusConflict, c.i18n.T("subscribers.emailExists"))
277276
} else {
278277
// return sub.Subscriber, errSubscriberExists
279278
c.log.Printf("error inserting subscriber: %v", err)
280279
return models.Subscriber{}, false, echo.NewHTTPError(http.StatusInternalServerError,
281-
c.i18n.Ts("globals.messages.errorCreating",
282-
"name", "{globals.terms.subscriber}", "error", pqErrMsg(err)))
280+
c.i18n.Ts("globals.messages.errorCreating", "name", "{globals.terms.subscriber}", "error", pqErrMsg(err)))
283281
}
284282
}
285283

@@ -337,7 +335,7 @@ func (c *Core) UpdateSubscriber(id int, sub models.Subscriber) (models.Subscribe
337335
// UpdateSubscriberWithLists updates a subscriber's properties.
338336
// If deleteLists is set to true, all existing subscriptions are deleted and only
339337
// the ones provided are added or retained.
340-
func (c *Core) UpdateSubscriberWithLists(id int, sub models.Subscriber, listIDs []int, listUUIDs []string, preconfirm, deleteLists bool) (models.Subscriber, error) {
338+
func (c *Core) UpdateSubscriberWithLists(id int, sub models.Subscriber, listIDs []int, listUUIDs []string, preconfirm, deleteLists bool) (models.Subscriber, bool, error) {
341339
subStatus := models.SubscriptionStatusUnconfirmed
342340
if preconfirm {
343341
subStatus = models.SubscriptionStatusConfirmed
@@ -347,7 +345,7 @@ func (c *Core) UpdateSubscriberWithLists(id int, sub models.Subscriber, listIDs
347345
attribs := []byte("{}")
348346
if len(sub.Attribs) > 0 {
349347
if b, err := json.Marshal(sub.Attribs); err != nil {
350-
return models.Subscriber{}, echo.NewHTTPError(http.StatusInternalServerError,
348+
return models.Subscriber{}, false, echo.NewHTTPError(http.StatusInternalServerError,
351349
c.i18n.Ts("globals.messages.errorUpdating",
352350
"name", "{globals.terms.subscriber}", "error", err.Error()))
353351
} else {
@@ -366,21 +364,23 @@ func (c *Core) UpdateSubscriberWithLists(id int, sub models.Subscriber, listIDs
366364
deleteLists)
367365
if err != nil {
368366
c.log.Printf("error updating subscriber: %v", err)
369-
return models.Subscriber{}, echo.NewHTTPError(http.StatusInternalServerError,
367+
return models.Subscriber{}, false, echo.NewHTTPError(http.StatusInternalServerError,
370368
c.i18n.Ts("globals.messages.errorUpdating", "name", "{globals.terms.subscriber}", "error", pqErrMsg(err)))
371369
}
372370

373371
out, err := c.GetSubscriber(sub.ID, "", sub.Email)
374372
if err != nil {
375-
return models.Subscriber{}, err
373+
return models.Subscriber{}, false, err
376374
}
377375

376+
hasOptin := false
378377
if !preconfirm && c.constants.SendOptinConfirmation {
379378
// Send a confirmation e-mail (if there are any double opt-in lists).
380-
c.h.SendOptinConfirmation(out, listIDs)
379+
num, _ := c.h.SendOptinConfirmation(out, listIDs)
380+
hasOptin = num > 0
381381
}
382382

383-
return out, nil
383+
return out, hasOptin, nil
384384
}
385385

386386
// BlocklistSubscribers blocklists the given list of subscribers.

0 commit comments

Comments
 (0)