Skip to content

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

Fix a number of cosmetic inconsistenies across handlers and functions.

Kailash Nadh007f4de8504b
diff --git a/cmd/admin.go b/cmd/admin.go
index e4207487..e6977a6f 100644
--- a/cmd/admin.go
+++ b/cmd/admin.go
@@ -28,6 +28,7 @@ func handleGetServerConfig(c echo.Context) error {
var (
app = c.Get("app").(*App)
)
+
out := serverConfig{
RootURL: app.constants.RootURL,
FromEmail: app.constants.FromEmail,
@@ -64,6 +65,7 @@ func handleGetDashboardCharts(c echo.Context) error {
app = c.Get("app").(*App)
)
+ // Get the chart data from the DB.
out, err := app.core.GetDashboardCharts()
if err != nil {
return err
@@ -78,6 +80,7 @@ func handleGetDashboardCounts(c echo.Context) error {
app = c.Get("app").(*App)
)
+ // Get the chart data from the DB.
out, err := app.core.GetDashboardCounts()
if err != nil {
return err
@@ -88,10 +91,16 @@ func handleGetDashboardCounts(c echo.Context) error {
// handleReloadApp restarts the app.
func handleReloadApp(c echo.Context) error {
- app := c.Get("app").(*App)
+ var (
+ app = c.Get("app").(*App)
+ )
+
go func() {
<-time.After(time.Millisecond * 500)
+
+ // Send the reload signal to trigger the wait loop in main.
app.chReload <- syscall.SIGHUP
}()
+
return c.JSON(http.StatusOK, okResp{true})
}
diff --git a/cmd/archive.go b/cmd/archive.go
index 173dbf40..c1f9c790 100644
--- a/cmd/archive.go
+++ b/cmd/archive.go
@@ -27,25 +27,28 @@ type campArchive struct {
func handleGetCampaignArchives(c echo.Context) error {
var (
app = c.Get("app").(*App)
- pg = app.paginator.NewFromURL(c.Request().URL.Query())
)
+ // Get archives from the DB.
+ pg := app.paginator.NewFromURL(c.Request().URL.Query())
camps, total, err := getCampaignArchives(pg.Offset, pg.Limit, false, app)
if err != nil {
return err
}
- var out models.PageResults
if len(camps) == 0 {
- out.Results = []campArchive{}
- return c.JSON(http.StatusOK, okResp{out})
+ return c.JSON(http.StatusOK, okResp{models.PageResults{
+ Results: []campArchive{},
+ }})
}
// Meta.
- out.Results = camps
- out.Total = total
- out.Page = pg.Page
- out.PerPage = pg.PerPage
+ out := models.PageResults{
+ Results: camps,
+ Total: total,
+ Page: pg.Page,
+ PerPage: pg.PerPage,
+ }
return c.JSON(200, okResp{out})
}
@@ -53,16 +56,21 @@ func handleGetCampaignArchives(c echo.Context) error {
// handleGetCampaignArchivesFeed renders the public campaign archives RSS feed.
func handleGetCampaignArchivesFeed(c echo.Context) error {
var (
- app = c.Get("app").(*App)
+ app = c.Get("app").(*App)
+ )
+
+ var (
pg = app.paginator.NewFromURL(c.Request().URL.Query())
showFullContent = app.constants.EnablePublicArchiveRSSContent
)
+ // Get archives from the DB.
camps, _, err := getCampaignArchives(pg.Offset, pg.Limit, showFullContent, app)
if err != nil {
return err
}
+ // Format output for the feed.
out := make([]*feeds.Item, 0, len(camps))
for _, c := range camps {
pubDate := c.CreatedAt.Time
@@ -79,6 +87,7 @@ func handleGetCampaignArchivesFeed(c echo.Context) error {
})
}
+ // Generate the feed.
feed := &feeds.Feed{
Title: app.constants.SiteName,
Link: &feeds.Link{Href: app.constants.RootURL},
@@ -98,9 +107,10 @@ func handleGetCampaignArchivesFeed(c echo.Context) error {
func handleCampaignArchivesPage(c echo.Context) error {
var (
app = c.Get("app").(*App)
- pg = app.paginator.NewFromURL(c.Request().URL.Query())
)
+ // Get archives from the DB.
+ pg := app.paginator.NewFromURL(c.Request().URL.Query())
out, total, err := getCampaignArchives(pg.Offset, pg.Limit, false, app)
if err != nil {
return err
@@ -120,50 +130,58 @@ func handleCampaignArchivesPage(c echo.Context) error {
// handleCampaignArchivePage renders the public campaign archives page.
func handleCampaignArchivePage(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- id = c.Param("id")
- uuid = ""
- slug = ""
+ app = c.Get("app").(*App)
)
// ID can be the UUID or slug.
- if reUUID.MatchString(id) {
- uuid = id
+ var (
+ idStr = c.Param("id")
+ uuid, slug string
+ )
+ if reUUID.MatchString(idStr) {
+ uuid = idStr
} else {
- slug = id
+ slug = idStr
}
+ // Get the campaign from the DB.
pubCamp, err := app.core.GetArchivedCampaign(0, uuid, slug)
if err != nil || pubCamp.Type != models.CampaignTypeRegular {
notFound := false
+
+ // Camppaig doesn't exist.
if er, ok := err.(*echo.HTTPError); ok {
if er.Code == http.StatusBadRequest {
notFound = true
}
} else if pubCamp.Type != models.CampaignTypeRegular {
+ // Campaign isn't of regular type.
notFound = true
}
+ // 404.
if notFound {
return c.Render(http.StatusNotFound, tplMessage,
makeMsgTpl(app.i18n.T("public.notFoundTitle"), "", app.i18n.T("public.campaignNotFound")))
}
+ // Some other internal error.
return c.Render(http.StatusInternalServerError, tplMessage,
makeMsgTpl(app.i18n.T("public.errorTitle"), "", app.i18n.Ts("public.errorFetchingCampaign")))
}
+ // "Compile" the campaign template with appropriate data.
out, err := compileArchiveCampaigns([]models.Campaign{pubCamp}, app)
if err != nil {
return c.Render(http.StatusInternalServerError, tplMessage,
makeMsgTpl(app.i18n.T("public.errorTitle"), "", app.i18n.Ts("public.errorFetchingCampaign")))
}
- // Render the message body.
+ // Render the campaign body.
camp := out[0].Campaign
msg, err := app.manager.NewCampaignMessage(camp, out[0].Subscriber)
if err != nil {
- app.log.Printf("error rendering message: %v", err)
+ app.log.Printf("error rendering campaign: %v", err)
return c.Render(http.StatusInternalServerError, tplMessage,
makeMsgTpl(app.i18n.T("public.errorTitle"), "", app.i18n.Ts("public.errorFetchingCampaign")))
}
@@ -177,6 +195,7 @@ func handleCampaignArchivePageLatest(c echo.Context) error {
app = c.Get("app").(*App)
)
+ // Get the latest campaign from the DB.
camps, _, err := getCampaignArchives(0, 1, true, app)
if err != nil {
return err
@@ -186,12 +205,12 @@ func handleCampaignArchivePageLatest(c echo.Context) error {
return c.Render(http.StatusNotFound, tplMessage,
makeMsgTpl(app.i18n.T("public.notFoundTitle"), "", app.i18n.T("public.campaignNotFound")))
}
-
camp := camps[0]
return c.HTML(http.StatusOK, camp.Content)
}
+// getCampaignArchives fetches the public campaign archives from the DB.
func getCampaignArchives(offset, limit int, renderBody bool, app *App) ([]campArchive, int, error) {
pubCamps, total, err := app.core.GetArchivedCampaigns(offset, limit)
if err != nil {
@@ -214,12 +233,14 @@ func getCampaignArchives(offset, limit int, renderBody bool, app *App) ([]campAr
SendAt: camp.SendAt,
}
+ // The campaign may have a custom slug.
if camp.ArchiveSlug.Valid {
archive.URL, _ = url.JoinPath(app.constants.ArchiveURL, camp.ArchiveSlug.String)
} else {
archive.URL, _ = url.JoinPath(app.constants.ArchiveURL, camp.UUID)
}
+ // Render the full template body if requested.
if renderBody {
msg, err := app.manager.NewCampaignMessage(camp, m.Subscriber)
if err != nil {
@@ -234,12 +255,13 @@ func getCampaignArchives(offset, limit int, renderBody bool, app *App) ([]campAr
return out, total, nil
}
+// compileArchiveCampaigns compiles the campaign template with the subscriber data.
func compileArchiveCampaigns(camps []models.Campaign, app *App) ([]manager.CampaignMessage, error) {
+
var (
- b = bytes.Buffer{}
+ b = bytes.Buffer{}
+ out = make([]manager.CampaignMessage, 0, len(camps))
)
-
- out := make([]manager.CampaignMessage, 0, len(camps))
for _, c := range camps {
camp := c
if err := camp.CompileTemplate(app.manager.TemplateFuncs(&camp)); err != nil {
@@ -266,7 +288,6 @@ func compileArchiveCampaigns(camps []models.Campaign, app *App) ([]manager.Campa
}
camp.Subject = b.String()
b.Reset()
-
}
out = append(out, m)
diff --git a/cmd/auth.go b/cmd/auth.go
index dff0d8dd..d128ff9e 100644
--- a/cmd/auth.go
+++ b/cmd/auth.go
@@ -43,7 +43,9 @@ var oidcProviders = map[string]bool{
// handleLoginPage renders the login page and handles the login form.
func handleLoginPage(c echo.Context) error {
- app := c.Get("app").(*App)
+ var (
+ app = c.Get("app").(*App)
+ )
// Has the user been setup?
app.Lock()
@@ -63,18 +65,20 @@ func handleLoginPage(c echo.Context) error {
}
}
+ // Render the page, with or without POST.
return renderLoginPage(c, loginErr)
}
// handleLoginSetupPage renders the first time user login page and handles the login form.
func handleLoginSetupPage(c echo.Context) error {
- app := c.Get("app").(*App)
+ var (
+ app = c.Get("app").(*App)
+ )
// Process POST login request.
var loginErr error
-
if c.Request().Method == http.MethodPost {
- loginErr = doLoginSetup(c)
+ loginErr = doFirstTimeSetup(c)
if loginErr == nil {
app.Lock()
app.needsUserSetup = false
@@ -83,16 +87,14 @@ func handleLoginSetupPage(c echo.Context) error {
}
}
+ // Render the page, with or without POST.
return renderLoginSetupPage(c, loginErr)
}
// handleLogout logs a user out.
func handleLogout(c echo.Context) error {
- var (
- sess = c.Get(auth.SessionKey).(*simplesessions.Session)
- )
-
- // Clear the session.
+ // Delete the session from the DB and cookie.
+ sess := c.Get(auth.SessionKey).(*simplesessions.Session)
_ = sess.Destroy()
return c.JSON(http.StatusOK, okResp{true})
@@ -100,7 +102,9 @@ func handleLogout(c echo.Context) error {
// handleOIDCLogin initializes an OIDC request and redirects to the OIDC provider for login.
func handleOIDCLogin(c echo.Context) error {
- app := c.Get("app").(*App)
+ var (
+ app = c.Get("app").(*App)
+ )
// Verify that the request came from the login page (CSRF).
nonce, err := c.Cookie("nonce")
@@ -108,29 +112,32 @@ func handleOIDCLogin(c echo.Context) error {
return echo.NewHTTPError(http.StatusUnauthorized, app.i18n.T("users.invalidRequest"))
}
+ // Sanitize the URL and make it relative.
next := utils.SanitizeURI(c.FormValue("next"))
if next == "/" {
next = uriAdmin
}
- state := oidcState{
- Nonce: nonce.Value,
- Next: next,
- }
+ // Preparethe OIDC payload to send to the provider.
+ state := oidcState{Nonce: nonce.Value, Next: next}
- stateJSON, err := json.Marshal(state)
+ b, err := json.Marshal(state)
if err != nil {
app.log.Printf("error marshalling OIDC state: %v", err)
return echo.NewHTTPError(http.StatusInternalServerError, app.i18n.T("globals.messages.internalError"))
}
- return c.Redirect(http.StatusFound, app.auth.GetOIDCAuthURL(base64.URLEncoding.EncodeToString(stateJSON), nonce.Value))
+ // Redirect to the external OIDC provider.
+ return c.Redirect(http.StatusFound, app.auth.GetOIDCAuthURL(base64.URLEncoding.EncodeToString(b), nonce.Value))
}
// handleOIDCFinish receives the redirect callback from the OIDC provider and completes the handshake.
func handleOIDCFinish(c echo.Context) error {
- app := c.Get("app").(*App)
+ var (
+ app = c.Get("app").(*App)
+ )
+ // Verify that the request actually originated from the login request (which sets the nonce value).
nonce, err := c.Cookie("nonce")
if err != nil || nonce.Value == "" {
return renderLoginPage(c, echo.NewHTTPError(http.StatusUnauthorized, app.i18n.T("users.invalidRequest")))
@@ -162,7 +169,6 @@ func handleOIDCFinish(c echo.Context) error {
if email == "" {
return renderLoginPage(c, errors.New(app.i18n.Ts("globals.messages.invalidFields", "name", "email")))
}
-
em, err := mail.ParseAddress(email)
if err != nil {
return renderLoginPage(c, err)
@@ -175,34 +181,37 @@ func handleOIDCFinish(c echo.Context) error {
return renderLoginPage(c, err)
}
- // Update user login.
+ // Update the user login state (avatar, logged in date) in the DB.
if err := app.core.UpdateUserLogin(user.ID, claims.Picture); err != nil {
return renderLoginPage(c, err)
}
- // Set the session.
+ // Set the session in the DB and cookie.
if err := app.auth.SaveSession(user, oidcToken, c); err != nil {
return renderLoginPage(c, err)
}
+ // Redirect to the next page.
return c.Redirect(http.StatusFound, utils.SanitizeURI(state.Next))
}
// renderLoginPage renders the login page and handles the login form.
func renderLoginPage(c echo.Context, loginErr error) error {
var (
- app = c.Get("app").(*App)
- next = utils.SanitizeURI(c.FormValue("next"))
+ app = c.Get("app").(*App)
)
+ next := utils.SanitizeURI(c.FormValue("next"))
if next == "/" {
next = uriAdmin
}
- oidcProvider := ""
- oidcProviderLogo := ""
+ var (
+ oidcProvider = ""
+ oidcLogo = ""
+ )
if app.constants.Security.OIDC.Enabled {
- oidcProviderLogo = "oidc.png"
+ oidcLogo = "oidc.png"
u, err := url.Parse(app.constants.Security.OIDC.Provider)
if err == nil {
h := strings.Split(u.Hostname(), ".")
@@ -214,8 +223,9 @@ func renderLoginPage(c echo.Context, loginErr error) error {
oidcProvider = u.Hostname()
}
+ // Lookup the logo in the known providers map.
if _, ok := oidcProviders[oidcProvider]; ok {
- oidcProviderLogo = oidcProvider + ".png"
+ oidcLogo = oidcProvider + ".png"
}
}
}
@@ -224,10 +234,11 @@ func renderLoginPage(c echo.Context, loginErr error) error {
Title: app.i18n.T("users.login"),
PasswordEnabled: true,
OIDCProvider: oidcProvider,
- OIDCProviderLogo: oidcProviderLogo,
+ OIDCProviderLogo: oidcLogo,
NextURI: next,
}
+ // If there was an error in the previous state (POST reqest), set it to render in the template.
if loginErr != nil {
if e, ok := loginErr.(*echo.HTTPError); ok {
out.Error = e.Message.(string)
@@ -236,7 +247,7 @@ func renderLoginPage(c echo.Context, loginErr error) error {
}
}
- // Generate and set a nonce for preventing CSRF requests.
+ // Generate and set a nonce for preventing CSRF requests that will be valided in the subsequent requests.
nonce, err := utils.GenerateRandomString(16)
if err != nil {
app.log.Printf("error generating OIDC nonce: %v", err)
@@ -251,16 +262,17 @@ func renderLoginPage(c echo.Context, loginErr error) error {
})
out.Nonce = nonce
+ // Render the login page.
return c.Render(http.StatusOK, "admin-login", out)
}
// renderLoginSetupPage renders the first time user setup page.
func renderLoginSetupPage(c echo.Context, loginErr error) error {
var (
- app = c.Get("app").(*App)
- next = utils.SanitizeURI(c.FormValue("next"))
+ app = c.Get("app").(*App)
)
+ next := utils.SanitizeURI(c.FormValue("next"))
if next == "/" {
next = uriAdmin
}
@@ -271,6 +283,7 @@ func renderLoginSetupPage(c echo.Context, loginErr error) error {
NextURI: next,
}
+ // If there was an error in the previous state (POST reqest), set it to render in the template.
if loginErr != nil {
if e, ok := loginErr.(*echo.HTTPError); ok {
out.Error = e.Message.(string)
@@ -288,12 +301,6 @@ func doLogin(c echo.Context) error {
app = c.Get("app").(*App)
)
- // Verify that the request came from the login page (CSRF).
- // nonce, err := c.Cookie("nonce")
- // if err != nil || nonce.Value == "" || nonce.Value != c.FormValue("nonce") {
- // return echo.NewHTTPError(http.StatusUnauthorized, app.i18n.T("users.invalidRequest"))
- // }
-
var (
username = strings.TrimSpace(c.FormValue("username"))
password = strings.TrimSpace(c.FormValue("password"))
@@ -302,24 +309,22 @@ func doLogin(c echo.Context) error {
if !strHasLen(username, 3, stdInputMaxLen) {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.Ts("globals.messages.invalidFields", "name", "username"))
}
-
if !strHasLen(password, 8, stdInputMaxLen) {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.Ts("globals.messages.invalidFields", "name", "password"))
}
- start := time.Now()
-
+ // Log the user in by fetching and verifying credentials from the DB.
user, err := app.core.LoginUser(username, password)
if err != nil {
return err
}
// Resist potential constant-time-comparison attacks with a min response time.
- if ms := time.Since(start).Milliseconds(); ms < 100 {
+ if ms := time.Since(time.Now()).Milliseconds(); ms < 100 {
time.Sleep(time.Duration(ms))
}
- // Set the session.
+ // Set the session in the DB and cookie.
if err := app.auth.SaveSession(user, "", c); err != nil {
return err
}
@@ -327,25 +332,18 @@ func doLogin(c echo.Context) error {
return nil
}
-// doLoginSetup sets a user up for the first time.
-func doLoginSetup(c echo.Context) error {
+// doFirstTimeSetup sets a user up for the first time.
+func doFirstTimeSetup(c echo.Context) error {
var (
app = c.Get("app").(*App)
)
- // Verify that the request came from the login page (CSRF).
- // nonce, err := c.Cookie("nonce")
- // if err != nil || nonce.Value == "" || nonce.Value != c.FormValue("nonce") {
- // return echo.NewHTTPError(http.StatusUnauthorized, app.i18n.T("users.invalidRequest"))
- // }
-
var (
email = strings.TrimSpace(c.FormValue("email"))
username = strings.TrimSpace(c.FormValue("username"))
password = strings.TrimSpace(c.FormValue("password"))
password2 = strings.TrimSpace(c.FormValue("password2"))
)
-
if !utils.ValidateEmail(email) {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.Ts("globals.messages.invalidFields", "name", "email"))
}
@@ -359,7 +357,7 @@ func doLoginSetup(c echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("users.passwordMismatch"))
}
- // Create the default "Super Admin".
+ // Create the default "Super Admin" with all permission.
r := auth.Role{
Type: auth.RoleTypeUser,
Name: null.NewString("Super Admin", true),
@@ -367,12 +365,14 @@ func doLoginSetup(c echo.Context) error {
for p := range app.constants.Permissions {
r.Permissions = append(r.Permissions, p)
}
+
+ // Create the role in the DB.
role, err := app.core.CreateRole(r)
if err != nil {
return err
}
- // Create the super admin user.
+ // Create the super admin user in the DB.
u := auth.User{
Type: auth.UserTypeUser,
HasPassword: true,
@@ -388,13 +388,13 @@ func doLoginSetup(c echo.Context) error {
return err
}
- // Log the user in.
+ // Log the user in directly.
user, err := app.core.LoginUser(username, password)
if err != nil {
return err
}
- // Set the session.
+ // Set the session in the DB and cookie.
if err := app.auth.SaveSession(user, "", c); err != nil {
return err
}
diff --git a/cmd/bounce.go b/cmd/bounce.go
index 6afcc132..ff4deebd 100644
--- a/cmd/bounce.go
+++ b/cmd/bounce.go
@@ -15,41 +15,43 @@ import (
func handleGetBounces(c echo.Context) error {
var (
app = c.Get("app").(*App)
- pg = app.paginator.NewFromURL(c.Request().URL.Query())
-
- id, _ = strconv.Atoi(c.Param("id"))
- campID, _ = strconv.Atoi(c.QueryParam("campaign_id"))
- source = c.FormValue("source")
- orderBy = c.FormValue("order_by")
- order = c.FormValue("order")
)
- // Fetch one bounce.
+ // Fetch one bounce from the DB.
+ id, _ := strconv.Atoi(c.Param("id"))
if id > 0 {
out, err := app.core.GetBounce(id)
if err != nil {
return err
}
+
return c.JSON(http.StatusOK, okResp{out})
}
+ // Query and fetch bounces from the DB.
+ var (
+ pg = app.paginator.NewFromURL(c.Request().URL.Query())
+ campID, _ = strconv.Atoi(c.QueryParam("campaign_id"))
+ source = c.FormValue("source")
+ orderBy = c.FormValue("order_by")
+ order = c.FormValue("order")
+ )
res, total, err := app.core.QueryBounces(campID, 0, source, orderBy, order, pg.Offset, pg.Limit)
if err != nil {
return err
}
// No results.
- var out models.PageResults
if len(res) == 0 {
- out.Results = []models.Bounce{}
- return c.JSON(http.StatusOK, okResp{out})
+ return c.JSON(http.StatusOK, okResp{models.PageResults{Results: []models.Bounce{}}})
}
- // Meta.
- out.Results = res
- out.Total = total
- out.Page = pg.Page
- out.PerPage = pg.PerPage
+ out := models.PageResults{
+ Results: res,
+ Total: total,
+ Page: pg.Page,
+ PerPage: pg.PerPage,
+ }
return c.JSON(http.StatusOK, okResp{out})
}
@@ -57,14 +59,15 @@ func handleGetBounces(c echo.Context) error {
// handleGetSubscriberBounces retrieves a subscriber's bounce records.
func handleGetSubscriberBounces(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- subID, _ = strconv.Atoi(c.Param("id"))
+ app = c.Get("app").(*App)
)
+ subID, _ := strconv.Atoi(c.Param("id"))
if subID < 1 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
+ // Query and fetch bounces from the DB.
out, _, err := app.core.QueryBounces(0, subID, "", "", "", 0, 1000)
if err != nil {
return err
@@ -76,35 +79,37 @@ func handleGetSubscriberBounces(c echo.Context) error {
// handleDeleteBounces handles bounce deletion, either a single one (ID in the URI), or a list.
func handleDeleteBounces(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- pID = c.Param("id")
- all, _ = strconv.ParseBool(c.QueryParam("all"))
- IDs = []int{}
+ app = c.Get("app").(*App)
)
// Is it an /:id call?
- if pID != "" {
- id, _ := strconv.Atoi(pID)
+ var (
+ all, _ = strconv.ParseBool(c.QueryParam("all"))
+ idStr = c.Param("id")
+
+ ids = []int{}
+ )
+ if idStr != "" {
+ id, _ := strconv.Atoi(idStr)
if id < 1 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
- IDs = append(IDs, id)
+ ids = append(ids, id)
} else if !all {
- // Multiple IDs.
+ // There are multiple IDs in the query string.
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()))
+ 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"))
+ return echo.NewHTTPError(http.StatusBadRequest, app.i18n.Ts("globals.messages.invalidID"))
}
- IDs = i
+ ids = i
}
- if err := app.core.DeleteBounces(IDs); err != nil {
+ // Delete bounces from the DB.
+ if err := app.core.DeleteBounces(ids); err != nil {
return err
}
@@ -114,10 +119,7 @@ func handleDeleteBounces(c echo.Context) error {
// handleBounceWebhook renders the HTML preview of a template.
func handleBounceWebhook(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- service = c.Param("service")
-
- bounces []models.Bounce
+ app = c.Get("app").(*App)
)
// Read the request body instead of using c.Bind() to read to save the entire raw request as meta.
@@ -127,6 +129,11 @@ func handleBounceWebhook(c echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.Ts("globals.messages.internalError"))
}
+ var (
+ service = c.Param("service")
+
+ bounces []models.Bounce
+ )
switch true {
// Native internal webhook.
case service == "":
@@ -224,7 +231,7 @@ func handleBounceWebhook(c echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.Ts("bounces.unknownService"))
}
- // Record bounces if any.
+ // Insert bounces into the DB.
for _, b := range bounces {
if err := app.bounce.Record(b); err != nil {
app.log.Printf("error recording bounce: %v", err)
diff --git a/cmd/campaigns.go b/cmd/campaigns.go
index 1342dcc4..6f0f6102 100644
--- a/cmd/campaigns.go
+++ b/cmd/campaigns.go
@@ -20,9 +20,9 @@ import (
"gopkg.in/volatiletech/null.v6"
)
-// campaignReq is a wrapper over the Campaign model for receiving
+// campReq is a wrapper over the Campaign model for receiving
// campaign creation and update data from APIs.
-type campaignReq struct {
+type campReq struct {
models.Campaign
// This overrides Campaign.Lists to receive and
@@ -37,9 +37,9 @@ type campaignReq struct {
SubscriberEmails pq.StringArray `json:"subscribers"`
}
-// campaignContentReq wraps params coming from API requests for converting
+// campContentReq wraps params coming from API requests for converting
// campaign content formats.
-type campaignContentReq struct {
+type campContentReq struct {
models.Campaign
From string `json:"from"`
To string `json:"to"`
@@ -55,14 +55,6 @@ func handleGetCampaigns(c echo.Context) error {
var (
app = c.Get("app").(*App)
user = auth.GetUser(c)
- pg = app.paginator.NewFromURL(c.Request().URL.Query())
-
- status = c.QueryParams()["status"]
- tags = c.QueryParams()["tag"]
- query = strings.TrimSpace(c.FormValue("query"))
- orderBy = c.FormValue("order_by")
- order = c.FormValue("order")
- noBody, _ = strconv.ParseBool(c.QueryParam("no_body"))
)
var (
@@ -76,7 +68,18 @@ func handleGetCampaigns(c echo.Context) error {
hasAllPerm, permittedLists = user.GetPermittedLists(auth.PermTypeGet | auth.PermTypeManage)
}
- // Query and retrieve the campaigns.
+ var (
+ pg = app.paginator.NewFromURL(c.Request().URL.Query())
+
+ status = c.QueryParams()["status"]
+ tags = c.QueryParams()["tag"]
+ query = strings.TrimSpace(c.FormValue("query"))
+ orderBy = c.FormValue("order_by")
+ order = c.FormValue("order")
+ noBody, _ = strconv.ParseBool(c.QueryParam("no_body"))
+ )
+
+ // Query and retrieve campaigns from the DB.
res, total, err := app.core.QueryCampaigns(query, status, tags, orderBy, order, hasAllPerm, permittedLists, pg.Offset, pg.Limit)
if err != nil {
return err
@@ -90,18 +93,17 @@ func handleGetCampaigns(c echo.Context) error {
}
// Paginate the response.
- var out models.PageResults
if len(res) == 0 {
- out.Results = []models.Campaign{}
- return c.JSON(http.StatusOK, okResp{out})
+ return c.JSON(http.StatusOK, okResp{models.PageResults{Results: []models.Campaign{}}})
}
- // Meta.
- out.Query = query
- out.Results = res
- out.Total = total
- out.Page = pg.Page
- out.PerPage = pg.PerPage
+ out := models.PageResults{
+ Query: query,
+ Results: res,
+ Total: total,
+ Page: pg.Page,
+ PerPage: pg.PerPage,
+ }
return c.JSON(http.StatusOK, okResp{out})
}
@@ -110,11 +112,9 @@ func handleGetCampaigns(c echo.Context) error {
func handleGetCampaign(c echo.Context) error {
var (
app = c.Get("app").(*App)
-
- id, _ = strconv.Atoi(c.Param("id"))
- noBody, _ = strconv.ParseBool(c.QueryParam("no_body"))
)
+ id, _ := strconv.Atoi(c.Param("id"))
if id < 1 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
@@ -130,6 +130,8 @@ func handleGetCampaign(c echo.Context) error {
return err
}
+ // Blank out the body if requested.
+ noBody, _ := strconv.ParseBool(c.QueryParam("no_body"))
if noBody {
out.Body = ""
}
@@ -141,11 +143,9 @@ func handleGetCampaign(c echo.Context) error {
func handlePreviewCampaign(c echo.Context) error {
var (
app = c.Get("app").(*App)
-
- id, _ = strconv.Atoi(c.Param("id"))
- tplID, _ = strconv.Atoi(c.FormValue("template_id"))
)
+ id, _ := strconv.Atoi(c.Param("id"))
if id < 1 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
@@ -156,6 +156,7 @@ func handlePreviewCampaign(c echo.Context) error {
}
// Fetch the campaign body from the DB.
+ tplID, _ := strconv.Atoi(c.FormValue("template_id"))
camp, err := app.core.GetCampaignForPreview(id, tplID)
if err != nil {
return err
@@ -194,15 +195,15 @@ func handlePreviewCampaign(c echo.Context) error {
// handleCampaignContent handles campaign content (body) format conversions.
func handleCampaignContent(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- id, _ = strconv.Atoi(c.Param("id"))
+ app = c.Get("app").(*App)
)
+ id, _ := strconv.Atoi(c.Param("id"))
if id < 1 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
- var camp campaignContentReq
+ var camp campContentReq
if err := c.Bind(&camp); err != nil {
return err
}
@@ -220,7 +221,7 @@ func handleCampaignContent(c echo.Context) error {
func handleCreateCampaign(c echo.Context) error {
var (
app = c.Get("app").(*App)
- o campaignReq
+ o campReq
)
if err := c.Bind(&o); err != nil {
@@ -295,7 +296,7 @@ func handleUpdateCampaign(c echo.Context) error {
// Read the incoming params into the existing campaign fields from the DB.
// This allows updating of values that have been sent whereas fields
// that are not in the request retain the old values.
- o := campaignReq{Campaign: cm}
+ o := campReq{Campaign: cm}
if err := c.Bind(&o); err != nil {
return err
}
@@ -419,6 +420,7 @@ func handleGetRunningCampaignStats(c echo.Context) error {
app = c.Get("app").(*App)
)
+ // Get the running campaign stats from the DB.
out, err := app.core.GetRunningCampaignStats()
if err != nil {
return err
@@ -431,10 +433,7 @@ func handleGetRunningCampaignStats(c echo.Context) error {
// Compute rate.
for i, c := range out {
if c.Started.Valid && c.UpdatedAt.Valid {
- diff := int(c.UpdatedAt.Time.Sub(c.Started.Time).Minutes())
- if diff < 1 {
- diff = 1
- }
+ diff := max(int(c.UpdatedAt.Time.Sub(c.Started.Time).Minutes()), 1)
rate := c.Sent / diff
if rate > c.Sent || rate > c.ToSend {
@@ -457,11 +456,9 @@ func handleGetRunningCampaignStats(c echo.Context) error {
func handleTestCampaign(c echo.Context) error {
var (
app = c.Get("app").(*App)
-
- id, _ = strconv.Atoi(c.Param("id"))
- tplID, _ = strconv.Atoi(c.FormValue("template_id"))
)
+ id, _ := strconv.Atoi(c.Param("id"))
if id < 1 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.errorID"))
}
@@ -472,7 +469,7 @@ func handleTestCampaign(c echo.Context) error {
}
// Get and validate fields.
- var req campaignReq
+ var req campReq
if err := c.Bind(&req); err != nil {
return err
}
@@ -487,17 +484,19 @@ func handleTestCampaign(c echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("campaigns.noSubsToTest"))
}
- // Get the subscribers.
- for i := 0; i < len(req.SubscriberEmails); i++ {
+ // Sanitize subscriber e-mails.
+ for i := range req.SubscriberEmails {
req.SubscriberEmails[i] = strings.ToLower(strings.TrimSpace(req.SubscriberEmails[i]))
}
+ // Get the subscribers from the DB by their e-mails.
subs, err := app.core.GetSubscribersByEmail(req.SubscriberEmails)
if err != nil {
return err
}
- // The campaign.
+ // Get the campaign from the DB for previewing.
+ tplID, _ := strconv.Atoi(c.FormValue("template_id"))
camp, err := app.core.GetCampaignForPreview(id, tplID)
if err != nil {
return err
@@ -522,8 +521,8 @@ func handleTestCampaign(c echo.Context) error {
// Send the test messages.
for _, s := range subs {
sub := s
- c := camp
- if err := sendTestMessage(sub, &c, app); err != nil {
+
+ if err := sendTestMessage(sub, &camp, app); err != nil {
app.log.Printf("error sending test message: %v", err)
return echo.NewHTTPError(http.StatusInternalServerError,
app.i18n.Ts("campaigns.errorSendTest", "error", err.Error()))
@@ -537,10 +536,6 @@ func handleTestCampaign(c echo.Context) error {
func handleGetCampaignViewAnalytics(c echo.Context) error {
var (
app = c.Get("app").(*App)
-
- typ = c.Param("type")
- from = c.QueryParams().Get("from")
- to = c.QueryParams().Get("to")
)
ids, err := parseStringIDs(c.Request().URL.Query()["id"])
@@ -554,6 +549,11 @@ func handleGetCampaignViewAnalytics(c echo.Context) error {
app.i18n.Ts("globals.messages.missingFields", "name", "`id`"))
}
+ var (
+ typ = c.Param("type")
+ from = c.QueryParams().Get("from")
+ to = c.QueryParams().Get("to")
+ )
if !strHasLen(from, 10, 30) || !strHasLen(to, 10, 30) {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("analytics.invalidDates"))
}
@@ -568,7 +568,7 @@ func handleGetCampaignViewAnalytics(c echo.Context) error {
return c.JSON(http.StatusOK, okResp{out})
}
- // View, click, bounce stats.
+ // Get the analytics numbers from the DB for the campaigns.
out, err := app.core.GetCampaignAnalyticsCounts(ids, typ, from, to)
if err != nil {
return err
@@ -589,15 +589,14 @@ func sendTestMessage(sub models.Subscriber, camp *models.Campaign, app *App) err
msg, err := app.manager.NewCampaignMessage(camp, sub)
if err != nil {
app.log.Printf("error rendering message: %v", err)
- return echo.NewHTTPError(http.StatusNotFound,
- app.i18n.Ts("templates.errorRendering", "error", err.Error()))
+ return echo.NewHTTPError(http.StatusNotFound, app.i18n.Ts("templates.errorRendering", "error", err.Error()))
}
return app.manager.PushCampaignMessage(msg)
}
// validateCampaignFields validates incoming campaign field values.
-func validateCampaignFields(c campaignReq, app *App) (campaignReq, error) {
+func validateCampaignFields(c campReq, app *App) (campReq, error) {
if c.FromEmail == "" {
c.FromEmail = app.constants.FromEmail
} else if !regexFromAddress.Match([]byte(c.FromEmail)) {
@@ -667,18 +666,18 @@ func canEditCampaign(status string) bool {
}
// makeOptinCampaignMessage makes a default opt-in campaign message body.
-func makeOptinCampaignMessage(o campaignReq, app *App) (campaignReq, error) {
+func makeOptinCampaignMessage(o campReq, app *App) (campReq, error) {
if len(o.ListIDs) == 0 {
return o, echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("campaigns.fieldInvalidListIDs"))
}
- // Fetch double opt-in lists from the given list IDs.
+ // Fetch double opt-in lists from the given list IDs from the DB.
lists, err := app.core.GetListsByOptin(o.ListIDs, models.ListOptinDouble)
if err != nil {
return o, err
}
- // No opt-in lists.
+ // There are no double opt-in lists.
if len(lists) == 0 {
return o, echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("campaigns.noOptinLists"))
}
diff --git a/cmd/handlers.go b/cmd/handlers.go
index cb4ecd00..71c40a4c 100644
--- a/cmd/handlers.go
+++ b/cmd/handlers.go
@@ -21,7 +21,7 @@ const (
)
type okResp struct {
- Data interface{} `json:"data"`
+ Data any `json:"data"`
}
var (
diff --git a/cmd/i18n.go b/cmd/i18n.go
index 261c79ea..82e57a37 100644
--- a/cmd/i18n.go
+++ b/cmd/i18n.go
@@ -26,7 +26,9 @@ var reLangCode = regexp.MustCompile(`[^a-zA-Z_0-9\\-]`)
// handleGetI18nLang returns the JSON language pack given the language code.
func handleGetI18nLang(c echo.Context) error {
- app := c.Get("app").(*App)
+ var (
+ app = c.Get("app").(*App)
+ )
lang := c.Param("lang")
if len(lang) > 6 || reLangCode.MatchString(lang) {
@@ -48,6 +50,7 @@ func getI18nLangList(app *App) ([]i18nLang, error) {
return nil, err
}
+ // Read language JSON files from the fs.
var out []i18nLang
for _, l := range list {
b, err := app.fs.Get(l)
@@ -63,6 +66,7 @@ func getI18nLangList(app *App) ([]i18nLang, error) {
out = append(out, i18nLang(r))
}
+ // Sort by language code.
sort.SliceStable(out, func(i, j int) bool {
return out[i].Code < out[j].Code
})
@@ -91,7 +95,6 @@ func getI18nLang(lang string, fs stuffbin.FileSystem) (*i18n.I18n, bool, error)
if err != nil {
return i, true, fmt.Errorf("error reading i18n language file: %s: %v", lang, err)
}
-
if err := i.Load(b); err != nil {
return i, true, fmt.Errorf("error loading i18n language file: %s: %v", lang, err)
}
diff --git a/cmd/import.go b/cmd/import.go
index f4ae7eca..60d08132 100644
--- a/cmd/import.go
+++ b/cmd/import.go
@@ -15,7 +15,9 @@ import (
// handleImportSubscribers handles the uploading and bulk importing of
// a ZIP file of one or more CSV files.
func handleImportSubscribers(c echo.Context) error {
- app := c.Get("app").(*App)
+ var (
+ app = c.Get("app").(*App)
+ )
// Is an import already running?
if app.importer.GetStats().Status == subimporter.StatusImporting {
@@ -54,6 +56,7 @@ func handleImportSubscribers(c echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("import.invalidDelim"))
}
+ // Open the HTTP file.
file, err := c.FormFile("file")
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest,
@@ -66,6 +69,7 @@ func handleImportSubscribers(c echo.Context) error {
}
defer src.Close()
+ // Copy it to a temp location.
out, err := os.CreateTemp("", "listmonk")
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError,
@@ -80,15 +84,15 @@ func handleImportSubscribers(c echo.Context) error {
// Start the importer session.
opt.Filename = file.Filename
- impSess, err := app.importer.NewSession(opt)
+ sess, err := app.importer.NewSession(opt)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError,
app.i18n.Ts("import.errorStarting", "error", err.Error()))
}
- go impSess.Start()
+ go sess.Start()
if strings.HasSuffix(strings.ToLower(file.Filename), ".csv") {
- go impSess.LoadCSV(out.Name(), rune(opt.Delim[0]))
+ go sess.LoadCSV(out.Name(), rune(opt.Delim[0]))
} else {
// Only 1 CSV from the ZIP is considered. If multiple files have
// to be processed, counting the net number of lines (to track progress),
@@ -96,12 +100,13 @@ func handleImportSubscribers(c echo.Context) error {
// multiple files becomes complex. Instead, it's just easier for the
// end user to concat multiple CSVs (if there are multiple in the first)
// place and upload as one in the first place.
- dir, files, err := impSess.ExtractZIP(out.Name(), 1)
+ dir, files, err := sess.ExtractZIP(out.Name(), 1)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError,
app.i18n.Ts("import.errorProcessingZIP", "error", err.Error()))
}
- go impSess.LoadCSV(dir+"/"+files[0], rune(opt.Delim[0]))
+
+ go sess.LoadCSV(dir+"/"+files[0], rune(opt.Delim[0]))
}
return c.JSON(http.StatusOK, okResp{app.importer.GetStats()})
@@ -111,14 +116,18 @@ func handleImportSubscribers(c echo.Context) error {
func handleGetImportSubscribers(c echo.Context) error {
var (
app = c.Get("app").(*App)
- s = app.importer.GetStats()
)
+
+ s := app.importer.GetStats()
return c.JSON(http.StatusOK, okResp{s})
}
// handleGetImportSubscriberStats returns import statistics.
func handleGetImportSubscriberStats(c echo.Context) error {
- app := c.Get("app").(*App)
+ var (
+ app = c.Get("app").(*App)
+ )
+
return c.JSON(http.StatusOK, okResp{string(app.importer.GetLogs())})
}
@@ -126,7 +135,10 @@ func handleGetImportSubscriberStats(c echo.Context) error {
// If there's an ongoing import, it'll be stopped, and if an import
// is finished, it's state is cleared.
func handleStopImportSubscribers(c echo.Context) error {
- app := c.Get("app").(*App)
+ var (
+ app = c.Get("app").(*App)
+ )
+
app.importer.Stop()
return c.JSON(http.StatusOK, okResp{app.importer.GetStats()})
}
diff --git a/cmd/init.go b/cmd/init.go
index 7708518c..b426c48c 100644
--- a/cmd/init.go
+++ b/cmd/init.go
@@ -8,6 +8,8 @@ import (
"errors"
"fmt"
"html/template"
+ "log"
+ "maps"
"net/http"
"os"
"path"
@@ -23,7 +25,7 @@ import (
"github.com/jmoiron/sqlx/types"
"github.com/knadh/goyesql/v2"
goyesqlx "github.com/knadh/goyesql/v2/sqlx"
- "github.com/knadh/koanf/maps"
+ koanfmaps "github.com/knadh/koanf/maps"
"github.com/knadh/koanf/parsers/toml"
"github.com/knadh/koanf/providers/confmap"
"github.com/knadh/koanf/providers/file"
@@ -130,7 +132,8 @@ type notifTpls struct {
contentType string
}
-func initFlags() {
+// initFlags initializes the commandline flags into the Koanf instance.
+func initFlags(ko *koanf.Koanf) {
f := flag.NewFlagSet("config", flag.ContinueOnError)
f.Usage = func() {
// Register --help handler.
@@ -378,7 +381,7 @@ func initSettings(query string, db *sqlx.DB, ko *koanf.Koanf) {
// Setting keys are dot separated, eg: app.favicon_url. Unflatten them into
// nested maps {app: {favicon_url}}.
- var out map[string]interface{}
+ var out map[string]any
if err := json.Unmarshal(s, &out); err != nil {
lo.Fatalf("error unmarshalling settings from DB: %v", err)
}
@@ -387,7 +390,8 @@ func initSettings(query string, db *sqlx.DB, ko *koanf.Koanf) {
}
}
-func initConstants() *constants {
+// initConstants initializes the app's global constants from the given koanf instance.
+func initConstants(ko *koanf.Koanf) *constants {
// Read constants.
var c constants
if err := ko.Unmarshal("app", &c); err != nil {
@@ -407,7 +411,7 @@ func initConstants() *constants {
c.RootURL = strings.TrimRight(c.RootURL, "/")
c.LoginURL = path.Join(uriAdmin, "/login")
c.Lang = ko.String("app.lang")
- c.Privacy.Exportable = maps.StringSliceToLookupMap(ko.Strings("privacy.exportable"))
+ c.Privacy.Exportable = koanfmaps.StringSliceToLookupMap(ko.Strings("privacy.exportable"))
c.MediaUpload.Provider = ko.String("upload.provider")
c.MediaUpload.Extensions = ko.Strings("upload.extensions")
c.Privacy.DomainBlocklist = ko.Strings("privacy.domain_blocklist")
@@ -485,7 +489,7 @@ func initI18n(lang string, fs stuffbin.FileSystem) *i18n.I18n {
// initCampaignManager initializes the campaign manager.
func initCampaignManager(q *models.Queries, cs *constants, app *App) *manager.Manager {
- campNotifCB := func(subject string, data interface{}) error {
+ campNotifCB := func(subject string, data any) error {
return app.sendNotification(cs.NotifyEmails, subject, notifTplCampaign, data, nil)
}
@@ -516,6 +520,7 @@ func initCampaignManager(q *models.Queries, cs *constants, app *App) *manager.Ma
}, newManagerStore(q, app.core, app.media), campNotifCB, app.i18n, lo)
}
+// initTxTemplates initializes and compiles the transactional templates and caches them in-memory.
func initTxTemplates(m *manager.Manager, app *App) {
tpls, err := app.core.GetTemplates(models.TemplateTypeTx, false)
if err != nil {
@@ -541,10 +546,14 @@ func initImporter(q *models.Queries, db *sqlx.DB, core *core.Core, app *App) *su
UpsertStmt: q.UpsertSubscriber.Stmt,
BlocklistStmt: q.UpsertBlocklistSubscriber.Stmt,
UpdateListDateStmt: q.UpdateListsDate.Stmt,
- NotifCB: func(subject string, data interface{}) error {
+
+ // Hook for triggering admin notifications and refreshing stats materialized
+ // views after a successful import.
+ NotifCB: func(subject string, data any) error {
// Refresh cached subscriber counts and stats.
core.RefreshMatViews(true)
+ // Send admin notification.
app.sendNotification(app.constants.NotifyEmails, subject, notifTplImport, data, nil)
return nil
},
@@ -603,7 +612,7 @@ func initSMTPMessengers() []manager.Messenger {
// initPostbackMessengers initializes and returns all the enabled
// HTTP postback messenger backends.
-func initPostbackMessengers() []manager.Messenger {
+func initPostbackMessengers(ko *koanf.Koanf) []manager.Messenger {
items := ko.Slices("messengers")
if len(items) == 0 {
return nil
@@ -638,7 +647,7 @@ func initPostbackMessengers() []manager.Messenger {
}
// initMediaStore initializes Upload manager with a custom backend.
-func initMediaStore() media.Store {
+func initMediaStore(ko *koanf.Koanf) media.Store {
switch provider := ko.String("upload.provider"); provider {
case "s3":
var o s3.Opt
@@ -708,7 +717,7 @@ func initNotifTemplates(fs stuffbin.FileSystem, i *i18n.I18n, cs *constants) *no
// initBounceManager initializes the bounce manager that scans mailboxes and listens to webhooks
// for incoming bounce events.
-func initBounceManager(app *App) *bounce.Manager {
+func initBounceManager(cb func(models.Bounce) error, stmt *sqlx.Stmt, lo *log.Logger, ko *koanf.Koanf) *bounce.Manager {
opt := bounce.Opt{
WebhooksEnabled: ko.Bool("bounce.webhooks_enabled"),
SESEnabled: ko.Bool("bounce.ses_enabled"),
@@ -730,7 +739,7 @@ func initBounceManager(app *App) *bounce.Manager {
ko.Bool("bounce.forwardemail.enabled"),
ko.String("bounce.forwardemail.key"),
},
- RecordBounceCB: app.core.RecordBounce,
+ RecordBounceCB: cb,
}
// For now, only one mailbox is supported.
@@ -750,9 +759,8 @@ func initBounceManager(app *App) *bounce.Manager {
break
}
- b, err := bounce.New(opt, &bounce.Queries{
- RecordQuery: app.queries.RecordBounce,
- }, app.log)
+ // Initialize the bounce manager.
+ b, err := bounce.New(opt, &bounce.Queries{RecordQuery: stmt}, lo)
if err != nil {
lo.Fatalf("error initializing bounce manager: %v", err)
}
@@ -760,6 +768,7 @@ func initBounceManager(app *App) *bounce.Manager {
return b
}
+// initAbout initializes the app's /about API endpoint with the app and system info.
func initAbout(q *models.Queries, db *sqlx.DB) about {
var (
mem runtime.MemStats
@@ -857,12 +866,15 @@ func initHTTPServer(app *App) *echo.Echo {
return srv
}
+// initCaptcha initializes the captcha service.
func initCaptcha() *captcha.Captcha {
return captcha.New(captcha.Opt{
CaptchaSecret: ko.String("security.captcha_secret"),
})
}
+// initCron initializes the cron job for refreshing slow query cache.
+
func initCron(core *core.Core) {
c := cron.New()
_, err := c.Add(ko.MustString("app.cache_slow_queries_interval"), func() {
@@ -879,6 +891,7 @@ func initCron(core *core.Core) {
lo.Printf("IMPORTANT: database slow query caching is enabled. Aggregate numbers and stats will not be realtime. Next refresh at: %v", c.Entries()[0].Next)
}
+// awaitReload waits for a SIGHUP signal to reload the app. Every setting change on the UI causes a reload.
func awaitReload(sigChan chan os.Signal, closerWait chan bool, closer func()) chan bool {
// The blocking signal handler that main() waits on.
out := make(chan bool)
@@ -911,6 +924,7 @@ func awaitReload(sigChan chan os.Signal, closerWait chan bool, closer func()) ch
return out
}
+// joinFSPaths joins the given paths with the root path and returns the full paths.
func joinFSPaths(root string, paths []string) []string {
out := make([]string, 0, len(paths))
for _, p := range paths {
@@ -923,6 +937,8 @@ func joinFSPaths(root string, paths []string) []string {
return out
}
+// initTplFuncs returns a generic template func map with custom template
+// functions and sprig template functions.
func initTplFuncs(i *i18n.I18n, cs *constants) template.FuncMap {
funcs := template.FuncMap{
"RootURL": func() string {
@@ -945,16 +961,17 @@ func initTplFuncs(i *i18n.I18n, cs *constants) template.FuncMap {
},
}
- for k, v := range sprig.GenericFuncMap() {
- funcs[k] = v
- }
+ // Copy spring functions.
+ maps.Copy(funcs, sprig.GenericFuncMap())
return funcs
}
-func initAuth(db *sql.DB, ko *koanf.Koanf, co *core.Core) (bool, *auth.Auth) {
+// initAuth initializes the auth module with the given DB connection and
+func initAuth(co *core.Core, db *sql.DB, ko *koanf.Koanf) (bool, *auth.Auth) {
var oidcCfg auth.OIDCConfig
+ // If OIDC is enabled, set up the OIDC config.
if ko.Bool("security.oidc.enabled") {
oidcCfg = auth.OIDCConfig{
Enabled: true,
@@ -965,14 +982,14 @@ func initAuth(db *sql.DB, ko *koanf.Koanf, co *core.Core) (bool, *auth.Auth) {
}
}
- // Session manager callbacks for getting and setting cookies.
+ // Setup the sessio manager callbacks for getting and setting cookies.
cb := &auth.Callbacks{
- GetCookie: func(name string, r interface{}) (*http.Cookie, error) {
+ GetCookie: func(name string, r any) (*http.Cookie, error) {
c := r.(echo.Context)
cookie, err := c.Cookie(name)
return cookie, err
},
- SetCookie: func(cookie *http.Cookie, w interface{}) error {
+ SetCookie: func(cookie *http.Cookie, w any) error {
c := w.(echo.Context)
c.SetCookie(cookie)
return nil
@@ -982,9 +999,8 @@ func initAuth(db *sql.DB, ko *koanf.Koanf, co *core.Core) (bool, *auth.Auth) {
},
}
- a, err := auth.New(auth.Config{
- OIDC: oidcCfg,
- }, db, cb, lo)
+ // Initiaize the auth module.
+ a, err := auth.New(auth.Config{OIDC: oidcCfg}, db, cb, lo)
if err != nil {
lo.Fatalf("error initializing auth: %v", err)
}
diff --git a/cmd/install.go b/cmd/install.go
index f313e43e..65754d27 100644
--- a/cmd/install.go
+++ b/cmd/install.go
@@ -270,7 +270,7 @@ func checkSchema(db *sqlx.DB) (bool, error) {
}
func installUser(username, password string, q *models.Queries) {
- consts := initConstants()
+ consts := initConstants(ko)
// Super admin role.
perms := []string{}
diff --git a/cmd/lists.go b/cmd/lists.go
index 10b6101e..c2e86e46 100644
--- a/cmd/lists.go
+++ b/cmd/lists.go
@@ -16,22 +16,13 @@ func handleGetLists(c echo.Context) error {
app = c.Get("app").(*App)
user = auth.GetUser(c)
pg = app.paginator.NewFromURL(c.Request().URL.Query())
-
- query = strings.TrimSpace(c.FormValue("query"))
- tags = c.QueryParams()["tag"]
- orderBy = c.FormValue("order_by")
- typ = c.FormValue("type")
- optin = c.FormValue("optin")
- order = c.FormValue("order")
- minimal, _ = strconv.ParseBool(c.FormValue("minimal"))
-
- out models.PageResults
)
// Get the list IDs (or blanket permission) the user has access to.
hasAllPerm, permittedIDs := user.GetPermittedLists(auth.PermTypeGet)
// Minimal query simply returns the list of all lists without JOIN subscriber counts. This is fast.
+ minimal, _ := strconv.ParseBool(c.FormValue("minimal"))
if minimal {
res, err := app.core.GetLists("", hasAllPerm, permittedIDs)
if err != nil {
@@ -42,25 +33,38 @@ func handleGetLists(c echo.Context) error {
}
// Meta.
- out.Results = res
- out.Total = len(res)
- out.Page = 1
- out.PerPage = out.Total
+ total := len(res)
+ out := models.PageResults{
+ Results: res,
+ Total: total,
+ Page: 1,
+ PerPage: total,
+ }
return c.JSON(http.StatusOK, okResp{out})
}
// Full list query.
+ var (
+ query = strings.TrimSpace(c.FormValue("query"))
+ tags = c.QueryParams()["tag"]
+ orderBy = c.FormValue("order_by")
+ typ = c.FormValue("type")
+ optin = c.FormValue("optin")
+ order = c.FormValue("order")
+ )
res, total, err := app.core.QueryLists(query, typ, optin, tags, orderBy, order, hasAllPerm, permittedIDs, pg.Offset, pg.Limit)
if err != nil {
return err
}
- out.Query = query
- out.Results = res
- out.Total = total
- out.Page = pg.Page
- out.PerPage = pg.PerPage
+ out := models.PageResults{
+ Query: query,
+ Results: res,
+ Total: total,
+ Page: pg.Page,
+ PerPage: pg.PerPage,
+ }
return c.JSON(http.StatusOK, okResp{out})
}
@@ -71,10 +75,9 @@ func handleGetList(c echo.Context) error {
var (
app = c.Get("app").(*App)
user = auth.GetUser(c)
-
- id, _ = strconv.Atoi(c.Param("id"))
)
+ id, _ := strconv.Atoi(c.Param("id"))
if id < 1 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
@@ -97,9 +100,9 @@ func handleGetList(c echo.Context) error {
func handleCreateList(c echo.Context) error {
var (
app = c.Get("app").(*App)
- l = models.List{}
)
+ l := models.List{}
if err := c.Bind(&l); err != nil {
return err
}
@@ -123,10 +126,9 @@ func handleUpdateList(c echo.Context) error {
var (
app = c.Get("app").(*App)
user = auth.GetUser(c)
-
- id, _ = strconv.Atoi(c.Param("id"))
)
+ id, _ := strconv.Atoi(c.Param("id"))
if id < 1 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
@@ -147,6 +149,7 @@ func handleUpdateList(c echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("lists.invalidName"))
}
+ // Update the list in the DB.
out, err := app.core.UpdateList(id, l)
if err != nil {
return err
@@ -161,11 +164,12 @@ func handleDeleteLists(c echo.Context) error {
var (
app = c.Get("app").(*App)
user = auth.GetUser(c)
+ )
+ var (
id, _ = strconv.ParseInt(c.Param("id"), 10, 64)
ids []int
)
-
if id < 1 && len(ids) == 0 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
diff --git a/cmd/main.go b/cmd/main.go
index 6e538bbb..8a209f58 100644
--- a/cmd/main.go
+++ b/cmd/main.go
@@ -77,8 +77,7 @@ var (
// Buffered log writer for storing N lines of log entries for the UI.
evStream = events.New()
bufLog = buflog.New(5000)
- lo = log.New(io.MultiWriter(os.Stdout, bufLog, evStream.ErrWriter()), "",
- log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile)
+ lo = log.New(io.MultiWriter(os.Stdout, bufLog, evStream.ErrWriter()), "", log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile)
ko = koanf.New(".")
fs stuffbin.FileSystem
@@ -97,7 +96,8 @@ var (
)
func init() {
- initFlags()
+ // Initialize commandline flags.
+ initFlags(ko)
// Display version.
if ko.Bool("version") {
@@ -174,8 +174,8 @@ func main() {
app := &App{
fs: fs,
db: db,
- constants: initConstants(),
- media: initMediaStore(),
+ constants: initConstants(ko),
+ media: initMediaStore(ko),
messengers: []manager.Messenger{},
log: lo,
bufLog: bufLog,
@@ -205,29 +205,35 @@ func main() {
Log: lo,
}
+ // Load bounce config.
if err := ko.Unmarshal("bounce.actions", &cOpt.Constants.BounceActions); err != nil {
lo.Fatalf("error unmarshalling bounce config: %v", err)
}
+ // Initialize the CRUD core.
app.core = core.New(cOpt, &core.Hooks{
- SendOptinConfirmation: sendOptinConfirmationHook(app),
+ SendOptinConfirmation: optinConfirmHook(app),
})
app.queries = queries
app.manager = initCampaignManager(app.queries, app.constants, app)
app.importer = initImporter(app.queries, db, app.core, app)
- hasUsers, auth := initAuth(db.DB, ko, app.core)
+ hasUsers, auth := initAuth(app.core, db.DB, ko)
app.auth = auth
+
// If there are are no users in the DB who can login, the app has to prompt
// for new user setup.
app.needsUserSetup = !hasUsers
+ // Initialize admin email notification templates.
app.notifTpls = initNotifTemplates(fs, app.i18n, app.constants)
initTxTemplates(app.manager, app)
+ // Initialize the bounce manager that processes bounces from webhooks and
+ // POP3 mailbox scanning.
if ko.Bool("bounce.enabled") {
- app.bounce = initBounceManager(app)
+ app.bounce = initBounceManager(app.core.RecordBounce, app.queries.RecordBounce, lo, ko)
go app.bounce.Run()
}
@@ -240,7 +246,7 @@ func main() {
}
// Initialize any additional postback messengers.
- app.messengers = append(app.messengers, initPostbackMessengers()...)
+ app.messengers = append(app.messengers, initPostbackMessengers(ko)...)
// Attach all messengers to the campaign manager.
for _, m := range app.messengers {
diff --git a/cmd/maintenance.go b/cmd/maintenance.go
index c11e3a85..19b8f031 100644
--- a/cmd/maintenance.go
+++ b/cmd/maintenance.go
@@ -11,10 +11,11 @@ import (
func handleGCSubscribers(c echo.Context) error {
var (
app = c.Get("app").(*App)
- typ = c.Param("type")
)
var (
+ typ = c.Param("type")
+
n int
err error
)
@@ -43,11 +44,13 @@ func handleGCSubscriptions(c echo.Context) error {
app = c.Get("app").(*App)
)
+ // Validate the date.
t, err := time.Parse(time.RFC3339, c.FormValue("before_date"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidData"))
}
+ // Delete unconfirmed subscriptions from the DB in bulk.
n, err := app.core.DeleteUnconfirmedSubscriptions(t)
if err != nil {
return err
diff --git a/cmd/manager_store.go b/cmd/manager_store.go
index 72c3c5da..384ad562 100644
--- a/cmd/manager_store.go
+++ b/cmd/manager_store.go
@@ -139,11 +139,13 @@ func (s *store) RecordBounce(b models.Bounce) (int64, int, error) {
return res.SubscriberID, res.Num, err
}
+// BlocklistSubscriber blocklists a subscriber permanently.
func (s *store) BlocklistSubscriber(id int64) error {
_, err := s.queries.BlocklistSubscribers.Exec(pq.Int64Array{id})
return err
}
+// DeleteSubscriber deletes a subscriber from the DB.
func (s *store) DeleteSubscriber(id int64) error {
_, err := s.queries.DeleteSubscribers.Exec(pq.Int64Array{id})
return err
diff --git a/cmd/media.go b/cmd/media.go
index 06255a41..c2dc5639 100644
--- a/cmd/media.go
+++ b/cmd/media.go
@@ -26,16 +26,16 @@ var (
// handleUploadMedia handles media file uploads.
func handleUploadMedia(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- cleanUp = false
+ app = c.Get("app").(*App)
)
+
file, err := c.FormFile("file")
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest,
app.i18n.Ts("media.invalidFile", "error", err.Error()))
}
- // Read file contents in memory
+ // Read the file from the HTTP form.
src, err := file.Open()
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError,
@@ -71,7 +71,7 @@ func handleUploadMedia(c echo.Context) error {
fName = appendSuffixToFilename(fName, suffix)
}
- // Upload the file.
+ // Upload the file to the media store.
fName, err = app.media.Put(fName, contentType, src)
if err != nil {
app.log.Printf("error uploading file: %v", err)
@@ -79,14 +79,13 @@ func handleUploadMedia(c echo.Context) error {
app.i18n.Ts("media.errorUploading", "error", err.Error()))
}
+ // This keeps track of whether the file has to be deleted from the DB and the store
+ // if any of the subsequent steps fail.
var (
+ cleanUp = false
thumbfName = ""
- width = 0
- height = 0
)
defer func() {
- // If any of the subroutines in this function fail,
- // the uploaded image should be removed.
if cleanUp {
app.media.Delete(fName)
@@ -96,6 +95,9 @@ func handleUploadMedia(c echo.Context) error {
}
}()
+ // Thumbnail width and height.
+ var width, height int
+
// Create thumbnail from file for non-vector formats.
isImage := inArray(ext, imageExts)
if isImage {
@@ -123,7 +125,7 @@ func handleUploadMedia(c echo.Context) error {
thumbfName = fName
}
- // Write to the DB.
+ // Images have metadata.
meta := models.JSON{}
if isImage {
meta = models.JSON{
@@ -131,32 +133,39 @@ func handleUploadMedia(c echo.Context) error {
"height": height,
}
}
+
+ // Insert the media into the DB.
m, err := app.core.InsertMedia(fName, thumbfName, contentType, meta, app.constants.MediaUpload.Provider, app.media)
if err != nil {
cleanUp = true
return err
}
+
return c.JSON(http.StatusOK, okResp{m})
}
// handleGetMedia handles retrieval of uploaded media.
func handleGetMedia(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- pg = app.paginator.NewFromURL(c.Request().URL.Query())
- query = c.FormValue("query")
- id, _ = strconv.Atoi(c.Param("id"))
+ app = c.Get("app").(*App)
)
- // Fetch one list.
+ // Fetch one media item from the DB.
+ id, _ := strconv.Atoi(c.Param("id"))
if id > 0 {
out, err := app.core.GetMedia(id, "", "", app.media)
if err != nil {
return err
}
+
return c.JSON(http.StatusOK, okResp{out})
}
+ // Get the media from the DB.
+ var (
+ pg = app.paginator.NewFromURL(c.Request().URL.Query())
+ query = c.FormValue("query")
+ )
res, total, err := app.core.QueryMedia(app.constants.MediaUpload.Provider, app.media, query, pg.Offset, pg.Limit)
if err != nil {
return err
@@ -175,19 +184,21 @@ func handleGetMedia(c echo.Context) error {
// deleteMedia handles deletion of uploaded media.
func handleDeleteMedia(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- id, _ = strconv.Atoi(c.Param("id"))
+ app = c.Get("app").(*App)
)
+ id, _ := strconv.Atoi(c.Param("id"))
if id < 1 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
+ // Delete the media from the DB. The query returns the filename.
fname, err := app.core.DeleteMedia(id)
if err != nil {
return err
}
+ // Delete the files from the media store.
app.media.Delete(fname)
app.media.Delete(thumbPrefix + fname)
diff --git a/cmd/notifications.go b/cmd/notifications.go
index 5ae296e3..8326a2ba 100644
--- a/cmd/notifications.go
+++ b/cmd/notifications.go
@@ -21,7 +21,7 @@ var (
)
// sendNotification sends out an e-mail notification to admins.
-func (app *App) sendNotification(toEmails []string, subject, tplName string, data interface{}, headers textproto.MIMEHeader) error {
+func (app *App) sendNotification(toEmails []string, subject, tplName string, data any, headers textproto.MIMEHeader) error {
if len(toEmails) == 0 {
return nil
}
diff --git a/cmd/public.go b/cmd/public.go
index 7e4c85b5..0e035e95 100644
--- a/cmd/public.go
+++ b/cmd/public.go
@@ -47,7 +47,7 @@ type tplData struct {
EnablePublicSubPage bool
EnablePublicArchive bool
IndividualTracking bool
- Data interface{}
+ Data any
L *i18n.I18n
}
@@ -68,13 +68,17 @@ type unsubTpl struct {
ShowManage bool
}
-type optinTpl struct {
- publicTpl
+type optinReq struct {
SubUUID string
ListUUIDs []string `query:"l" form:"l"`
Lists []models.List `query:"-" form:"-"`
}
+type optinTpl struct {
+ publicTpl
+ optinReq
+}
+
type msgTpl struct {
publicTpl
MessageTitle string
@@ -92,7 +96,7 @@ var (
)
// Render executes and renders a template for echo.
-func (t *tplRenderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
+func (t *tplRenderer) Render(w io.Writer, name string, data any, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, tplData{
SiteName: t.SiteName,
RootURL: t.RootURL,
@@ -140,12 +144,11 @@ func handleGetPublicLists(c echo.Context) error {
// This is the view the {{ MessageURL }} template tag links to in e-mail campaigns.
func handleViewCampaignMessage(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- campUUID = c.Param("campUUID")
- subUUID = c.Param("subUUID")
+ app = c.Get("app").(*App)
)
// Get the campaign.
+ campUUID := c.Param("campUUID")
camp, err := app.core.GetCampaign(0, campUUID, "")
if err != nil {
if er, ok := err.(*echo.HTTPError); ok {
@@ -160,6 +163,7 @@ func handleViewCampaignMessage(c echo.Context) error {
}
// Get the subscriber.
+ subUUID := c.Param("subUUID")
sub, err := app.core.GetSubscriber(0, subUUID, "")
if err != nil {
if err == sql.ErrNoRows {
@@ -189,41 +193,46 @@ func handleViewCampaignMessage(c echo.Context) error {
return c.HTML(http.StatusOK, string(msg.Body()))
}
-// handleSubscriptionPage renders the subscription management page and
-// handles unsubscriptions. This is the view that {{ UnsubscribeURL }} in
-// campaigns link to.
+// handleSubscriptionPage renders the subscription management page and handles unsubscriptions.
+// This is the view that {{ UnsubscribeURL }} in campaigns link to.
func handleSubscriptionPage(c echo.Context) error {
var (
- app = c.Get("app").(*App)
+ app = c.Get("app").(*App)
+ )
+
+ var (
subUUID = c.Param("subUUID")
showManage, _ = strconv.ParseBool(c.FormValue("manage"))
- out = unsubTpl{}
)
- out.SubUUID = subUUID
- out.Title = app.i18n.T("public.unsubscribeTitle")
- out.AllowBlocklist = app.constants.Privacy.AllowBlocklist
- out.AllowExport = app.constants.Privacy.AllowExport
- out.AllowWipe = app.constants.Privacy.AllowWipe
- out.AllowPreferences = app.constants.Privacy.AllowPreferences
+ // Get the subscriber from the DB.
s, err := app.core.GetSubscriber(0, subUUID, "")
if err != nil {
return c.Render(http.StatusInternalServerError, tplMessage,
makeMsgTpl(app.i18n.T("public.errorTitle"), "", app.i18n.Ts("public.errorProcessingRequest")))
}
- out.Subscriber = s
+ // Prepare the public template.
+ out := unsubTpl{
+ Subscriber: s,
+ SubUUID: subUUID,
+ publicTpl: publicTpl{Title: app.i18n.T("public.unsubscribeTitle")},
+ AllowBlocklist: app.constants.Privacy.AllowBlocklist,
+ AllowExport: app.constants.Privacy.AllowExport,
+ AllowWipe: app.constants.Privacy.AllowWipe,
+ AllowPreferences: app.constants.Privacy.AllowPreferences,
+ }
+
+ // If the subscriber is blocklisted, throw an error.
if s.Status == models.SubscriberStatusBlockListed {
- return c.Render(http.StatusOK, tplMessage,
- makeMsgTpl(app.i18n.T("public.noSubTitle"), "", app.i18n.Ts("public.blocklisted")))
+ return c.Render(http.StatusOK, tplMessage, makeMsgTpl(app.i18n.T("public.noSubTitle"), "", app.i18n.Ts("public.blocklisted")))
}
// Only show preference management if it's enabled in settings.
if app.constants.Privacy.AllowPreferences {
out.ShowManage = showManage
- }
- if out.ShowManage {
- // Get the subscriber's lists.
+
+ // Get the subscriber's lists from the DB to render in the template.
subs, err := app.core.GetSubscriptions(0, subUUID, false)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("public.errorFetchingLists"))
@@ -231,6 +240,7 @@ func handleSubscriptionPage(c echo.Context) error {
out.Subscriptions = make([]models.Subscription, 0, len(subs))
for _, s := range subs {
+ // Private lists shouldn't be rendered in the template.
if s.Type == models.ListTypePrivate {
continue
}
@@ -247,26 +257,27 @@ func handleSubscriptionPage(c echo.Context) error {
// campaigns link to.
func handleSubscriptionPrefs(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- campUUID = c.Param("campUUID")
- subUUID = c.Param("subUUID")
-
- req struct {
- Name string `form:"name" json:"name"`
- ListUUIDs []string `form:"l" json:"list_uuids"`
- Blocklist bool `form:"blocklist" json:"blocklist"`
- Manage bool `form:"manage" json:"manage"`
- }
+ app = c.Get("app").(*App)
)
// Read the form.
+ var req struct {
+ Name string `form:"name" json:"name"`
+ ListUUIDs []string `form:"l" json:"list_uuids"`
+ Blocklist bool `form:"blocklist" json:"blocklist"`
+ Manage bool `form:"manage" json:"manage"`
+ }
if err := c.Bind(&req); err != nil {
return c.Render(http.StatusBadRequest, tplMessage,
makeMsgTpl(app.i18n.T("public.errorTitle"), "", app.i18n.T("globals.messages.invalidData")))
}
// Simple unsubscribe.
- blocklist := app.constants.Privacy.AllowBlocklist && req.Blocklist
+ var (
+ campUUID = c.Param("campUUID")
+ subUUID = c.Param("subUUID")
+ blocklist = app.constants.Privacy.AllowBlocklist && req.Blocklist
+ )
if !req.Manage || blocklist {
if err := app.core.UnsubscribeByCampaign(subUUID, campUUID, blocklist); err != nil {
return c.Render(http.StatusInternalServerError, tplMessage,
@@ -299,7 +310,7 @@ func handleSubscriptionPrefs(c echo.Context) error {
}
sub.Name = req.Name
- // Update name.
+ // Update the subscriber properties in the DB.
if _, err := app.core.UpdateSubscriber(sub.ID, sub); err != nil {
return c.Render(http.StatusInternalServerError, tplMessage,
makeMsgTpl(app.i18n.T("public.errorTitle"), "", app.i18n.T("public.errorProcessingRequest")))
@@ -312,11 +323,13 @@ func handleSubscriptionPrefs(c echo.Context) error {
reqUUIDs[u] = struct{}{}
}
+ // Get subscription from teh DB.
subs, err := app.core.GetSubscriptions(0, subUUID, false)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("public.errorFetchingLists"))
}
+ // Filter the lists in the request against the subscriptions in the DB.
unsubUUIDs := make([]string, 0, len(req.ListUUIDs))
for _, s := range subs {
if s.Type == models.ListTypePrivate {
@@ -343,23 +356,21 @@ func handleSubscriptionPrefs(c echo.Context) error {
// notifications.
func handleOptinPage(c echo.Context) error {
var (
- app = c.Get("app").(*App)
+ app = c.Get("app").(*App)
+ )
+
+ var (
subUUID = c.Param("subUUID")
confirm, _ = strconv.ParseBool(c.FormValue("confirm"))
- out = optinTpl{}
+ req optinReq
)
- out.SubUUID = subUUID
- out.Title = app.i18n.T("public.confirmOptinSubTitle")
- out.SubUUID = subUUID
-
- // Get and validate fields.
- if err := c.Bind(&out); err != nil {
+ if err := c.Bind(&req); err != nil {
return err
}
// Validate list UUIDs if there are incoming UUIDs in the request.
- if len(out.ListUUIDs) > 0 {
- for _, l := range out.ListUUIDs {
+ if len(req.ListUUIDs) > 0 {
+ for _, l := range req.ListUUIDs {
if !reUUID.MatchString(l) {
return c.Render(http.StatusBadRequest, tplMessage,
makeMsgTpl(app.i18n.T("public.errorTitle"), "", app.i18n.T("globals.messages.invalidUUID")))
@@ -368,7 +379,7 @@ func handleOptinPage(c echo.Context) error {
}
// Get the list of subscription lists where the subscriber hasn't confirmed.
- lists, err := app.core.GetSubscriberLists(0, subUUID, nil, out.ListUUIDs, models.SubscriptionStatusUnconfirmed, "")
+ lists, err := app.core.GetSubscriberLists(0, subUUID, nil, req.ListUUIDs, models.SubscriptionStatusUnconfirmed, "")
if err != nil {
return c.Render(http.StatusInternalServerError, tplMessage,
makeMsgTpl(app.i18n.T("public.errorTitle"), "", app.i18n.Ts("public.errorFetchingLists")))
@@ -379,7 +390,6 @@ func handleOptinPage(c echo.Context) error {
return c.Render(http.StatusOK, tplMessage,
makeMsgTpl(app.i18n.T("public.noSubTitle"), "", app.i18n.Ts("public.noSubInfo")))
}
- out.Lists = lists
// Confirm.
if confirm {
@@ -392,7 +402,8 @@ func handleOptinPage(c echo.Context) error {
}
}
- if err := app.core.ConfirmOptionSubscription(subUUID, out.ListUUIDs, meta); err != nil {
+ // Confirm subscriptions in the DB.
+ if err := app.core.ConfirmOptionSubscription(subUUID, req.ListUUIDs, meta); err != nil {
app.log.Printf("error unsubscribing: %v", err)
return c.Render(http.StatusInternalServerError, tplMessage,
makeMsgTpl(app.i18n.T("public.errorTitle"), "", app.i18n.Ts("public.errorProcessingRequest")))
@@ -402,6 +413,11 @@ func handleOptinPage(c echo.Context) error {
makeMsgTpl(app.i18n.T("public.subConfirmedTitle"), "", app.i18n.Ts("public.subConfirmed")))
}
+ var out optinTpl
+ out.Lists = lists
+ out.SubUUID = subUUID
+ out.Title = app.i18n.T("public.confirmOptinSubTitle")
+
return c.Render(http.StatusOK, "optin", out)
}
@@ -417,13 +433,14 @@ func handleSubscriptionFormPage(c echo.Context) error {
makeMsgTpl(app.i18n.T("public.errorTitle"), "", app.i18n.Ts("public.invalidFeature")))
}
- // Get all public lists.
+ // Get all public lists from the DB.
lists, err := app.core.GetLists(models.ListTypePublic, true, nil)
if err != nil {
return c.Render(http.StatusInternalServerError, tplMessage,
makeMsgTpl(app.i18n.T("public.errorTitle"), "", app.i18n.Ts("public.errorFetchingLists")))
}
+ // There are no public lists available for subscription.
if len(lists) == 0 {
return c.Render(http.StatusInternalServerError, tplMessage,
makeMsgTpl(app.i18n.T("public.errorTitle"), "", app.i18n.Ts("public.noListsAvailable")))
@@ -433,6 +450,7 @@ func handleSubscriptionFormPage(c echo.Context) error {
out.Title = app.i18n.T("public.sub")
out.Lists = lists
+ // Captcha is enabled. Set the key for the template to render.
if app.constants.Security.EnableCaptcha {
out.CaptchaKey = app.constants.Security.CaptchaKey
}
@@ -472,10 +490,11 @@ func handleSubscriptionForm(c echo.Context) error {
return e
}
- return c.Render(e.Code, tplMessage,
- makeMsgTpl(app.i18n.T("public.errorTitle"), "", fmt.Sprintf("%s", e.Message)))
+ return c.Render(e.Code, tplMessage, makeMsgTpl(app.i18n.T("public.errorTitle"), "", fmt.Sprintf("%s", e.Message)))
}
+ // If there were double optin lists, show the opt-in pending message instead of
+ // the subscription confirmation message.
msg := "public.subConfirmed"
if hasOptin {
msg = "public.subOptinPending"
@@ -510,17 +529,20 @@ func handlePublicSubscription(c echo.Context) error {
// campaign. These links are generated by {{ TrackLink }} tags in campaigns.
func handleLinkRedirect(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- linkUUID = c.Param("linkUUID")
- campUUID = c.Param("campUUID")
- subUUID = c.Param("subUUID")
+ app = c.Get("app").(*App)
)
// If individual tracking is disabled, do not record the subscriber ID.
+ subUUID := c.Param("subUUID")
if !app.constants.Privacy.IndividualTracking {
subUUID = ""
}
+ // Inser the link click in the DB.
+ var (
+ linkUUID = c.Param("linkUUID")
+ campUUID = c.Param("campUUID")
+ )
url, err := app.core.RegisterCampaignLinkClick(linkUUID, campUUID, subUUID)
if err != nil {
e := err.(*echo.HTTPError)
@@ -536,17 +558,17 @@ func handleLinkRedirect(c echo.Context) error {
// the {{ TrackView }} template tag in campaigns.
func handleRegisterCampaignView(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- campUUID = c.Param("campUUID")
- subUUID = c.Param("subUUID")
+ app = c.Get("app").(*App)
)
// If individual tracking is disabled, do not record the subscriber ID.
+ subUUID := c.Param("subUUID")
if !app.constants.Privacy.IndividualTracking {
subUUID = ""
}
// Exclude dummy hits from template previews.
+ campUUID := c.Param("campUUID")
if campUUID != dummyUUID && subUUID != dummyUUID {
if err := app.core.RegisterCampaignView(campUUID, subUUID); err != nil {
app.log.Printf("error registering campaign view: %s", err)
@@ -563,9 +585,9 @@ func handleRegisterCampaignView(c echo.Context) error {
// is dependent on the configuration.
func handleSelfExportSubscriberData(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- subUUID = c.Param("subUUID")
+ app = c.Get("app").(*App)
)
+
// Is export allowed?
if !app.constants.Privacy.AllowExport {
return c.Render(http.StatusBadRequest, tplMessage,
@@ -575,6 +597,7 @@ func handleSelfExportSubscriberData(c echo.Context) error {
// Get the subscriber's data. A single query that gets the profile,
// list subscriptions, campaign views, and link clicks. Names of
// private lists are replaced with "Private list".
+ subUUID := c.Param("subUUID")
data, b, err := exportSubscriberData(0, subUUID, app.constants.Privacy.Exportable, app)
if err != nil {
app.log.Printf("error exporting subscriber data: %s", err)
@@ -596,7 +619,7 @@ func handleSelfExportSubscriberData(c echo.Context) error {
)
subject, body = getTplSubject(subject, body)
- // Send the data as a JSON attachment to the subscriber.
+ // E-mail the data as a JSON attachment to the subscriber.
const fname = "data.json"
if err := app.emailMessenger.Push(models.Message{
ContentType: app.notifTpls.contentType,
@@ -626,8 +649,7 @@ func handleSelfExportSubscriberData(c echo.Context) error {
// clicks remain as orphan data unconnected to any subscriber.
func handleWipeSubscriberData(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- subUUID = c.Param("subUUID")
+ app = c.Get("app").(*App)
)
// Is wiping allowed?
@@ -636,6 +658,7 @@ func handleWipeSubscriberData(c echo.Context) error {
makeMsgTpl(app.i18n.T("public.errorTitle"), "", app.i18n.Ts("public.invalidFeature")))
}
+ subUUID := c.Param("subUUID")
if err := app.core.DeleteSubscribers(nil, []string{subUUID}); err != nil {
app.log.Printf("error wiping subscriber data: %s", err)
return c.Render(http.StatusInternalServerError, tplMessage,
@@ -654,6 +677,7 @@ func drawTransparentImage(h, w int) []byte {
out = &bytes.Buffer{}
)
_ = png.Encode(out, img)
+
return out.Bytes()
}
@@ -663,14 +687,14 @@ func drawTransparentImage(h, w int) []byte {
func processSubForm(c echo.Context) (bool, error) {
var (
app = c.Get("app").(*App)
- req struct {
- Name string `form:"name" json:"name"`
- Email string `form:"email" json:"email"`
- FormListUUIDs []string `form:"l" json:"list_uuids"`
- }
)
// Get and validate fields.
+ var req struct {
+ Name string `form:"name" json:"name"`
+ Email string `form:"email" json:"email"`
+ FormListUUIDs []string `form:"l" json:"list_uuids"`
+ }
if err := c.Bind(&req); err != nil {
return false, err
}
@@ -722,13 +746,15 @@ func processSubForm(c echo.Context) (bool, error) {
Status: models.SubscriberStatusEnabled,
}, nil, listUUIDs, false)
if err != nil {
- // Subscriber already exists. Update subscriptions.
+ // Subscriber already exists. Update subscriptions in the DB.
if e, ok := err.(*echo.HTTPError); ok && e.Code == http.StatusConflict {
+ // Get the subscriber from the DB by their email.
sub, err := app.core.GetSubscriber(0, "", req.Email)
if err != nil {
return false, err
}
+ // Update the subscriber's subscriptions in the DB.
_, hasOptin, err := app.core.UpdateSubscriberWithLists(sub.ID, sub, nil, listUUIDs, false, false)
if err != nil {
return false, err
diff --git a/cmd/roles.go b/cmd/roles.go
index f4376659..197f79ef 100644
--- a/cmd/roles.go
+++ b/cmd/roles.go
@@ -44,17 +44,17 @@ func handleGeListRoles(c echo.Context) error {
func handleCreateUserRole(c echo.Context) error {
var (
app = c.Get("app").(*App)
- r = auth.Role{}
)
+ var r auth.Role
if err := c.Bind(&r); err != nil {
return err
}
-
if err := validateUserRole(r, app); err != nil {
return err
}
+ // Create the role in the DB.
out, err := app.core.CreateRole(r)
if err != nil {
return err
@@ -67,17 +67,17 @@ func handleCreateUserRole(c echo.Context) error {
func handleCreateListRole(c echo.Context) error {
var (
app = c.Get("app").(*App)
- r = auth.ListRole{}
)
+ var r auth.ListRole
if err := c.Bind(&r); err != nil {
return err
}
-
if err := validateListRole(r, app); err != nil {
return err
}
+ // Create the role in the DB.
out, err := app.core.CreateListRole(r)
if err != nil {
return err
@@ -89,10 +89,11 @@ func handleCreateListRole(c echo.Context) error {
// handleUpdateUserRole handles role modification.
func handleUpdateUserRole(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- id, _ = strconv.Atoi(c.Param("id"))
+ app = c.Get("app").(*App)
)
+ // ID 1 is reserved for the Super Admin role and anything below that is invalid.
+ id, _ := strconv.Atoi(c.Param("id"))
if id < 2 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
@@ -102,7 +103,6 @@ func handleUpdateUserRole(c echo.Context) error {
if err := c.Bind(&r); err != nil {
return err
}
-
if err := validateUserRole(r, app); err != nil {
return err
}
@@ -110,12 +110,13 @@ func handleUpdateUserRole(c echo.Context) error {
// Validate.
r.Name.String = strings.TrimSpace(r.Name.String)
+ // Update the role in the DB.
out, err := app.core.UpdateUserRole(id, r)
if err != nil {
return err
}
- // Cache the API token for validating API queries without hitting the DB every time.
+ // Cache API tokens for in-memory, off-DB /api/* request auth.
if _, err := cacheUsers(app.core, app.auth); err != nil {
return err
}
@@ -126,10 +127,11 @@ func handleUpdateUserRole(c echo.Context) error {
// handleUpdateListRole handles role modification.
func handleUpdateListRole(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- id, _ = strconv.Atoi(c.Param("id"))
+ app = c.Get("app").(*App)
)
+ // ID 1 is reserved for the Super Admin role and anything below that is invalid.
+ id, _ := strconv.Atoi(c.Param("id"))
if id < 2 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
@@ -147,12 +149,13 @@ func handleUpdateListRole(c echo.Context) error {
// Validate.
r.Name.String = strings.TrimSpace(r.Name.String)
+ // Update the role in the DB.
out, err := app.core.UpdateListRole(id, r)
if err != nil {
return err
}
- // Cache the API token for validating API queries without hitting the DB every time.
+ // Cache API tokens for in-memory, off-DB /api/* request auth.
if _, err := cacheUsers(app.core, app.auth); err != nil {
return err
}
@@ -163,19 +166,20 @@ func handleUpdateListRole(c echo.Context) error {
// handleDeleteRole handles role deletion.
func handleDeleteRole(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- id, _ = strconv.ParseInt(c.Param("id"), 10, 64)
+ app = c.Get("app").(*App)
)
- if id < 1 {
+ id, _ := strconv.ParseInt(c.Param("id"), 10, 64)
+ if id < 2 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
+ // Delete the role from the DB.
if err := app.core.DeleteRole(int(id)); err != nil {
return err
}
- // Cache the API token for validating API queries without hitting the DB every time.
+ // Cache API tokens for in-memory, off-DB /api/* request auth.
if _, err := cacheUsers(app.core, app.auth); err != nil {
return err
}
@@ -184,7 +188,6 @@ func handleDeleteRole(c echo.Context) error {
}
func validateUserRole(r auth.Role, app *App) error {
- // Validate fields.
if !strHasLen(r.Name.String, 1, stdInputMaxLen) {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.Ts("globals.messages.invalidFields", "name", "name"))
}
@@ -199,14 +202,13 @@ func validateUserRole(r auth.Role, app *App) error {
}
func validateListRole(r auth.ListRole, app *App) error {
- // Validate fields.
if !strHasLen(r.Name.String, 1, stdInputMaxLen) {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.Ts("globals.messages.invalidFields", "name", "name"))
}
for _, l := range r.Lists {
for _, p := range l.Permissions {
- if p != "list:get" && p != "list:manage" {
+ if p != auth.PermListGet && p != auth.PermListManage {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.Ts("globals.messages.invalidFields", "name", fmt.Sprintf("list permission: %s", p)))
}
}
diff --git a/cmd/settings.go b/cmd/settings.go
index 7a1a4e9c..a686f78e 100644
--- a/cmd/settings.go
+++ b/cmd/settings.go
@@ -29,11 +29,13 @@ type aboutHost struct {
Machine string `json:"arch"`
Hostname string `json:"hostname"`
}
+
type aboutSystem struct {
NumCPU int `json:"num_cpu"`
AllocMB uint64 `json:"memory_alloc_mb"`
OSMB uint64 `json:"memory_from_os_mb"`
}
+
type about struct {
Version string `json:"version"`
Build string `json:"build"`
@@ -50,7 +52,9 @@ var (
// handleGetSettings returns settings from the DB.
func handleGetSettings(c echo.Context) error {
- app := c.Get("app").(*App)
+ var (
+ app = c.Get("app").(*App)
+ )
s, err := app.core.GetSettings()
if err != nil {
@@ -82,10 +86,10 @@ func handleGetSettings(c echo.Context) error {
func handleUpdateSettings(c echo.Context) error {
var (
app = c.Get("app").(*App)
- set models.Settings
)
// Unmarshal and marshal the fields once to sanitize the settings blob.
+ var set models.Settings
if err := c.Bind(&set); err != nil {
return err
}
@@ -150,6 +154,7 @@ func handleUpdateSettings(c echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("settings.errorNoSMTP"))
}
+ // Always remove the trailing slash from the app root URL.
set.AppRootURL = strings.TrimRight(set.AppRootURL, "/")
// Bounce boxes.
@@ -284,13 +289,18 @@ func handleUpdateSettings(c echo.Context) error {
// handleGetLogs returns the log entries stored in the log buffer.
func handleGetLogs(c echo.Context) error {
- app := c.Get("app").(*App)
+ var (
+ app = c.Get("app").(*App)
+ )
+
return c.JSON(http.StatusOK, okResp{app.bufLog.Lines()})
}
// handleTestSMTPSettings returns the log entries stored in the log buffer.
func handleTestSMTPSettings(c echo.Context) error {
- app := c.Get("app").(*App)
+ var (
+ app = c.Get("app").(*App)
+ )
// Copy the raw JSON post body.
reqBody, err := io.ReadAll(c.Request().Body)
@@ -327,6 +337,7 @@ func handleTestSMTPSettings(c echo.Context) error {
app.i18n.Ts("globals.messages.errorCreating", "name", "SMTP", "error", err.Error()))
}
+ // Render the test email template body.
var b bytes.Buffer
if err := app.notifTpls.tpls.ExecuteTemplate(&b, "smtp-test", nil); err != nil {
app.log.Printf("error compiling notification template '%s': %v", "smtp-test", err)
@@ -349,9 +360,9 @@ func handleTestSMTPSettings(c echo.Context) error {
func handleGetAboutInfo(c echo.Context) error {
var (
app = c.Get("app").(*App)
- mem runtime.MemStats
)
+ var mem runtime.MemStats
runtime.ReadMemStats(&mem)
out := app.about
diff --git a/cmd/subscribers.go b/cmd/subscribers.go
index 928b4a8e..d54a6cb1 100644
--- a/cmd/subscribers.go
+++ b/cmd/subscribers.go
@@ -54,11 +54,11 @@ var (
// handleGetSubscriber handles the retrieval of a single subscriber by ID.
func handleGetSubscriber(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- id, _ = strconv.Atoi(c.Param("id"))
- user = auth.GetUser(c)
+ app = c.Get("app").(*App)
+ user = auth.GetUser(c)
)
+ id, _ := strconv.Atoi(c.Param("id"))
if id < 1 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
@@ -82,32 +82,34 @@ func handleQuerySubscribers(c echo.Context) error {
var (
app = c.Get("app").(*App)
user = auth.GetUser(c)
- pg = app.paginator.NewFromURL(c.Request().URL.Query())
-
- // The "WHERE ?" bit.
- query = sanitizeSQLExp(c.FormValue("query"))
- subStatus = c.FormValue("subscription_status")
- orderBy = c.FormValue("order_by")
- order = c.FormValue("order")
- out models.PageResults
)
// Filter list IDs by permission.
- listIDs, err := filterListQueryByPerm(c.QueryParams(), user, app)
+ listIDs, err := filterListQueryByPerm("list_id", c.QueryParams(), user, app)
if err != nil {
return err
}
+ var (
+ // The "WHERE ?" bit.
+ query = sanitizeSQLExp(c.FormValue("query"))
+ subStatus = c.FormValue("subscription_status")
+ orderBy = c.FormValue("order_by")
+ order = c.FormValue("order")
+ pg = app.paginator.NewFromURL(c.Request().URL.Query())
+ )
res, total, err := app.core.QuerySubscribers(query, listIDs, subStatus, order, orderBy, pg.Offset, pg.Limit)
if err != nil {
return err
}
- out.Query = query
- out.Results = res
- out.Total = total
- out.Page = pg.Page
- out.PerPage = pg.PerPage
+ out := models.PageResults{
+ Query: query,
+ Results: res,
+ Total: total,
+ Page: pg.Page,
+ PerPage: pg.PerPage,
+ }
return c.JSON(http.StatusOK, okResp{out})
}
@@ -117,13 +119,10 @@ func handleExportSubscribers(c echo.Context) error {
var (
app = c.Get("app").(*App)
user = auth.GetUser(c)
-
- // The "WHERE ?" bit.
- query = sanitizeSQLExp(c.FormValue("query"))
)
// Filter list IDs by permission.
- listIDs, err := filterListQueryByPerm(c.QueryParams(), user, app)
+ listIDs, err := filterListQueryByPerm("list_id", c.QueryParams(), user, app)
if err != nil {
return err
}
@@ -138,6 +137,7 @@ func handleExportSubscribers(c echo.Context) error {
subStatus := c.QueryParam("subscription_status")
// Get the batched export iterator.
+ query := sanitizeSQLExp(c.FormValue("query"))
exp, err := app.core.ExportSubscribers(query, subIDs, listIDs, subStatus, app.constants.DBBatchSize)
if err != nil {
return err
@@ -186,11 +186,10 @@ func handleCreateSubscriber(c echo.Context) error {
var (
app = c.Get("app").(*App)
user = auth.GetUser(c)
-
- req subimporter.SubReq
)
// Get and validate fields.
+ var req subimporter.SubReq
if err := c.Bind(&req); err != nil {
return err
}
@@ -218,24 +217,24 @@ func handleUpdateSubscriber(c echo.Context) error {
var (
app = c.Get("app").(*App)
user = auth.GetUser(c)
-
- id, _ = strconv.Atoi(c.Param("id"))
- req struct {
- models.Subscriber
- Lists []int `json:"lists"`
- PreconfirmSubs bool `json:"preconfirm_subscriptions"`
- }
)
+ id, _ := strconv.Atoi(c.Param("id"))
+ if id < 1 {
+ return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
+ }
+
// Get and validate fields.
+ req := struct {
+ models.Subscriber
+ Lists []int `json:"lists"`
+ PreconfirmSubs bool `json:"preconfirm_subscriptions"`
+ }{}
if err := c.Bind(&req); err != nil {
return err
}
- if id < 1 {
- return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
- }
-
+ // Sanitize and validate the email field.
if em, err := app.importer.SanitizeEmail(req.Email); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
} else {
@@ -249,6 +248,7 @@ func handleUpdateSubscriber(c echo.Context) error {
// Filter lists against the current user's permitted lists.
listIDs := user.FilterListsByPerm(auth.PermTypeManage, req.Lists)
+ // Update the subscriber in the DB.
out, _, err := app.core.UpdateSubscriberWithLists(id, req.Subscriber, listIDs, nil, req.PreconfirmSubs, true)
if err != nil {
return err
@@ -260,10 +260,10 @@ func handleUpdateSubscriber(c echo.Context) error {
// handleSubscriberSendOptin sends an optin confirmation e-mail to a subscriber.
func handleSubscriberSendOptin(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- id, _ = strconv.Atoi(c.Param("id"))
+ app = c.Get("app").(*App)
)
+ id, _ := strconv.Atoi(c.Param("id"))
if id < 1 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
@@ -274,7 +274,8 @@ func handleSubscriberSendOptin(c echo.Context) error {
return err
}
- if _, err := sendOptinConfirmationHook(app)(out, nil); err != nil {
+ // Trigger the opt-in confirmation e-mail hook.
+ if _, err := optinConfirmHook(app)(out, nil); err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, app.i18n.T("subscribers.errorSendingOptin"))
}
@@ -285,12 +286,14 @@ func handleSubscriberSendOptin(c echo.Context) error {
// It takes either an ID in the URI, or a list of IDs in the request body.
func handleBlocklistSubscribers(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- pID = c.Param("id")
- subIDs []int
+ app = c.Get("app").(*App)
)
// Is it a /:id call?
+ var (
+ subIDs []int
+ pID = c.Param("id")
+ )
if pID != "" {
id, _ := strconv.Atoi(pID)
if id < 1 {
@@ -313,6 +316,7 @@ func handleBlocklistSubscribers(c echo.Context) error {
subIDs = req.SubscriberIDs
}
+ // Update the subscribers in the DB.
if err := app.core.BlocklistSubscribers(subIDs); err != nil {
return err
}
@@ -327,12 +331,13 @@ func handleManageSubscriberLists(c echo.Context) error {
var (
app = c.Get("app").(*App)
user = auth.GetUser(c)
+ )
+ // Is it an /:id call?
+ var (
pID = c.Param("id")
subIDs []int
)
-
- // Is it an /:id call?
if pID != "" {
id, _ := strconv.Atoi(pID)
if id < 1 {
@@ -359,7 +364,7 @@ func handleManageSubscriberLists(c echo.Context) error {
// Filter lists against the current user's permitted lists.
listIDs := user.FilterListsByPerm(auth.PermTypeManage, req.TargetListIDs)
- // Action.
+ // Run the action in the DB.
var err error
switch req.Action {
case "add":
@@ -383,12 +388,14 @@ func handleManageSubscriberLists(c echo.Context) error {
// It takes either an ID in the URI, or a list of IDs in the request body.
func handleDeleteSubscribers(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- pID = c.Param("id")
- subIDs []int
+ app = c.Get("app").(*App)
)
// Is it an /:id call?
+ var (
+ pID = c.Param("id")
+ subIDs []int
+ )
if pID != "" {
id, _ := strconv.Atoi(pID)
if id < 1 {
@@ -408,6 +415,7 @@ func handleDeleteSubscribers(c echo.Context) error {
subIDs = i
}
+ // Delete the subscribers from the DB.
if err := app.core.DeleteSubscribers(subIDs, nil); err != nil {
return err
}
@@ -420,19 +428,21 @@ func handleDeleteSubscribers(c echo.Context) error {
func handleDeleteSubscribersByQuery(c echo.Context) error {
var (
app = c.Get("app").(*App)
- req subQueryReq
)
+ var req subQueryReq
if err := c.Bind(&req); err != nil {
return err
}
if req.All {
+ // If the "all" flag is set, ignore any subquery that may be present.
req.Query = ""
} else if req.Query == "" {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.Ts("globals.messages.invalidFields", "name", "query"))
}
+ // Delete the subscribers from the DB.
if err := app.core.DeleteSubscribersByQuery(req.Query, req.ListIDs, req.SubscriptionStatus); err != nil {
return err
}
@@ -445,9 +455,9 @@ func handleDeleteSubscribersByQuery(c echo.Context) error {
func handleBlocklistSubscribersByQuery(c echo.Context) error {
var (
app = c.Get("app").(*App)
- req subQueryReq
)
+ var req subQueryReq
if err := c.Bind(&req); err != nil {
return err
}
@@ -456,6 +466,7 @@ func handleBlocklistSubscribersByQuery(c echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.Ts("globals.messages.invalidFields", "name", "query"))
}
+ // Update the subscribers in the DB.
if err := app.core.BlocklistSubscribersByQuery(req.Query, req.ListIDs, req.SubscriptionStatus); err != nil {
return err
}
@@ -469,10 +480,9 @@ func handleManageSubscriberListsByQuery(c echo.Context) error {
var (
app = c.Get("app").(*App)
user = auth.GetUser(c)
-
- req subQueryReq
)
+ var req subQueryReq
if err := c.Bind(&req); err != nil {
return err
}
@@ -485,7 +495,7 @@ func handleManageSubscriberListsByQuery(c echo.Context) error {
sourceListIDs := user.FilterListsByPerm(auth.PermTypeManage, req.ListIDs)
targetListIDs := user.FilterListsByPerm(auth.PermTypeManage, req.TargetListIDs)
- // Action.
+ // Run the action in the DB.
var err error
switch req.Action {
case "add":
@@ -509,14 +519,14 @@ func handleManageSubscriberListsByQuery(c echo.Context) error {
func handleDeleteSubscriberBounces(c echo.Context) error {
var (
app = c.Get("app").(*App)
- pID = c.Param("id")
)
- id, _ := strconv.Atoi(pID)
+ id, _ := strconv.Atoi(c.Param("id"))
if id < 1 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
+ // Delete the bounces from the DB.
if err := app.core.DeleteSubscriberBounces(id, ""); err != nil {
return err
}
@@ -546,10 +556,10 @@ func handleExportSubscriberData(c echo.Context) error {
if err != nil {
app.log.Printf("error exporting subscriber data: %s", err)
return echo.NewHTTPError(http.StatusInternalServerError,
- app.i18n.Ts("globals.messages.errorFetching",
- "name", "{globals.terms.subscribers}", "error", err.Error()))
+ app.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.subscribers}", "error", err.Error()))
}
+ // Set headers to force the browser to prompt for download.
c.Response().Header().Set("Cache-Control", "no-cache")
c.Response().Header().Set("Content-Disposition", `attachment; filename="data.json"`)
return c.Blob(http.StatusOK, "application/json", b)
@@ -595,15 +605,16 @@ func sanitizeSQLExp(q string) string {
if len(q) == 0 {
return ""
}
- q = strings.TrimSpace(q)
// Remove semicolon suffix.
+ q = strings.TrimSpace(q)
if q[len(q)-1] == ';' {
q = q[:len(q)-1]
}
return q
}
+// getQueryInts parses the list of given query param values into ints.
func getQueryInts(param string, qp url.Values) ([]int, error) {
var out []int
if vals, ok := qp[param]; ok {
@@ -623,10 +634,10 @@ func getQueryInts(param string, qp url.Values) ([]int, error) {
return out, nil
}
-// sendOptinConfirmationHook returns an enclosed callback that sends optin confirmation e-mails.
+// optinConfirmHook returns an enclosed callback that sends optin confirmation e-mails.
// This is plugged into the 'core' package to send optin confirmations when a new subscriber is
// created via `core.CreateSubscriber()`.
-func sendOptinConfirmationHook(app *App) func(sub models.Subscriber, listIDs []int) (int, error) {
+func optinConfirmHook(app *App) func(sub models.Subscriber, listIDs []int) (int, error) {
return func(sub models.Subscriber, listIDs []int) (int, error) {
lists, err := app.core.GetSubscriberLists(sub.ID, "", listIDs, nil, models.SubscriptionStatusUnconfirmed, models.ListOptinDouble)
if err != nil {
@@ -696,12 +707,13 @@ func hasSubPerm(u auth.User, subIDs []int, app *App) error {
return nil
}
-func filterListQueryByPerm(qp url.Values, user auth.User, app *App) ([]int, error) {
+// filterListQueryByPerm filters the list IDs in the query params and returns the list IDs to which the user has access.
+func filterListQueryByPerm(param string, qp url.Values, user auth.User, app *App) ([]int, error) {
var listIDs []int
// If there are incoming list query params, filter them by permission.
- if qp.Has("list_id") {
- ids, err := getQueryInts("list_id", qp)
+ if qp.Has(param) {
+ ids, err := getQueryInts(param, qp)
if err != nil {
return nil, echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
diff --git a/cmd/templates.go b/cmd/templates.go
index 392e2471..6a77ebc7 100644
--- a/cmd/templates.go
+++ b/cmd/templates.go
@@ -35,12 +35,13 @@ var (
func handleGetTemplates(c echo.Context) error {
var (
app = c.Get("app").(*App)
+ )
+ // Fetch one list.
+ var (
id, _ = strconv.Atoi(c.Param("id"))
noBody, _ = strconv.ParseBool(c.QueryParam("no_body"))
)
-
- // Fetch one list.
if id > 0 {
out, err := app.core.GetTemplate(id, noBody)
if err != nil {
@@ -50,6 +51,7 @@ func handleGetTemplates(c echo.Context) error {
return c.JSON(http.StatusOK, okResp{out})
}
+ // Fetch templates from the DB (with or without body).
out, err := app.core.GetTemplates("", noBody)
if err != nil {
return err
@@ -61,8 +63,7 @@ func handleGetTemplates(c echo.Context) error {
// handlePreviewTemplate renders the HTML preview of a template.
func handlePreviewTemplate(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- id, _ = strconv.Atoi(c.Param("id"))
+ app = c.Get("app").(*App)
)
tpl := models.Template{
@@ -70,7 +71,7 @@ func handlePreviewTemplate(c echo.Context) error {
Body: c.FormValue("body"),
}
- // Body is posted.
+ // Body is posted with the request.
if tpl.Body != "" {
if tpl.Type == "" {
tpl.Type = models.TemplateTypeCampaign
@@ -81,7 +82,8 @@ func handlePreviewTemplate(c echo.Context) error {
app.i18n.Ts("templates.placeholderHelp", "placeholder", tplTag))
}
} else {
- // There is no body. Fetch the template.
+ // There is no body. Fetch the template from the DB.
+ id, _ := strconv.Atoi(c.Param("id"))
if id < 1 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
@@ -142,30 +144,28 @@ func handlePreviewTemplate(c echo.Context) error {
func handleCreateTemplate(c echo.Context) error {
var (
app = c.Get("app").(*App)
- o = models.Template{}
)
+ var o models.Template
if err := c.Bind(&o); err != nil {
return err
}
-
if err := validateTemplate(o, app); err != nil {
return err
}
- var f template.FuncMap
-
// Subject is only relevant for fixed tx templates. For campaigns,
// the subject changes per campaign and is on models.Campaign.
+ var funcs template.FuncMap
if o.Type == models.TemplateTypeCampaign {
o.Subject = ""
- f = app.manager.TemplateFuncs(nil)
+ funcs = app.manager.TemplateFuncs(nil)
} else {
- f = app.manager.GenericTemplateFuncs()
+ funcs = app.manager.GenericTemplateFuncs()
}
// Compile the template and validate.
- if err := o.Compile(f); err != nil {
+ if err := o.Compile(funcs); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
@@ -187,10 +187,10 @@ func handleCreateTemplate(c echo.Context) error {
// handleUpdateTemplate handles template modification.
func handleUpdateTemplate(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- id, _ = strconv.Atoi(c.Param("id"))
+ app = c.Get("app").(*App)
)
+ id, _ := strconv.Atoi(c.Param("id"))
if id < 1 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
@@ -199,27 +199,26 @@ func handleUpdateTemplate(c echo.Context) error {
if err := c.Bind(&o); err != nil {
return err
}
-
if err := validateTemplate(o, app); err != nil {
return err
}
- var f template.FuncMap
-
// Subject is only relevant for fixed tx templates. For campaigns,
// the subject changes per campaign and is on models.Campaign.
+ var funcs template.FuncMap
if o.Type == models.TemplateTypeCampaign {
o.Subject = ""
- f = app.manager.TemplateFuncs(nil)
+ funcs = app.manager.TemplateFuncs(nil)
} else {
- f = app.manager.GenericTemplateFuncs()
+ funcs = app.manager.GenericTemplateFuncs()
}
// Compile the template and validate.
- if err := o.Compile(f); err != nil {
+ if err := o.Compile(funcs); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
+ // Update the template in the DB.
out, err := app.core.UpdateTemplate(id, o.Name, o.Subject, []byte(o.Body))
if err != nil {
return err
@@ -237,14 +236,15 @@ func handleUpdateTemplate(c echo.Context) error {
// handleTemplateSetDefault handles template modification.
func handleTemplateSetDefault(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- id, _ = strconv.Atoi(c.Param("id"))
+ app = c.Get("app").(*App)
)
+ id, _ := strconv.Atoi(c.Param("id"))
if id < 1 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
+ // Update the template in the DB.
if err := app.core.SetDefaultTemplate(id); err != nil {
return err
}
@@ -255,14 +255,15 @@ func handleTemplateSetDefault(c echo.Context) error {
// handleDeleteTemplate handles template deletion.
func handleDeleteTemplate(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- id, _ = strconv.Atoi(c.Param("id"))
+ app = c.Get("app").(*App)
)
+ id, _ := strconv.Atoi(c.Param("id"))
if id < 1 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
+ // Delete the template from the DB.
if err := app.core.DeleteTemplate(id); err != nil {
return err
}
diff --git a/cmd/tx.go b/cmd/tx.go
index 743fcba2..33c81a96 100644
--- a/cmd/tx.go
+++ b/cmd/tx.go
@@ -17,9 +17,10 @@ import (
func handleSendTxMessage(c echo.Context) error {
var (
app = c.Get("app").(*App)
- m models.TxMessage
)
+ var m models.TxMessage
+
// If it's a multipart form, there may be file attachments.
if strings.HasPrefix(c.Request().Header.Get("Content-Type"), "multipart/form-data") {
form, err := c.MultipartForm()
@@ -30,8 +31,7 @@ func handleSendTxMessage(c echo.Context) error {
data, ok := form.Value["data"]
if !ok || len(data) != 1 {
- return echo.NewHTTPError(http.StatusBadRequest,
- app.i18n.Ts("globals.messages.invalidFields", "name", "data"))
+ return echo.NewHTTPError(http.StatusBadRequest, app.i18n.Ts("globals.messages.invalidFields", "name", "data"))
}
// Parse the JSON data.
@@ -66,7 +66,7 @@ func handleSendTxMessage(c echo.Context) error {
return err
}
- // Validate input.
+ // Validate fields.
if r, err := validateTxMessage(m, app); err != nil {
return err
} else {
@@ -90,7 +90,7 @@ func handleSendTxMessage(c echo.Context) error {
}
notFound := []string{}
- for n := 0; n < num; n++ {
+ for n := range num {
var (
subID int
subEmail string
@@ -160,6 +160,7 @@ func handleSendTxMessage(c echo.Context) error {
return c.JSON(http.StatusOK, okResp{true})
}
+// validateTxMessage validates the tx message fields.
func validateTxMessage(m models.TxMessage, app *App) (models.TxMessage, error) {
if len(m.SubscriberEmails) > 0 && m.SubscriberEmail != "" {
return m, echo.NewHTTPError(http.StatusBadRequest,
diff --git a/cmd/users.go b/cmd/users.go
index 22bca298..dee0ccab 100644
--- a/cmd/users.go
+++ b/cmd/users.go
@@ -20,33 +20,37 @@ var (
// handleGetUsers retrieves users.
func handleGetUsers(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- userID, _ = strconv.Atoi(c.Param("id"))
+ app = c.Get("app").(*App)
)
- // Fetch one.
- single := false
+ var (
+ single = false
+ userID, _ = strconv.Atoi(c.Param("id"))
+ )
if userID > 0 {
single = true
}
if single {
+ // Get the user from the DB.
out, err := app.core.GetUser(userID, "", "")
if err != nil {
return err
}
+ // Blank out the password hash respond to the API.
out.Password = null.String{}
return c.JSON(http.StatusOK, okResp{out})
}
- // Get all users.
+ // Get all users from the DB.
out, err := app.core.GetUsers()
if err != nil {
return err
}
+ // Blank out password hashes before respond to the API.
for n := range out {
out[n].Password = null.String{}
}
@@ -58,9 +62,9 @@ func handleGetUsers(c echo.Context) error {
func handleCreateUser(c echo.Context) error {
var (
app = c.Get("app").(*App)
- u = auth.User{}
)
+ var u = auth.User{}
if err := c.Bind(&u); err != nil {
return err
}
@@ -93,16 +97,18 @@ func handleCreateUser(c echo.Context) error {
u.Name = u.Username
}
- // Create the user in the database.
+ // Create the user in the DB.
user, err := app.core.CreateUser(u)
if err != nil {
return err
}
+
+ // Blank out the password hash before responding to the API.
if user.Type != auth.UserTypeAPI {
user.Password = null.String{}
}
- // Cache the API token for validating API queries without hitting the DB every time.
+ // Cache the API token for in-memory, off-DB /api/* request auth.
if _, err := cacheUsers(app.core, app.auth); err != nil {
return err
}
@@ -113,10 +119,10 @@ func handleCreateUser(c echo.Context) error {
// handleUpdateUser handles user modification.
func handleUpdateUser(c echo.Context) error {
var (
- app = c.Get("app").(*App)
- id, _ = strconv.Atoi(c.Param("id"))
+ app = c.Get("app").(*App)
)
+ id, _ := strconv.Atoi(c.Param("id"))
if id < 1 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
@@ -127,7 +133,6 @@ func handleUpdateUser(c echo.Context) error {
return err
}
- // Validate.
u.Username = strings.TrimSpace(u.Username)
u.Name = strings.TrimSpace(u.Name)
email := strings.TrimSpace(u.Email.String)
@@ -144,17 +149,20 @@ func handleUpdateUser(c echo.Context) error {
if !utils.ValidateEmail(email) {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.Ts("globals.messages.invalidFields", "name", "email"))
}
+
+ // Validate password if password login is enabled.
if u.PasswordLogin && u.Password.String != "" {
if !strHasLen(u.Password.String, 8, stdInputMaxLen) {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.Ts("globals.messages.invalidFields", "name", "password"))
}
if u.Password.String != "" {
+ // If a password is sent, validate it before updating in the DB. If it's not set, leave the password in the DB untouched.
if !strHasLen(u.Password.String, 8, stdInputMaxLen) {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.Ts("globals.messages.invalidFields", "name", "password"))
}
} else {
- // Get the existing user for password validation.
+ // Get the user from the DB.
user, err := app.core.GetUser(id, "", "")
if err != nil {
return err
@@ -171,6 +179,7 @@ func handleUpdateUser(c echo.Context) error {
u.Email = null.String{String: email, Valid: true}
}
+ // Default the name to username if not set.
if u.Name == "" {
u.Name = u.Username
}
@@ -181,10 +190,10 @@ func handleUpdateUser(c echo.Context) error {
return err
}
- // Clear the pasword before sending outside.
+ // Blank out the password hash before responding to the API.
user.Password = null.String{}
- // Cache the API token for validating API queries without hitting the DB every time.
+ // Cache the API token for in-memory, off-DB /api/* request auth.
if _, err := cacheUsers(app.core, app.auth); err != nil {
return err
}
@@ -195,24 +204,26 @@ func handleUpdateUser(c echo.Context) error {
// handleDeleteUsers handles user deletion, either a single one (ID in the URI), or a list.
func handleDeleteUsers(c echo.Context) error {
var (
- app = c.Get("app").(*App)
+ app = c.Get("app").(*App)
+ )
+
+ var (
id, _ = strconv.ParseInt(c.Param("id"), 10, 64)
ids []int
)
-
if id < 1 && len(ids) == 0 {
return echo.NewHTTPError(http.StatusBadRequest, app.i18n.T("globals.messages.invalidID"))
}
-
if id > 0 {
ids = append(ids, int(id))
}
+ // Delete the user(s) from the DB.
if err := app.core.DeleteUsers(ids); err != nil {
return err
}
- // Cache the API token for validating API queries without hitting the DB every time.
+ // Cache the API token for in-memory, off-DB /api/* request auth.
if _, err := cacheUsers(app.core, app.auth); err != nil {
return err
}
@@ -225,6 +236,8 @@ func handleGetUserProfile(c echo.Context) error {
var (
user = auth.GetUser(c)
)
+
+ // Blank out the password hash before responding to the API.
user.Password.String = ""
user.Password.Valid = false
@@ -238,6 +251,7 @@ func handleUpdateUserProfile(c echo.Context) error {
user = auth.GetUser(c)
)
+ // Incoming params.
u := auth.User{}
if err := c.Bind(&u); err != nil {
return err
@@ -260,10 +274,13 @@ func handleUpdateUserProfile(c echo.Context) error {
}
}
+ // Update the user in the DB.
out, err := app.core.UpdateUserProfile(user.ID, u)
if err != nil {
return err
}
+
+ // Blank out the password hash before responding to the API.
out.Password = null.String{}
return c.JSON(http.StatusOK, okResp{out})
@@ -273,14 +290,14 @@ func handleUpdateUserProfile(c echo.Context) error {
// It also returns a bool indicating whether there are any actual users in the DB at all,
// which if there aren't, the first time user setup needs to be run.
func cacheUsers(co *core.Core, a *auth.Auth) (bool, error) {
- allUsers, err := co.GetUsers()
+ users, err := co.GetUsers()
if err != nil {
return false, err
}
hasUser := false
- apiUsers := make([]auth.User, 0, len(allUsers))
- for _, u := range allUsers {
+ apiUsers := make([]auth.User, 0, len(users))
+ for _, u := range users {
if u.Type == auth.UserTypeAPI && u.Status == auth.UserStatusEnabled {
apiUsers = append(apiUsers, u)
}
diff --git a/cmd/utils.go b/cmd/utils.go
index ebbf7e46..9c55822c 100644
--- a/cmd/utils.go
+++ b/cmd/utils.go
@@ -5,6 +5,7 @@ import (
"fmt"
"path/filepath"
"regexp"
+ "slices"
"strconv"
"strings"
)
@@ -15,12 +16,7 @@ var (
// inArray checks if a string is present in a list of strings.
func inArray(val string, vals []string) (ok bool) {
- for _, v := range vals {
- if v == val {
- return true
- }
- }
- return false
+ return slices.Contains(vals, val)
}
// makeFilename sanitizes a filename (user supplied upload filenames).
diff --git a/frontend/cypress.config.js b/frontend/cypress.config.js
index 0a068027..8fa37681 100644
--- a/frontend/cypress.config.js
+++ b/frontend/cypress.config.js
@@ -13,6 +13,7 @@ module.exports = defineConfig({
viewportWidth: 1400,
viewportHeight: 950,
e2e: {
+ experimentalRunAllSpecs: true,
testIsolation: false,
experimentalSessionAndOrigin: false,
// We've imported your old cypress plugins here.
diff --git a/frontend/src/api/index.js b/frontend/src/api/index.js
index a083ff60..8abb08b9 100644
--- a/frontend/src/api/index.js
+++ b/frontend/src/api/index.js
@@ -443,87 +443,87 @@ export const deleteGCSubscriptions = async (beforeDate) => http.delete(
export const getUsers = () => http.get(
'/api/users',
{
- loading: auth.Users,
- store: auth.Users,
+ loading: models.users,
+ store: models.users,
},
);
export const queryUsers = () => http.get(
'/api/users',
{
- loading: auth.Users,
- store: auth.Users,
+ loading: models.users,
+ store: models.users,
},
);
export const getUser = async (id) => http.get(
`/api/users/${id}`,
- { loading: auth.Users },
+ { loading: models.users },
);
export const createUser = (data) => http.post(
'/api/users',
data,
- { loading: auth.Users },
+ { loading: models.users },
);
export const updateUser = (data) => http.put(
`/api/users/${data.id}`,
data,
- { loading: auth.Users },
+ { loading: models.users },
);
export const deleteUser = (id) => http.delete(
`/api/users/${id}`,
- { loading: auth.Users },
+ { loading: models.users },
);
export const getUserProfile = () => http.get(
'/api/profile',
- { loading: auth.Users, store: models.profile },
+ { loading: models.users, store: models.profile },
);
export const updateUserProfile = (data) => http.put(
'/api/profile',
data,
- { loading: auth.Users, store: models.profile },
+ { loading: models.users, store: models.profile },
);
export const getUserRoles = async () => http.get(
'/api/roles/users',
- { loading: auth.UserRoles, store: auth.UserRoles },
+ { loading: models.userRoles, store: models.userRoles },
);
export const getListRoles = async () => http.get(
'/api/roles/lists',
- { loading: auth.ListRoles, store: auth.ListRoles },
+ { loading: models.listRoles, store: models.listRoles },
);
export const createUserRole = (data) => http.post(
'/api/roles/users',
data,
- { loading: auth.UserRoles },
+ { loading: models.userRoles },
);
export const createListRole = (data) => http.post(
'/api/roles/lists',
data,
- { loading: auth.ListRoles },
+ { loading: models.listRoles },
);
export const updateUserRole = (data) => http.put(
`/api/roles/users/${data.id}`,
data,
- { loading: auth.UserRoles },
+ { loading: models.userRoles },
);
export const updateListRole = (data) => http.put(
`/api/roles/lists/${data.id}`,
data,
- { loading: auth.UserRoles },
+ { loading: models.userRoles },
);
export const deleteRole = (id) => http.delete(
`/api/roles/${id}`,
- { loading: auth.UserRoles },
+ { loading: models.userRoles },
);
diff --git a/frontend/src/store/index.js b/frontend/src/store/index.js
index f2c6e901..a5bfeb3d 100644
--- a/frontend/src/store/index.js
+++ b/frontend/src/store/index.js
@@ -41,10 +41,10 @@ export default new Vuex.Store({
[models.campaigns]: (state) => state[models.campaigns],
[models.media]: (state) => state[models.media],
[models.templates]: (state) => state[models.templates],
- [auth.Users]: (state) => state[auth.Users],
+ [models.users]: (state) => state[models.users],
[models.profile]: (state) => state[models.profile],
- [auth.UserRoles]: (state) => state[auth.UserRoles],
- [auth.ListRoles]: (state) => state[auth.ListRoles],
+ [models.userRoles]: (state) => state[models.userRoles],
+ [models.listRoles]: (state) => state[models.listRoles],
[models.settings]: (state) => state[models.settings],
[models.serverConfig]: (state) => state[models.serverConfig],
[models.logs]: (state) => state[models.logs],
diff --git a/internal/auth/auth.go b/internal/auth/auth.go
index c82d0bdc..4bf7af9b 100644
--- a/internal/auth/auth.go
+++ b/internal/auth/auth.go
@@ -20,11 +20,6 @@ import (
"golang.org/x/oauth2"
)
-const (
- sessTypeNative = "native"
- sessTypeOIDC = "oidc"
-)
-
type OIDCclaim struct {
Email string `json:"email"`
EmailVerified bool `json:"email_verified"`
@@ -53,8 +48,8 @@ type Config struct {
// Callbacks takes two callback functions required by simplesessions.
type Callbacks struct {
- SetCookie func(cookie *http.Cookie, w interface{}) error
- GetCookie func(name string, r interface{}) (*http.Cookie, error)
+ SetCookie func(cookie *http.Cookie, w any) error
+ GetCookie func(name string, r any) (*http.Cookie, error)
GetUser func(id int) (User, error)
}
@@ -72,6 +67,9 @@ type Auth struct {
log *log.Logger
}
+var sessPruneInterval = time.Hour * 12
+
+// New returns an initialize Auth instance.
func New(cfg Config, db *sql.DB, cb *Callbacks, lo *log.Logger) (*Auth, error) {
a := &Auth{
cfg: cfg,
@@ -125,7 +123,7 @@ func New(cfg Config, db *sql.DB, cb *Callbacks, lo *log.Logger) (*Auth, error) {
if err := st.Prune(); err != nil {
lo.Printf("error pruning login sessions: %v", err)
}
- time.Sleep(time.Hour * 12)
+ time.Sleep(sessPruneInterval)
}()
return a, nil
@@ -136,12 +134,12 @@ func New(cfg Config, db *sql.DB, cb *Callbacks, lo *log.Logger) (*Auth, error) {
// in the database in one shot.
func (o *Auth) CacheAPIUsers(users []User) {
o.Lock()
- o.apiUsers = map[string]User{}
+ defer o.Unlock()
+ o.apiUsers = map[string]User{}
for _, u := range users {
o.apiUsers[u.Username] = u
}
- o.Unlock()
}
// CacheAPIUser caches an API user for authenticating requests.
@@ -265,6 +263,7 @@ func (o *Auth) Middleware(next echo.HandlerFunc) echo.HandlerFunc {
}
}
+// Perm is an HTTP handler middleware that checks if the authenticated user has the required permissions.
func (o *Auth) Perm(next echo.HandlerFunc, perms ...string) echo.HandlerFunc {
return func(c echo.Context) error {
u, ok := c.Get(UserHTTPCtxKey).(User)
@@ -306,7 +305,7 @@ func (o *Auth) SaveSession(u User, oidcToken string, c echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError, "error creating session")
}
- if err := sess.SetMulti(map[string]interface{}{"user_id": u.ID, "oidc_token": oidcToken}); err != nil {
+ if err := sess.SetMulti(map[string]any{"user_id": u.ID, "oidc_token": oidcToken}); err != nil {
o.log.Printf("error setting login session: %v", err)
return echo.NewHTTPError(http.StatusInternalServerError, "error creating session")
}
@@ -314,9 +313,10 @@ func (o *Auth) SaveSession(u User, oidcToken string, c echo.Context) error {
return nil
}
+// validateSession checks if the cookie session is valid (in the DB) and returns the session and user details.
func (o *Auth) validateSession(c echo.Context) (*simplesessions.Session, User, error) {
// Cookie session.
- sess, err := o.sess.Acquire(nil, c, c)
+ sess, err := o.sess.Acquire(context.TODO(), c, c)
if err != nil {
return nil, User{}, echo.NewHTTPError(http.StatusForbidden, err.Error())
}
diff --git a/internal/bounce/bounce.go b/internal/bounce/bounce.go
index 932f6ba2..9a49b108 100644
--- a/internal/bounce/bounce.go
+++ b/internal/bounce/bounce.go
@@ -11,13 +11,6 @@ import (
"github.com/knadh/listmonk/models"
)
-const (
- // subID is the identifying subscriber ID header to look for in
- // bounced e-mails.
- subID = "X-Listmonk-Subscriber"
- campID = "X-Listmonk-Campaign"
-)
-
// Mailbox represents a POP/IMAP mailbox client that can scan messages and pass
// them to a given channel.
type Mailbox interface {
@@ -118,20 +111,13 @@ func (m *Manager) Run() {
go m.runMailboxScanner()
}
- for {
- select {
- case b, ok := <-m.queue:
- if !ok {
- return
- }
-
- if b.CreatedAt.IsZero() {
- b.CreatedAt = time.Now()
- }
+ for b := range m.queue {
+ if b.CreatedAt.IsZero() {
+ b.CreatedAt = time.Now()
+ }
- if err := m.opt.RecordBounceCB(b); err != nil {
- continue
- }
+ if err := m.opt.RecordBounceCB(b); err != nil {
+ continue
}
}
}
@@ -149,8 +135,6 @@ func (m *Manager) runMailboxScanner() {
// Record records a new bounce event given the subscriber's email or UUID.
func (m *Manager) Record(b models.Bounce) error {
- select {
- case m.queue <- b:
- }
+ m.queue <- b
return nil
}
diff --git a/internal/bounce/webhooks/forwardemail.go b/internal/bounce/webhooks/forwardemail.go
index 59c90222..5603ed88 100644
--- a/internal/bounce/webhooks/forwardemail.go
+++ b/internal/bounce/webhooks/forwardemail.go
@@ -14,12 +14,12 @@ import (
)
type BounceDetails struct {
- Action string `json:"action"`
- Message string `json:"message"`
- Category string `json:"category"`
- Code int `json:"code"`
- Status interface{} `json:"status"`
- Line int `json:"line"`
+ Action string `json:"action"`
+ Message string `json:"message"`
+ Category string `json:"category"`
+ Code int `json:"code"`
+ Status any `json:"status"`
+ Line int `json:"line"`
}
type forwardemailNotif struct {
diff --git a/internal/buflog/buflog.go b/internal/buflog/buflog.go
index d9d64bf9..e533c49c 100644
--- a/internal/buflog/buflog.go
+++ b/internal/buflog/buflog.go
@@ -29,13 +29,14 @@ func New(maxLines int) *BufLog {
// using LIFO.
func (bu *BufLog) Write(b []byte) (n int, err error) {
bu.Lock()
+ defer bu.Unlock()
+
if len(bu.lines) >= bu.maxLines {
bu.lines[0] = ""
bu.lines = bu.lines[1:len(bu.lines)]
}
-
bu.lines = append(bu.lines, strings.TrimSpace(string(b)))
- bu.Unlock()
+
return len(b), nil
}
diff --git a/internal/core/campaigns.go b/internal/core/campaigns.go
index 32b17902..fc01bb77 100644
--- a/internal/core/campaigns.go
+++ b/internal/core/campaigns.go
@@ -89,7 +89,7 @@ func (c *Core) GetArchivedCampaign(id int, uuid, archiveSlug string) (models.Cam
// the archive template is returned.
func (c *Core) getCampaign(id int, uuid, archiveSlug string, tplType string) (models.Campaign, error) {
// Unsafe to ignore scanning fields not present in models.Campaigns.
- var uu interface{}
+ var uu any
if uuid != "" {
uu = uuid
}
diff --git a/internal/core/lists.go b/internal/core/lists.go
index 9848a0ba..b3b94001 100644
--- a/internal/core/lists.go
+++ b/internal/core/lists.go
@@ -76,7 +76,7 @@ func (c *Core) QueryLists(searchStr, typ, optin string, tags []string, orderBy,
// GetList gets a list by its ID or UUID.
func (c *Core) GetList(id int, uuid string) (models.List, error) {
- var uu interface{}
+ var uu any
if uuid != "" {
uu = uuid
}
@@ -123,10 +123,10 @@ func (c *Core) GetListsByOptin(ids []int, optinType string) ([]models.List, erro
// otherwise, they have UUIDs as the keys.
// Note: This is a really weird and awkward API. Ideally, Go Generics
// should've somehow supported generic struct methods.
-func (c *Core) GetListTypes(ids []int, uuids []string) (map[interface{}]string, error) {
+func (c *Core) GetListTypes(ids []int, uuids []string) (map[any]string, error) {
res := []listType{}
- out := map[interface{}]string{}
+ out := map[any]string{}
if err := c.q.GetListTypes.Select(&res, pq.Array(ids), pq.StringArray(uuids)); err != nil {
c.log.Printf("error fetching list types: %v", err)
return nil, echo.NewHTTPError(http.StatusInternalServerError,
diff --git a/internal/core/media.go b/internal/core/media.go
index f8c5201a..2e9256de 100644
--- a/internal/core/media.go
+++ b/internal/core/media.go
@@ -45,7 +45,7 @@ func (c *Core) QueryMedia(provider string, s media.Store, query string, offset,
// GetMedia returns a media item.
func (c *Core) GetMedia(id int, uuid, fileName string, s media.Store) (media.Media, error) {
- var uu interface{}
+ var uu any
if uuid != "" {
uu = uuid
}
diff --git a/internal/core/subscribers.go b/internal/core/subscribers.go
index 7d354222..742f1196 100644
--- a/internal/core/subscribers.go
+++ b/internal/core/subscribers.go
@@ -17,7 +17,7 @@ import (
// GetSubscriber fetches a subscriber by one of the given params.
func (c *Core) GetSubscriber(id int, uuid, email string) (models.Subscriber, error) {
- var uu interface{}
+ var uu any
if uuid != "" {
uu = uuid
}
@@ -121,9 +121,7 @@ func (c *Core) QuerySubscribers(query string, listIDs []int, subStatus string, o
}
// Run the query again and fetch the actual data. stmt is the raw SQL query.
- out := models.Subscribers{}
- stmt := fmt.Sprintf(c.q.QuerySubscribersCount, cond)
- stmt = strings.ReplaceAll(c.q.QuerySubscribers, "%query%", cond)
+ stmt := strings.ReplaceAll(c.q.QuerySubscribers, "%query%", cond)
stmt = strings.ReplaceAll(stmt, "%order%", orderBy+" "+order)
tx, err := c.db.BeginTxx(context.Background(), &sql.TxOptions{ReadOnly: true})
@@ -133,6 +131,7 @@ func (c *Core) QuerySubscribers(query string, listIDs []int, subStatus string, o
}
defer tx.Rollback()
+ var out models.Subscribers
if err := tx.Select(&out, stmt, pq.Array(listIDs), subStatus, offset, limit); err != nil {
return nil, 0, echo.NewHTTPError(http.StatusInternalServerError,
c.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.subscribers}", "error", pqErrMsg(err)))
@@ -157,7 +156,7 @@ func (c *Core) GetSubscriberLists(subID int, uuid string, listIDs []int, listUUI
listUUIDs = []string{}
}
- var uu interface{}
+ var uu any
if uuid != "" {
uu = uuid
}
@@ -177,7 +176,7 @@ func (c *Core) GetSubscriberLists(subID int, uuid string, listIDs []int, listUUI
// Get the subscriber's data. A single query that gets the profile, list subscriptions, campaign views,
// and link clicks. Names of private lists are replaced with "Private list".
func (c *Core) GetSubscriberProfileForExport(id int, uuid string) (models.SubscriberExportProfile, error) {
- var uu interface{}
+ var uu any
if uuid != "" {
uu = uuid
}
@@ -204,8 +203,7 @@ func (c *Core) ExportSubscribers(query string, subIDs, listIDs []int, subStatus
cond = " AND " + query
}
- stmt := fmt.Sprintf(c.q.QuerySubscribersForExport, cond)
- stmt = strings.ReplaceAll(c.q.QuerySubscribersForExport, "%query%", cond)
+ stmt := strings.ReplaceAll(c.q.QuerySubscribersForExport, "%query%", cond)
// Verify that the arbitrary SQL search expression is read only.
if cond != "" {
@@ -484,7 +482,7 @@ func (c *Core) ConfirmOptionSubscription(subUUID string, listUUIDs []string, met
// DeleteSubscriberBounces deletes the given list of subscribers.
func (c *Core) DeleteSubscriberBounces(id int, uuid string) error {
- var uu interface{}
+ var uu any
if uuid != "" {
uu = uuid
}
diff --git a/internal/events/events.go b/internal/events/events.go
index 5c684c4e..de646a41 100644
--- a/internal/events/events.go
+++ b/internal/events/events.go
@@ -16,11 +16,11 @@ const (
// Event represents a single event in the system.
type Event struct {
- ID string `json:"id"`
- Type string `json:"type"`
- Message string `json:"message"`
- Data interface{} `json:"data"`
- Channels []string `json:"-"`
+ ID string `json:"id"`
+ Type string `json:"type"`
+ Message string `json:"message"`
+ Data any `json:"data"`
+ Channels []string `json:"-"`
}
type Events struct {
diff --git a/internal/i18n/i18n.go b/internal/i18n/i18n.go
index 08d7602a..440411b5 100644
--- a/internal/i18n/i18n.go
+++ b/internal/i18n/i18n.go
@@ -12,8 +12,8 @@ import (
// I18n offers translation functions over a language map.
type I18n struct {
- code string `json:"code"`
- name string `json:"name"`
+ Code string `json:"code"`
+ Name string `json:"name"`
langMap map[string]string
}
@@ -38,8 +38,8 @@ func New(b []byte) (*I18n, error) {
return &I18n{
langMap: l,
- code: code,
- name: name,
+ Code: code,
+ Name: name,
}, nil
}
@@ -58,16 +58,6 @@ func (i *I18n) Load(b []byte) error {
return nil
}
-// Name returns the canonical name of the language.
-func (i *I18n) Name() string {
- return i.name
-}
-
-// Code returns the ISO code of the language.
-func (i *I18n) Code() string {
- return i.code
-}
-
// JSON returns the languagemap as raw JSON.
func (i *I18n) JSON() []byte {
b, _ := json.Marshal(i.langMap)
diff --git a/internal/manager/manager.go b/internal/manager/manager.go
index 74e2a640..f40c665c 100644
--- a/internal/manager/manager.go
+++ b/internal/manager/manager.go
@@ -10,6 +10,8 @@ import (
"sync"
"time"
+ "maps"
+
"github.com/Masterminds/sprig/v3"
"github.com/knadh/listmonk/internal/i18n"
"github.com/knadh/listmonk/models"
@@ -140,11 +142,6 @@ type Config struct {
ScanCampaigns bool
}
-type msgError struct {
- st *pipe
- err error
-}
-
var pushTimeout = time.Second * 3
// New returns a new instance of Mailer.
@@ -186,6 +183,7 @@ func (m *Manager) AddMessenger(msg Messenger) error {
return fmt.Errorf("messenger '%s' is already loaded", id)
}
m.messengers[id] = msg
+
return nil
}
@@ -201,6 +199,7 @@ func (m *Manager) PushMessage(msg models.Message) error {
m.log.Printf("message push timed out: '%s'", msg.Subject)
return errors.New("message push timed out")
}
+
return nil
}
@@ -221,12 +220,14 @@ func (m *Manager) PushCampaignMessage(msg CampaignMessage) error {
m.log.Printf("message push timed out: '%s'", msg.Subject())
return errors.New("message push timed out")
}
+
return nil
}
// HasMessenger checks if a given messenger is registered.
func (m *Manager) HasMessenger(id string) bool {
_, ok := m.messengers[id]
+
return ok
}
@@ -234,6 +235,7 @@ func (m *Manager) HasMessenger(id string) bool {
func (m *Manager) HasRunningCampaigns() bool {
m.pipesMut.Lock()
defer m.pipesMut.Unlock()
+
return len(m.pipes) > 0
}
@@ -361,9 +363,7 @@ func (m *Manager) TemplateFuncs(c *models.Campaign) template.FuncMap {
},
}
- for k, v := range m.tplFuncs {
- f[k] = v
- }
+ maps.Copy(f, m.tplFuncs)
return f
}
@@ -394,33 +394,30 @@ func (m *Manager) scanCampaigns(tick time.Duration) {
t := time.NewTicker(tick)
defer t.Stop()
- for {
- select {
- // Periodically scan the data source for campaigns to process.
- case <-t.C:
- ids, counts := m.getCurrentCampaigns()
- campaigns, err := m.store.NextCampaigns(ids, counts)
+ // Periodically scan the data source for campaigns to process.
+ for range t.C {
+ ids, counts := m.getCurrentCampaigns()
+ campaigns, err := m.store.NextCampaigns(ids, counts)
+ if err != nil {
+ m.log.Printf("error fetching campaigns: %v", err)
+ continue
+ }
+
+ for _, c := range campaigns {
+ // Create a new pipe that'll handle this campaign's states.
+ p, err := m.newPipe(c)
if err != nil {
- m.log.Printf("error fetching campaigns: %v", err)
+ m.log.Printf("error processing campaign (%s): %v", c.Name, err)
continue
}
+ m.log.Printf("start processing campaign (%s)", c.Name)
- for _, c := range campaigns {
- // Create a new pipe that'll handle this campaign's states.
- p, err := m.newPipe(c)
- if err != nil {
- m.log.Printf("error processing campaign (%s): %v", c.Name, err)
- continue
- }
- m.log.Printf("start processing campaign (%s)", c.Name)
-
- // If subscriber processing is busy, move on. Blocking and waiting
- // can end up in a race condition where the waiting campaign's
- // state in the data source has changed.
- select {
- case m.nextPipes <- p:
- default:
- }
+ // If subscriber processing is busy, move on. Blocking and waiting
+ // can end up in a race condition where the waiting campaign's
+ // state in the data source has changed.
+ select {
+ case m.nextPipes <- p:
+ default:
}
}
}
@@ -484,8 +481,10 @@ func (m *Manager) worker() {
}
}
+ // Set the headers.
out.Headers = h
+ // Push the message to the messenger.
err := m.messengers[msg.Campaign.Messenger].Push(out)
if err != nil {
m.log.Printf("error sending message in campaign %s: subscriber %d: %v", msg.Campaign.Name, msg.Subscriber.ID, err)
@@ -497,6 +496,8 @@ func (m *Manager) worker() {
msg.pipe.wg.Done()
if err != nil {
+ // Call the error callback, which keeps track of the error count
+ // and stops the campaign if the error count exceeds the threshold.
msg.pipe.OnError()
} else {
id := uint64(msg.Subscriber.ID)
@@ -514,26 +515,14 @@ func (m *Manager) worker() {
return
}
- err := m.messengers[msg.Messenger].Push(msg)
- if err != nil {
+ // Push the message to the messenger.
+ if err := m.messengers[msg.Messenger].Push(msg); err != nil {
m.log.Printf("error sending message '%s': %v", msg.Subject, err)
}
}
}
}
-// getRunningCampaignIDs returns the IDs of campaigns currently being processed.
-func (m *Manager) getRunningCampaignIDs() []int64 {
- // Needs to return an empty slice in case there are no campaigns.
- m.pipesMut.RLock()
- ids := make([]int64, 0, len(m.pipes))
- for _, p := range m.pipes {
- ids = append(ids, int64(p.camp.ID))
- }
- m.pipesMut.RUnlock()
- return ids
-}
-
// getCurrentCampaigns returns the IDs of campaigns currently being processed
// and their sent counts.
func (m *Manager) getCurrentCampaigns() ([]int64, []int64) {
@@ -557,14 +546,6 @@ func (m *Manager) getCurrentCampaigns() ([]int64, []int64) {
return ids, counts
}
-// isCampaignProcessing checks if the campaign is being processed.
-func (m *Manager) isCampaignProcessing(id int) bool {
- m.pipesMut.RLock()
- _, ok := m.pipes[id]
- m.pipesMut.RUnlock()
- return ok
-}
-
// trackLink register a URL and return its UUID to be used in message templates
// for tracking links.
func (m *Manager) trackLink(url, campUUID, subUUID string) string {
@@ -597,7 +578,7 @@ func (m *Manager) trackLink(url, campUUID, subUUID string) string {
func (m *Manager) sendNotif(c *models.Campaign, status, reason string) error {
var (
subject = fmt.Sprintf("%s: %s", cases.Title(language.Und).String(status), c.Name)
- data = map[string]interface{}{
+ data = map[string]any{
"ID": c.ID,
"Name": c.Name,
"Status": status,
@@ -606,11 +587,14 @@ func (m *Manager) sendNotif(c *models.Campaign, status, reason string) error {
"Reason": reason,
}
)
+
return m.notifCB(subject, data)
}
+// makeGnericFuncMap returns a generic template func map with custom template
+// functions and sprig template functions.
func (m *Manager) makeGnericFuncMap() template.FuncMap {
- f := template.FuncMap{
+ funcs := template.FuncMap{
"Date": func(layout string) string {
if layout == "" {
layout = time.ANSIC
@@ -625,13 +609,14 @@ func (m *Manager) makeGnericFuncMap() template.FuncMap {
},
}
- for k, v := range sprig.GenericFuncMap() {
- f[k] = v
- }
+ // Copy spring functions.
+ maps.Copy(funcs, sprig.GenericFuncMap())
- return f
+ return funcs
}
+// attachMedia loads any media/attachments from the media store and attaches
+// the byte blobs to the campaign.
func (m *Manager) attachMedia(c *models.Campaign) error {
// Load any media/attachments.
for _, mid := range []int64(c.MediaIDs) {
diff --git a/internal/manager/pipe.go b/internal/manager/pipe.go
index 0ab3b16f..57a3a51e 100644
--- a/internal/manager/pipe.go
+++ b/internal/manager/pipe.go
@@ -105,7 +105,7 @@ func (p *pipe) NextSubscribers() (bool, error) {
// Check if the sliding window is active.
if hasSliding {
- diff := time.Now().Sub(p.m.slidingStart)
+ diff := time.Since(p.m.slidingStart)
// Window has expired. Reset the clock.
if diff >= p.m.cfg.SlidingWindowDuration {
@@ -134,6 +134,8 @@ func (p *pipe) NextSubscribers() (bool, error) {
return true, nil
}
+// OnError keeps track of the number of errors that occur while sending messages
+// and pauses the campaign if the error threshold is met.
func (p *pipe) OnError() {
if p.m.cfg.MaxSendErrors < 1 {
return
@@ -177,6 +179,8 @@ func (p *pipe) newMessage(s models.Subscriber) (CampaignMessage, error) {
return msg, nil
}
+// cleanup finishes the campaign and updates the campaign status in the DB
+// and also triggers a notification to the admin.
func (p *pipe) cleanup() {
defer func() {
p.m.pipesMut.Lock()
diff --git a/internal/media/providers/filesystem/filesystem.go b/internal/media/providers/filesystem/filesystem.go
index e953eaab..0bad1f53 100644
--- a/internal/media/providers/filesystem/filesystem.go
+++ b/internal/media/providers/filesystem/filesystem.go
@@ -30,20 +30,21 @@ func New(opts Opts) (media.Store, error) {
// Put accepts the filename, the content type and file object itself and stores the file in disk.
func (c *Client) Put(filename string, cType string, src io.ReadSeeker) (string, error) {
- var out *os.File
-
// Get the directory path
dir := getDir(c.opts.UploadPath)
- o, err := os.OpenFile(filepath.Join(dir, filename), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0664)
+
+ // Read the file contents.
+ out, err := os.OpenFile(filepath.Join(dir, filename), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0664)
if err != nil {
return "", err
}
- out = o
defer out.Close()
+ // Copy it to the target location.
if _, err := io.Copy(out, src); err != nil {
return "", err
}
+
return filename, nil
}
@@ -61,11 +62,9 @@ func (c *Client) GetBlob(url string) ([]byte, error) {
// Delete accepts a filename and removes it from disk.
func (c *Client) Delete(file string) error {
dir := getDir(c.opts.UploadPath)
+
err := os.Remove(filepath.Join(dir, file))
- if err != nil {
- return err
- }
- return nil
+ return err
}
// getDir returns the current working directory path if no directory is specified,
@@ -74,5 +73,6 @@ func getDir(dir string) string {
if dir == "" {
dir, _ = os.Getwd()
}
+
return dir
}
diff --git a/internal/media/providers/s3/s3.go b/internal/media/providers/s3/s3.go
index 2bf7eee0..8aa99640 100644
--- a/internal/media/providers/s3/s3.go
+++ b/internal/media/providers/s3/s3.go
@@ -98,6 +98,7 @@ func (c *Client) GetURL(name string) string {
Timestamp: time.Now(),
ExpirySeconds: int(c.opts.Expiry.Seconds()),
})
+
return u
}
@@ -114,6 +115,7 @@ func (c *Client) GetBlob(uurl string) ([]byte, error) {
uurl = filepath.Base(p.Path)
}
+ // Download the file from S3.
file, err := c.s3.FileDownload(simples3.DownloadInput{
Bucket: c.opts.Bucket,
ObjectKey: c.makeBucketPath(filepath.Base(uurl)),
@@ -122,6 +124,7 @@ func (c *Client) GetBlob(uurl string) ([]byte, error) {
return nil, err
}
+ // Read it into a byte blob.
b, err := io.ReadAll(file)
if err != nil {
return nil, err
@@ -137,6 +140,7 @@ func (c *Client) Delete(name string) error {
Bucket: c.opts.Bucket,
ObjectKey: c.makeBucketPath(name),
})
+
return err
}
diff --git a/internal/messenger/email/email.go b/internal/messenger/email/email.go
index 0e0d4262..0ae15c76 100644
--- a/internal/messenger/email/email.go
+++ b/internal/messenger/email/email.go
@@ -22,7 +22,7 @@ const (
// Server represents an SMTP server's credentials.
type Server struct {
- // Name is unique identifier for the server.
+ // Name is a unique identifier for the server.
Name string `json:"name"`
Username string `json:"username"`
Password string `json:"password"`
@@ -33,6 +33,7 @@ type Server struct {
// Rest of the options are embedded directly from the smtppool lib.
// The JSON tag is for config unmarshal to work.
+ //lint:ignore SA5008 ,squash is needed by koanf/mapstructure config unmarshal.
smtppool.Opt `json:",squash"`
pool *smtppool.Pool
@@ -55,6 +56,7 @@ func New(name string, servers ...Server) (*Emailer, error) {
for _, srv := range servers {
s := srv
+
var auth smtp.Auth
switch s.AuthProtocol {
case "cram":
@@ -134,6 +136,7 @@ func (e *Emailer) Push(m models.Message) error {
}
}
+ // Create the email.
em := smtppool.Email{
From: m.From,
To: m.To,
diff --git a/internal/messenger/postback/postback.go b/internal/messenger/postback/postback.go
index c99ec742..9fb6fbeb 100644
--- a/internal/messenger/postback/postback.go
+++ b/internal/messenger/postback/postback.go
@@ -149,6 +149,7 @@ func (p *Postback) Flush() error {
// Close closes idle HTTP connections.
func (p *Postback) Close() error {
p.c.CloseIdleConnections()
+
return nil
}
@@ -192,6 +193,7 @@ func (p *Postback) exec(method, rURL string, reqBody []byte, headers http.Header
req.URL.RawQuery = string(reqBody)
}
+ // Execute the request.
r, err := p.c.Do(req)
if err != nil {
return err
diff --git a/internal/migrations/v4.0.0.go b/internal/migrations/v4.0.0.go
index 918b8def..0eec621e 100644
--- a/internal/migrations/v4.0.0.go
+++ b/internal/migrations/v4.0.0.go
@@ -125,9 +125,7 @@ func V4_0_0(db *sqlx.DB, fs stuffbin.FileSystem, ko *koanf.Koanf, lo *log.Logger
perms := []string{}
for _, group := range permGroups {
- for _, p := range group.Permissions {
- perms = append(perms, p)
- }
+ perms = append(perms, group.Permissions...)
}
if _, err := db.Exec(`INSERT INTO roles (type, name, permissions) VALUES('user', 'Super Admin', $1) ON CONFLICT DO NOTHING`, pq.Array(perms)); err != nil {
return err
diff --git a/internal/subimporter/importer.go b/internal/subimporter/importer.go
index 41164b97..542dec2e 100644
--- a/internal/subimporter/importer.go
+++ b/internal/subimporter/importer.go
@@ -31,9 +31,6 @@ import (
)
const (
- // stdInputMaxLen is the maximum allowed length for a standard input field.
- stdInputMaxLen = 200
-
// commitBatchSize is the number of inserts to commit in a single SQL transaction.
commitBatchSize = 10000
)
@@ -191,6 +188,7 @@ func (im *Importer) NewSession(opt SessionOpt) (*Session, error) {
func (im *Importer) GetStats() Status {
im.RLock()
defer im.RUnlock()
+
return Status{
Name: im.status.Name,
Status: im.status.Status,
@@ -207,6 +205,7 @@ func (im *Importer) GetLogs() []byte {
if im.status.logBuf == nil {
return []byte{}
}
+
return im.status.logBuf.Bytes()
}
@@ -233,6 +232,7 @@ func (im *Importer) isDone() bool {
s = false
}
im.RUnlock()
+
return s
}
@@ -270,13 +270,10 @@ func (s *Session) Start() {
err error
total = 0
cur = 0
-
- listIDs = make([]int, len(s.opt.ListIDs))
)
- for i, v := range s.opt.ListIDs {
- listIDs[i] = v
- }
+ listIDs := make([]int, len(s.opt.ListIDs))
+ copy(listIDs, s.opt.ListIDs)
for sub := range s.subQueue {
if cur == 0 {
@@ -354,6 +351,7 @@ func (s *Session) Start() {
if _, err := s.im.opt.UpdateListDateStmt.Exec(pq.Array(listIDs)); err != nil {
s.log.Printf("error updating lists date: %v", err)
}
+
s.im.sendNotif(StatusFinished)
}
@@ -476,8 +474,8 @@ func (s *Session) LoadCSV(srcPath string, delim rune) error {
return errors.New("empty file")
}
- s.im.Lock()
// Exclude the header from count.
+ s.im.Lock()
s.im.status.Total = numLines - 1
s.im.Unlock()
@@ -575,6 +573,7 @@ func (s *Session) LoadCSV(srcPath string, delim rune) error {
close(s.subQueue)
failed = false
+
return nil
}
@@ -584,6 +583,7 @@ func (im *Importer) Stop() {
im.Lock()
im.status = Status{Status: StatusNone}
im.Unlock()
+
return
}
diff --git a/models/models.go b/models/models.go
index bdba0fe4..f0cfaa73 100644
--- a/models/models.go
+++ b/models/models.go
@@ -99,7 +99,7 @@ var regTplFuncs = []regTplFunc{
// and substituting it with {{ Track "http://link.com" . }} (the dot context)
// before compilation. This is to make linking easier for users.
{
- regExp: regexp.MustCompile("{{(\\s+)?TrackLink(\\s+)?(.+?)(\\s+)?}}"),
+ regExp: regexp.MustCompile(`{{(\\s+)?TrackLink(\\s+)?(.+?)(\\s+)?}}`),
replace: `{{ TrackLink $3 . }}`,
},
@@ -121,11 +121,11 @@ var regTplFuncs = []regTplFunc{
// AdminNotifCallback is a callback function that's called
// when a campaign's status changes.
-type AdminNotifCallback func(subject string, data interface{}) error
+type AdminNotifCallback func(subject string, data any) error
// PageResults is a generic HTTP response container for paginated results of list of items.
type PageResults struct {
- Results interface{} `json:"results"`
+ Results any `json:"results"`
Query string `json:"query"`
Total int `json:"total"`
@@ -174,7 +174,7 @@ type SubscriberExportProfile struct {
}
// JSON is the wrapper for reading and writing arbitrary JSONB fields from the DB.
-type JSON map[string]interface{}
+type JSON map[string]any
// StringIntMap is used to define DB Scan()s.
type StringIntMap map[string]int
@@ -379,12 +379,12 @@ type TxMessage struct {
SubscriberEmail string `json:"subscriber_email"`
SubscriberID int `json:"subscriber_id"`
- TemplateID int `json:"template_id"`
- Data map[string]interface{} `json:"data"`
- FromEmail string `json:"from_email"`
- Headers Headers `json:"headers"`
- ContentType string `json:"content_type"`
- Messenger string `json:"messenger"`
+ TemplateID int `json:"template_id"`
+ Data map[string]any `json:"data"`
+ FromEmail string `json:"from_email"`
+ Headers Headers `json:"headers"`
+ ContentType string `json:"content_type"`
+ Messenger string `json:"messenger"`
// File attachments added from multi-part form data.
Attachments []Attachment `json:"-"`
@@ -455,7 +455,7 @@ func (s JSON) Value() (driver.Value, error) {
}
// Scan unmarshals JSONB from the DB.
-func (s JSON) Scan(src interface{}) error {
+func (s JSON) Scan(src any) error {
if src == nil {
s = make(JSON)
return nil
@@ -468,7 +468,7 @@ func (s JSON) Scan(src interface{}) error {
}
// Scan unmarshals JSONB from the DB.
-func (s StringIntMap) Scan(src interface{}) error {
+func (s StringIntMap) Scan(src any) error {
if src == nil {
s = make(StringIntMap)
return nil
@@ -524,7 +524,7 @@ func (c *Campaign) CompileTemplate(f template.FuncMap) error {
subj = r.regExp.ReplaceAllString(subj, r.replace)
}
- var txtFuncs map[string]interface{} = f
+ var txtFuncs map[string]any = f
subjTpl, err := txttpl.New(ContentTpl).Funcs(txtFuncs).Parse(subj)
if err != nil {
return fmt.Errorf("error compiling subject: %v", err)
@@ -689,7 +689,7 @@ func (s Subscriber) LastName() string {
}
// Scan implements the sql.Scanner interface.
-func (h *Headers) Scan(src interface{}) error {
+func (h *Headers) Scan(src any) error {
var b []byte
switch src := src.(type) {
case []byte:
diff --git a/models/queries.go b/models/queries.go
index cc3c86ac..f08cd6a7 100644
--- a/models/queries.go
+++ b/models/queries.go
@@ -157,7 +157,7 @@ func (q *Queries) CompileSubscriberQueryTpl(exp string, db *sqlx.DB, subStatus s
// compileSubscriberQueryTpl takes an arbitrary WHERE expressions and a subscriber
// query template that depends on the filter (eg: delete by query, blocklist by query etc.)
// combines and executes them.
-func (q *Queries) ExecSubQueryTpl(exp, tpl string, listIDs []int, db *sqlx.DB, subStatus string, args ...interface{}) error {
+func (q *Queries) ExecSubQueryTpl(exp, tpl string, listIDs []int, db *sqlx.DB, subStatus string, args ...any) error {
// Perform a dry run.
filterExp, err := q.CompileSubscriberQueryTpl(exp, db, subStatus)
if err != nil {
@@ -169,7 +169,7 @@ func (q *Queries) ExecSubQueryTpl(exp, tpl string, listIDs []int, db *sqlx.DB, s
}
// First argument is the boolean indicating if the query is a dry run.
- a := append([]interface{}{false, pq.Array(listIDs), subStatus}, args...)
+ a := append([]any{false, pq.Array(listIDs), subStatus}, args...)
if _, err := db.Exec(fmt.Sprintf(tpl, filterExp), a...); err != nil {
return err
}