Skip to content

Commit 73e07e9

Browse files
committed
feat(notifications): improve logo_url and app_name fetching moving it to notifications package
1 parent b552267 commit 73e07e9

File tree

3 files changed

+30
-32
lines changed

3 files changed

+30
-32
lines changed

cli/server.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,36 +1309,6 @@ 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-
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
1314-
defer cancel()
1315-
1316-
logoURL, err := options.Database.GetLogoURL(ctx)
1317-
if err != nil {
1318-
if errors.Is(err, sql.ErrNoRows) {
1319-
return notifications.NotificationsDefaultLogoURL
1320-
}
1321-
1322-
return ""
1323-
}
1324-
1325-
return logoURL
1326-
},
1327-
"app_name": func() string {
1328-
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
1329-
defer cancel()
1330-
1331-
appName, err := options.Database.GetApplicationName(ctx)
1332-
if err != nil {
1333-
if errors.Is(err, sql.ErrNoRows) {
1334-
return notifications.NotificationsDefaultAppName
1335-
}
1336-
1337-
return ""
1338-
}
1339-
1340-
return appName
1341-
},
13421312
}
13431313
}
13441314

coderd/notifications/notifier.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package notifications
22

33
import (
44
"context"
5+
"database/sql"
56
"encoding/json"
7+
"errors"
68
"sync"
79
"text/template"
810

@@ -23,8 +25,8 @@ import (
2325
)
2426

2527
const (
26-
NotificationsDefaultLogoURL = "https://coder.com/coder-logo-horizontal.png"
27-
NotificationsDefaultAppName = "Coder"
28+
notificationsDefaultLogoURL = "https://coder.com/coder-logo-horizontal.png"
29+
notificationsDefaultAppName = "Coder"
2830
)
2931

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

233+
appName, err := n.store.GetApplicationName(ctx)
234+
if err != nil {
235+
if !errors.Is(err, sql.ErrNoRows) {
236+
return nil, xerrors.Errorf("get application name: %w", err)
237+
}
238+
appName = notificationsDefaultAppName
239+
}
240+
241+
logoURL, err := n.store.GetLogoURL(ctx)
242+
if err != nil {
243+
if !errors.Is(err, sql.ErrNoRows) {
244+
return nil, xerrors.Errorf("get logo URL: %w", err)
245+
}
246+
logoURL = notificationsDefaultLogoURL
247+
}
248+
249+
helpers := make(template.FuncMap)
250+
for k, v := range n.helpers {
251+
helpers[k] = v
252+
}
253+
254+
helpers["app_name"] = func() string { return appName }
255+
helpers["logo_url"] = func() string { return logoURL }
256+
231257
var title, body string
232258
if title, err = render.GoTemplate(msg.TitleTemplate, payload, n.helpers); err != nil {
233259
return nil, xerrors.Errorf("render title: %w", err)

coderd/notifications/spec.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ type Store interface {
2222
FetchNewMessageMetadata(ctx context.Context, arg database.FetchNewMessageMetadataParams) (database.FetchNewMessageMetadataRow, error)
2323
GetNotificationMessagesByStatus(ctx context.Context, arg database.GetNotificationMessagesByStatusParams) ([]database.NotificationMessage, error)
2424
GetNotificationsSettings(ctx context.Context) (string, error)
25+
GetApplicationName(ctx context.Context) (string, error)
26+
GetLogoURL(ctx context.Context) (string, error)
2527
}
2628

2729
// Handler is responsible for preparing and delivering a notification by a given method.

0 commit comments

Comments
 (0)