Skip to content

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

Fix DB state issues in visual campaign cloning.

Kailash Nadh20e4b100cac3
diff --git a/cmd/campaigns.go b/cmd/campaigns.go
index ef007879..9c730a28 100644
--- a/cmd/campaigns.go
+++ b/cmd/campaigns.go
@@ -88,6 +88,7 @@ func (a *App) GetCampaigns(c echo.Context) error {
if noBody {
for i := range res {
res[i].Body = ""
+ res[i].BodySource.Valid = false
}
}
@@ -565,6 +566,16 @@ func (a *App) validateCampaignFields(c campReq) (campReq, error) {
return c, errors.New(a.i18n.T("campaigns.fieldInvalidSubject"))
}
+ // If no content-type is specified, default to richtext.
+ if c.ContentType != models.CampaignContentTypeRichtext &&
+ c.ContentType != models.CampaignContentTypeHTML &&
+ c.ContentType != models.CampaignContentTypePlain &&
+ c.ContentType != models.CampaignContentTypeVisual &&
+ c.ContentType != models.CampaignContentTypeMarkdown {
+ c.ContentType = models.CampaignContentTypeRichtext
+ c.BodySource.Valid = false
+ }
+
// If there's a "send_at" date, it should be in the future.
if c.SendAt.Valid {
if c.SendAt.Time.Before(time.Now()) {
diff --git a/cmd/init.go b/cmd/init.go
index eb2d0a79..0ddc1b6b 100644
--- a/cmd/init.go
+++ b/cmd/init.go
@@ -316,7 +316,7 @@ func initDB() *sqlx.DB {
db.SetMaxIdleConns(c.MaxIdle)
db.SetConnMaxLifetime(c.MaxLifetime)
- return db
+ return db.Unsafe()
}
// readQueries reads named SQL queries from the SQL queries file into a query map.
@@ -358,7 +358,7 @@ func prepareQueries(qMap goyesql.Queries, db *sqlx.DB, ko *koanf.Koanf) *models.
// Scan and prepare all queries.
var q models.Queries
- if err := goyesqlx.ScanToStruct(&q, qMap, db.Unsafe()); err != nil {
+ if err := goyesqlx.ScanToStruct(&q, qMap, db); err != nil {
lo.Fatalf("error preparing SQL queries: %v", err)
}
diff --git a/frontend/src/views/Campaigns.vue b/frontend/src/views/Campaigns.vue
index c7021e78..357e6dc1 100644
--- a/frontend/src/views/Campaigns.vue
+++ b/frontend/src/views/Campaigns.vue
@@ -422,6 +422,7 @@ export default Vue.extend({
tags: c.tags,
template_id: c.templateId,
body: c.body,
+ body_source: c.bodySource,
altbody: c.altbody,
headers: c.headers,
send_later: sendLater,
diff --git a/internal/core/campaigns.go b/internal/core/campaigns.go
index 733af39d..80511778 100644
--- a/internal/core/campaigns.go
+++ b/internal/core/campaigns.go
@@ -42,7 +42,7 @@ func (c *Core) QueryCampaigns(searchStr string, statuses, tags []string, orderBy
c.i18n.Ts("globals.messages.errorFetching", "name", "{globals.terms.campaign}", "error", pqErrMsg(err)))
}
- for i := 0; i < len(out); i++ {
+ for i := range out {
// Replace null tags.
if out[i].Tags == nil {
out[i].Tags = []string{}
@@ -211,12 +211,6 @@ func (c *Core) CreateCampaign(o models.Campaign, listIDs []int, mediaIDs []int)
// UpdateCampaign updates a campaign.
func (c *Core) UpdateCampaign(id int, o models.Campaign, listIDs []int, mediaIDs []int) (models.Campaign, error) {
- // If it's a visual campain, no template ID should be saved.
- if o.ContentType == models.CampaignContentTypeVisual {
- o.TemplateID.Valid = false
- o.ArchiveTemplateID.Valid = false
- }
-
_, err := c.q.UpdateCampaign.Exec(id,
o.Name,
o.Subject,
diff --git a/queries.sql b/queries.sql
index 9bb694f2..e24755b8 100644
--- a/queries.sql
+++ b/queries.sql
@@ -514,8 +514,10 @@ WITH tpl AS (
FROM templates
WHERE
CASE
- WHEN $13::INT IS NOT NULL AND $13::INT != 0 THEN id = $13::INT
- ELSE is_default = TRUE
+ -- If a template ID is present, use it. If not, use the default template only if
+ -- it's not a visual template.
+ WHEN $13::INT IS NOT NULL THEN id = $13::INT
+ ELSE $8 != 'visual' AND is_default = TRUE
END
LIMIT 1
),
@@ -541,7 +543,7 @@ camp AS (
-- body
COALESCE(NULLIF($6, ''), (SELECT body FROM tpl), ''),
$7,
- COALESCE(NULLIF($8, ''), (SELECT content_type FROM tpl), 'richtext')::content_type,
+ $8::content_type,
$9, $10, $11, $12,
(SELECT id FROM tpl),
(SELECT to_send FROM counts),
@@ -551,7 +553,7 @@ camp AS (
$17,
$18,
-- body_source
- COALESCE(NULLIF($20, ''), (SELECT body_source FROM tpl))
+ COALESCE($20, (SELECT body_source FROM tpl))
RETURNING id
),
med AS (
@@ -571,11 +573,7 @@ SELECT id FROM camp;
-- there's a COUNT() OVER() that still returns the total result count
-- for pagination in the frontend, albeit being a field that'll repeat
-- with every resultant row.
-SELECT c.id, c.uuid, c.name, c.subject, c.from_email,
- c.messenger, c.started_at, c.to_send, c.sent, c.type,
- c.body, c.altbody, c.send_at, c.headers, c.status, c.content_type, c.tags,
- c.template_id, c.archive, c.archive_slug, c.archive_template_id, c.archive_meta,
- c.created_at, c.updated_at,
+SELECT c.*,
COUNT(*) OVER () AS total,
(
SELECT COALESCE(ARRAY_TO_JSON(ARRAY_AGG(l)), '[]') FROM (
@@ -888,10 +886,11 @@ WITH camp AS (
headers=$9,
tags=$10::VARCHAR(100)[],
messenger=$11,
- template_id=$12,
+ -- template_id shouldn't be saved for visual campaigns.
+ template_id=(CASE WHEN $7::content_type = 'visual' THEN NULL ELSE $12::INT END),
archive=$14,
archive_slug=$15,
- archive_template_id=$16,
+ archive_template_id=(CASE WHEN $7::content_type = 'visual' THEN NULL ELSE $16::INT END),
archive_meta=$17,
body_source=$19,
updated_at=NOW()