Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blocklist subscribers based on bounces #2226

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions cmd/bounce.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,41 @@ func handleDeleteBounces(c echo.Context) error {
return c.JSON(http.StatusOK, okResp{true})
}

func handleBlocklistSubscriberBounces(c echo.Context) error {
var (
app = c.Get("app").(*App)
pID = c.Param("id")
all, _ = strconv.ParseBool(c.QueryParam("all"))
IDs = []int{}
)
// Is it an /:id call?
if pID != "" {
id, _ := strconv.Atoi(pID)
if id < 1 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
IDs = append(IDs, id)
} else if !all {
// Multiple IDs.
i, err := parseStringIDs(c.Request().URL.Query()["id"])
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest,
app.i18n.Ts("globals.messages.invalidID", "error", err.Error()))
}

if len(i) == 0 {
return echo.NewHTTPError(http.StatusBadRequest,
app.i18n.Ts("globals.messages.invalidID"))
}
IDs = i
}

if err := app.core.BlocklistSubscriberBounces(IDs); err != nil {
return err
}
return c.JSON(http.StatusOK, okResp{true})
}

// handleBounceWebhook renders the HTML preview of a template.
func handleBounceWebhook(c echo.Context) error {
var (
Expand Down
2 changes: 2 additions & 0 deletions cmd/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ func initHTTPHandlers(e *echo.Echo, app *App) {
api.GET("/api/bounces/:id", pm(handleGetBounces, "bounces:get"))
api.DELETE("/api/bounces", pm(handleDeleteBounces, "bounces:manage"))
api.DELETE("/api/bounces/:id", pm(handleDeleteBounces, "bounces:manage"))
api.PUT("/api/bounces/blocklist", pm(handleBlocklistSubscriberBounces, "bounces:manage"))
api.PUT("/api/bounces/:id/blocklist", pm(handleBlocklistSubscriberBounces, "bounces:manage"))

// Subscriber operations based on arbitrary SQL queries.
// These aren't very REST-like.
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ export const deleteSubscriberBounces = async (id) => http.delete(
{ loading: models.bounces },
);

export const blocklistSubscriberBounce = async (id) => http.put(
`/api/bounces/${id}/blocklist`,
{ loading: models.bounces },
);

export const blocklistSubscriberBounces = async (params) => http.put(
'/api/bounces/blocklist',
{ params, loading: models.bounces },
);

export const deleteBounce = async (id) => http.delete(
`/api/bounces/${id}`,
{ loading: models.bounces },
Expand Down
9 changes: 9 additions & 0 deletions internal/core/bounces.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,12 @@ func (c *Core) DeleteBounces(ids []int) error {
}
return nil
}

func (c *Core) BlocklistSubscriberBounces(ids []int) error {
if _, err := c.q.BlocklistSubscribersByBounces.Exec(pq.Array(ids)); err != nil {
c.log.Printf("error blocklisting subscribers by bounces: %v", err)
return echo.NewHTTPError(http.StatusInternalServerError,
c.i18n.Ts("globals.messages.errorUpdating", "name", "{globals.terms.bounce}", "error", pqErrMsg(err)))
}
return nil
}
11 changes: 6 additions & 5 deletions models/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,12 @@ type Queries struct {
UpdateSettings *sqlx.Stmt `query:"update-settings"`

// GetStats *sqlx.Stmt `query:"get-stats"`
RecordBounce *sqlx.Stmt `query:"record-bounce"`
QueryBounces string `query:"query-bounces"`
DeleteBounces *sqlx.Stmt `query:"delete-bounces"`
DeleteBouncesBySubscriber *sqlx.Stmt `query:"delete-bounces-by-subscriber"`
GetDBInfo string `query:"get-db-info"`
RecordBounce *sqlx.Stmt `query:"record-bounce"`
QueryBounces string `query:"query-bounces"`
DeleteBounces *sqlx.Stmt `query:"delete-bounces"`
DeleteBouncesBySubscriber *sqlx.Stmt `query:"delete-bounces-by-subscriber"`
BlocklistSubscribersByBounces *sqlx.Stmt `query:"blocklist-subscribers-by-bounces"`
GetDBInfo string `query:"get-db-info"`

CreateUser *sqlx.Stmt `query:"create-user"`
UpdateUser *sqlx.Stmt `query:"update-user"`
Expand Down
10 changes: 10 additions & 0 deletions queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,16 @@ WITH sub AS (
)
DELETE FROM bounces WHERE subscriber_id = (SELECT id FROM sub);

--name: blocklist-subscribers-by-bounces
WITH subscriber_ids_to_blocklist AS (
SELECT DISTINCT b.subscriber_id
FROM bounces b
WHERE b.id = ANY($1)
)
UPDATE subscribers
SET status = 'blocklisted'
WHERE id IN (SELECT subscriber_id FROM subscriber_ids_to_blocklist)
RETURNING id, email, status;

-- name: get-db-info
SELECT JSON_BUILD_OBJECT('version', (SELECT VERSION()),
Expand Down
Loading