Skip to content

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

Fix incorrect template states in DB in campaign creation and broken preview.

Kailash Nadhf1fbadf6a692
diff --git a/cmd/campaigns.go b/cmd/campaigns.go
index ad33995c..ef007879 100644
--- a/cmd/campaigns.go
+++ b/cmd/campaigns.go
@@ -147,12 +147,12 @@ func (a *App) PreviewCampaign(c echo.Context) error {
contentType = c.FormValue("content_type")
tplID, _ = strconv.Atoi(c.FormValue("template_id"))
)
- // For visual content type don't use the template for preview, use only body.
- if contentType == models.CampaignContentTypeVisual && tplID < 1 {
+ // For visual content, template ID for previewing is irrelevant.
+ if contentType == models.CampaignContentTypeVisual || tplID < 1 {
tplID = 0
}
- // Get the campaign from the DB for previewing.
+ // Get the campaign from the DB for previewing with the `template_body` field.
camp, err := a.core.GetCampaignForPreview(id, tplID)
if err != nil {
return err
@@ -162,6 +162,11 @@ func (a *App) PreviewCampaign(c echo.Context) error {
if isPost {
camp.ContentType = contentType
camp.Body = c.FormValue("body")
+
+ // For visual campaigns, template body from the DB shouldn't be used.
+ if contentType == models.CampaignContentTypeVisual {
+ camp.TemplateBody = ""
+ }
}
// Use a dummy campaign ID to prevent views and clicks from {{ TrackView }}
@@ -181,6 +186,7 @@ func (a *App) PreviewCampaign(c echo.Context) error {
a.i18n.Ts("templates.errorRendering", "error", err.Error()))
}
+ // Plaintext headers for plain body.
if camp.ContentType == models.CampaignContentTypePlain {
return c.String(http.StatusOK, string(msg.Body()))
}
diff --git a/frontend/src/views/Templates.vue b/frontend/src/views/Templates.vue
index c19a0fd1..552357b3 100644
--- a/frontend/src/views/Templates.vue
+++ b/frontend/src/views/Templates.vue
@@ -34,7 +34,8 @@
<b-tag v-if="props.row.type === 'campaign'" :class="props.row.type" :data-cy="`type-${props.row.type}`">
{{ $tc('templates.typeCampaignHTML') }}
</b-tag>
- <b-tag v-else-if="props.row.type === 'campaign_visual'" :class="props.row.type" :data-cy="`type-${props.row.type}`">
+ <b-tag v-else-if="props.row.type === 'campaign_visual'" :class="props.row.type"
+ :data-cy="`type-${props.row.type}`">
{{ $tc('templates.typeCampaignVisual') }}
</b-tag>
<b-tag v-else :class="props.row.type" :data-cy="`type-${props.row.type}`">
diff --git a/i18n/en.json b/i18n/en.json
index fbeb23aa..cc0080fa 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -611,8 +611,8 @@
"templates.preview": "Preview",
"templates.rawHTML": "Raw HTML",
"templates.subject": "Subject",
- "templates.typeCampaignHTML": "Campaign (HTML)",
- "templates.typeCampaignVisual": "Campaign (Visual)",
+ "templates.typeCampaignHTML": "Campaign / HTML",
+ "templates.typeCampaignVisual": "Campaign / Visual",
"templates.typeTransactional": "Transactional",
"users.apiOneTimeToken": "Copy the API access token now. It will not be shown again.",
"users.cantDeleteRole": "Cannot delete role that is in use.",
diff --git a/internal/core/campaigns.go b/internal/core/campaigns.go
index f3fdb87e..733af39d 100644
--- a/internal/core/campaigns.go
+++ b/internal/core/campaigns.go
@@ -126,7 +126,7 @@ func (c *Core) getCampaign(id int, uuid, archiveSlug string, tplType string) (mo
// GetCampaignForPreview retrieves a campaign with a template body. If the optional tplID is > 0
// that particular template is used, otherwise, the template saved on the campaign is.
-func (c *Core) GetCampaignForPreview(id int, tplID int) (models.Campaign, error) {
+func (c *Core) GetCampaignForPreview(id, tplID int) (models.Campaign, error) {
var out models.Campaign
if err := c.q.GetCampaignForPreview.Get(&out, id, tplID); err != nil {
if err == sql.ErrNoRows {
@@ -183,11 +183,11 @@ func (c *Core) CreateCampaign(o models.Campaign, listIDs []int, mediaIDs []int)
o.Headers,
pq.StringArray(normalizeTags(o.Tags)),
o.Messenger,
- o.TemplateID.Int,
+ o.TemplateID,
pq.Array(listIDs),
o.Archive,
o.ArchiveSlug,
- o.ArchiveTemplateID.Int,
+ o.ArchiveTemplateID,
o.ArchiveMeta,
pq.Array(mediaIDs),
o.BodySource,
@@ -211,6 +211,12 @@ 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 2a9cff4e..9bb694f2 100644
--- a/queries.sql
+++ b/queries.sql
@@ -502,18 +502,20 @@ DELETE FROM lists WHERE id = ALL($1);
-- This creates the campaign and inserts campaign_lists relationships.
WITH tpl AS (
-- Select the template for the given template ID or use the default template.
- SELECT id,
- CASE WHEN type = 'campaign_visual' THEN body ELSE '' END AS body,
- CASE WHEN type = 'campaign_visual' THEN body_source ELSE '' END AS body_source,
- CASE
- WHEN type = 'campaign_visual' THEN 'visual'
- ELSE 'richtext'
- END AS content_type
+ SELECT
+ -- If the template is a visual template, then use it's HTML body as the campaign
+ -- body and its block source as the campaign's block source,
+ -- and don't set a template_id in the campaigns table, as it's essentially an
+ -- HTML template body "import" during creation.
+ (CASE WHEN type = 'campaign_visual' THEN NULL ELSE id END) AS id,
+ (CASE WHEN type = 'campaign_visual' THEN body ELSE '' END) AS body,
+ (CASE WHEN type = 'campaign_visual' THEN body_source ELSE '' END) AS body_source,
+ (CASE WHEN type = 'campaign_visual' THEN 'visual' ELSE 'richtext' END) AS content_type
FROM templates
WHERE
CASE
WHEN $13::INT IS NOT NULL AND $13::INT != 0 THEN id = $13::INT
- WHEN $13::INT = 0 THEN is_default = TRUE
+ ELSE is_default = TRUE
END
LIMIT 1
),
@@ -536,7 +538,7 @@ camp AS (
content_type, send_at, headers, tags, messenger, template_id, to_send,
max_subscriber_id, archive, archive_slug, archive_template_id, archive_meta, body_source)
SELECT $1, $2, $3, $4, $5,
- -- Body
+ -- body
COALESCE(NULLIF($6, ''), (SELECT body FROM tpl), ''),
$7,
COALESCE(NULLIF($8, ''), (SELECT content_type FROM tpl), 'richtext')::content_type,
@@ -545,10 +547,10 @@ camp AS (
(SELECT to_send FROM counts),
(SELECT max_sub_id FROM counts),
$15, $16,
- -- Template ID
- COALESCE(NULLIF($17, 0), (SELECT id FROM tpl)),
+ -- archive_template_id
+ $17,
$18,
- -- Body source
+ -- body_source
COALESCE(NULLIF($20, ''), (SELECT body_source FROM tpl))
RETURNING id
),
@@ -663,7 +665,7 @@ LEFT JOIN bounces AS b ON (b.campaign_id = id)
ORDER BY ARRAY_POSITION($1, id);
-- name: get-campaign-for-preview
-SELECT campaigns.*, COALESCE(templates.body, (SELECT body FROM templates WHERE is_default = true LIMIT 1)) AS template_body,
+SELECT campaigns.*, COALESCE(templates.body, '') AS template_body,
(
SELECT COALESCE(ARRAY_TO_JSON(ARRAY_AGG(l)), '[]') FROM (
SELECT COALESCE(campaign_lists.list_id, 0) AS id,