Skip to content

Commit 1b1a4c4

Browse files
committed
feat(notification): move logo_url and app_name logic to helpers functions
1 parent 2def52e commit 1b1a4c4

File tree

6 files changed

+35
-18
lines changed

6 files changed

+35
-18
lines changed

cli/server.go

+24
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,30 @@ func templateHelpers(options *coderd.Options) map[string]any {
13091309
return map[string]any{
13101310
"base_url": func() string { return options.AccessURL.String() },
13111311
"current_year": func() string { return strconv.Itoa(time.Now().Year()) },
1312+
"logo_url": func() string {
1313+
logoURL, err := options.Database.GetLogoURL(context.Background())
1314+
if err != nil {
1315+
if errors.Is(err, sql.ErrNoRows) {
1316+
return notifications.NotificationsDefaultLogoURL
1317+
}
1318+
1319+
return ""
1320+
}
1321+
1322+
return logoURL
1323+
},
1324+
"app_name": func() string {
1325+
appName, err := options.Database.GetApplicationName(context.Background())
1326+
if err != nil {
1327+
if errors.Is(err, sql.ErrNoRows) {
1328+
return notifications.NotificationsDefaultAppName
1329+
}
1330+
1331+
return ""
1332+
}
1333+
1334+
return appName
1335+
},
13121336
}
13131337
}
13141338

coderd/notifications/dispatch/smtp/html.gotmpl

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
<body style="margin: 0; padding: 0; font-family: -apple-system, system-ui, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif; color: #020617; background: #f8fafc;">
99
<div style="max-width: 600px; margin: 20px auto; padding: 60px; border: 1px solid #e2e8f0; border-radius: 8px; background-color: #fff; text-align: left; font-size: 14px; line-height: 1.5;">
1010
<div style="text-align: center;">
11-
<img src="{{ .Labels._logo_url }}" alt="Company Logo" style="height: 40px;" />
11+
<img src="{{ logo_url }}" alt="Company Logo" style="height: 40px;" />
12+
<h1 style="text-align: center; font-size: 36px; font-weight: 400; margin: 8px 0 32px; line-height: 1.5;">
13+
{{ app_name }}
14+
</h1>
1215
</div>
1316
<h1 style="text-align: center; font-size: 24px; font-weight: 400; margin: 8px 0 32px; line-height: 1.5;">
1417
{{ .Labels._subject }}

coderd/notifications/dispatch/smtp_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ func TestSMTP(t *testing.T) {
445445
helpers := map[string]any{
446446
"base_url": func() string { return "http://test.com" },
447447
"current_year": func() string { return "2024" },
448+
"logo_url": func() string { return "https://logo.company" },
449+
"app_name": func() string { return "TestCompany" },
448450
}
449451
handler := dispatch.NewSMTPHandler(tc.cfg, helpers, logger.Named("smtp"))
450452

coderd/notifications/notifications_test.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,8 @@ func TestWebhookDispatch(t *testing.T) {
241241

242242
// WHEN: a notification is enqueued (including arbitrary labels)
243243
input := map[string]string{
244-
"a": "b",
245-
"c": "d",
246-
"_logo_url": notifications.NotificationsDefaultLogoURL,
244+
"a": "b",
245+
"c": "d",
247246
}
248247
msgID, err := enq.Enqueue(ctx, user.ID, notifications.TemplateWorkspaceDeleted, input, "test")
249248
require.NoError(t, err)

coderd/notifications/notifier.go

+1-14
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ package notifications
22

33
import (
44
"context"
5-
"database/sql"
65
"encoding/json"
7-
"errors"
86
"sync"
97
"text/template"
108

@@ -26,6 +24,7 @@ import (
2624

2725
const (
2826
NotificationsDefaultLogoURL = "https://coder.com/coder-logo-horizontal.png"
27+
NotificationsDefaultAppName = "Coder"
2928
)
3029

3130
// notifier is a consumer of the notifications_messages queue. It dequeues messages from that table and processes them
@@ -229,18 +228,6 @@ func (n *notifier) prepare(ctx context.Context, msg database.AcquireNotification
229228
return nil, xerrors.Errorf("failed to resolve handler %q", msg.Method)
230229
}
231230

232-
logoURL, err := n.store.GetLogoURL(ctx)
233-
if err != nil && !errors.Is(err, sql.ErrNoRows) {
234-
n.log.Error(ctx, "failed fetching logo url", slog.Error(err))
235-
}
236-
237-
if logoURL == "" {
238-
//nolint:ineffassign // define to default value if unable to fetch one from db
239-
logoURL = NotificationsDefaultLogoURL
240-
}
241-
242-
payload.Labels["_logo_url"] = logoURL
243-
244231
var title, body string
245232
if title, err = render.GoTemplate(msg.TitleTemplate, payload, n.helpers); err != nil {
246233
return nil, xerrors.Errorf("render title: %w", err)

coderd/notifications/utils_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ func defaultHelpers() map[string]any {
3939
return map[string]any{
4040
"base_url": func() string { return "http://test.com" },
4141
"current_year": func() string { return "2024" },
42+
"logo_url": func() string { return "https://logo.company" },
43+
"app_name": func() string { return "TestCompany" },
4244
}
4345
}
4446

0 commit comments

Comments
 (0)