feat(coderd): add company logo when available for email notifications (#14935)

This PR aims to close #14253 

We keep the default behavior using the Coder logo if there's no logo
set.
Otherwise we want to use the logo based on the URL set in appearance.

---------

Co-authored-by: defelmnq <yvincent@coder.com>
This commit is contained in:
Vincent Vielle
2024-10-22 14:06:19 +02:00
committed by GitHub
co-authored by defelmnq
parent c42f487668
commit 297089e944
16 changed files with 289 additions and 52 deletions
+57
View File
@@ -0,0 +1,57 @@
package notifications
import (
"context"
"database/sql"
"errors"
"text/template"
"golang.org/x/xerrors"
"cdr.dev/slog"
)
func (n *notifier) fetchHelpers(ctx context.Context) (map[string]any, error) {
appName, err := n.fetchAppName(ctx)
if err != nil {
n.log.Error(ctx, "failed to fetch app name", slog.Error(err))
return nil, xerrors.Errorf("fetch app name: %w", err)
}
logoURL, err := n.fetchLogoURL(ctx)
if err != nil {
n.log.Error(ctx, "failed to fetch logo URL", slog.Error(err))
return nil, xerrors.Errorf("fetch logo URL: %w", err)
}
helpers := make(template.FuncMap)
for k, v := range n.helpers {
helpers[k] = v
}
helpers["app_name"] = func() string { return appName }
helpers["logo_url"] = func() string { return logoURL }
return helpers, nil
}
func (n *notifier) fetchAppName(ctx context.Context) (string, error) {
appName, err := n.store.GetApplicationName(ctx)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return notificationsDefaultAppName, nil
}
return "", xerrors.Errorf("get application name: %w", err)
}
return appName, nil
}
func (n *notifier) fetchLogoURL(ctx context.Context) (string, error) {
logoURL, err := n.store.GetLogoURL(ctx)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return notificationsDefaultLogoURL, nil
}
return "", xerrors.Errorf("get logo URL: %w", err)
}
return logoURL, nil
}