Skip to content

Commit

Permalink
feat: add welcome email
Browse files Browse the repository at this point in the history
  • Loading branch information
taciturnaxolotl committed Oct 3, 2024
1 parent b7ed219 commit 6aee74e
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
2 changes: 2 additions & 0 deletions routes/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ func (h *LoginHandler) PostSignup(w http.ResponseWriter, r *http.Request) {
return
}

h.mailSrvc.SendWelcome(user)

// Check if submitted with admin token in authorization header
authHeader := r.Header.Get("Authorization")
if authHeader == "Bearer "+h.config.Security.AdminToken {
Expand Down
23 changes: 23 additions & 0 deletions services/mail/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
)

const (
tplNameWelcome = "welcome"
tplNamePasswordReset = "reset_password"
tplNameImportNotification = "import_finished"
tplNameWakatimeFailureNotification = "wakatime_connection_failure"
Expand Down Expand Up @@ -60,6 +61,20 @@ func NewMailService() services.IMailService {
return &MailService{sendingService: sendingService, config: config, templates: templates}
}

func (m *MailService) SendWelcome(recipient *models.User) error {
tpl, err := m.getWelcomeTemplate(WelcomeTplData{Name: recipient.Name, Email: recipient.Email, Id: recipient.ID})
if err != nil {
return err
}
mail := &models.Mail{
From: models.MailAddress(m.config.Mail.Sender),
To: models.MailAddresses([]models.MailAddress{models.MailAddress(recipient.Email)}),
Subject: subjectPasswordReset,
}
mail.WithHTML(tpl.String())
return m.sendingService.Send(mail)
}

func (m *MailService) SendPasswordReset(recipient *models.User, resetLink string) error {
tpl, err := m.getPasswordResetTemplate(PasswordResetTplData{ResetLink: resetLink})
if err != nil {
Expand Down Expand Up @@ -141,6 +156,14 @@ func (m *MailService) SendSubscriptionNotification(recipient *models.User, hasEx
return m.sendingService.Send(mail)
}

func (m *MailService) getWelcomeTemplate(data WelcomeTplData) (*bytes.Buffer, error) {
var rendered bytes.Buffer
if err := m.templates[m.fmtName(tplNameWelcome)].Execute(&rendered, data); err != nil {
return nil, err
}
return &rendered, nil
}

func (m *MailService) getPasswordResetTemplate(data PasswordResetTplData) (*bytes.Buffer, error) {
var rendered bytes.Buffer
if err := m.templates[m.fmtName(tplNamePasswordReset)].Execute(&rendered, data); err != nil {
Expand Down
6 changes: 6 additions & 0 deletions services/mail/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ package mail

import "github.com/hackclub/hackatime/models"

type WelcomeTplData struct {
Email string
Name string
Id string
}

type PasswordResetTplData struct {
ResetLink string
}
Expand Down
1 change: 1 addition & 0 deletions services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type IProjectLabelService interface {
}

type IMailService interface {
SendWelcome(*models.User) error
SendPasswordReset(*models.User, string) error
SendWakatimeFailureNotification(*models.User, int) error
SendImportNotification(*models.User, time.Duration, int) error
Expand Down
78 changes: 78 additions & 0 deletions views/mail/welcome.tpl.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<!DOCTYPE html>
<html lang="en">
<head>
{{ template "head.tpl.html" . }}
<style>
body {
text-align: center;
justify-content: center;
background-color: #d6d7d7;
font-family: sans-serif;
-webkit-font-smoothing: antialiased;
color: #2c240c;
font-size: 14px;
line-height: 1.4;
margin: 0;
padding: 0;
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%;
}

.content {
display: flex;
flex-direction: column;
align-items: center;
}

.main {
border-radius: 3px;
width: 100%;
padding: 20px;
}

.btn-primary {
display: inline-block;
background-color: #dd9821;
border: solid 1px #9b5f00;
color: #0e0901 !important;
border-radius: 5px;
box-sizing: border-box;
cursor: pointer;
text-decoration: none;
font-size: 14px;
font-weight: bold;
margin: 0;
padding: 12px 25px;
text-transform: capitalize;
border-color: #8f5d0c;
}
</style>
</head>
<body>
{{ template "theader.tpl.html" . }}

<main class="content">
<h1>Welcome to Hackatime {{ .Name }}</h1>
<p>Welcome to Hackatime! Your username is: {{ .Id }}</p>
<p>
If you don't know your password then change it below otherwise
you can login at
<a href="https://{{ .PublicUrl }}/login"
>{{ .PublicUrl }}/login</a
>
</p>
<a
href="https://{{ .PublicUrl }}/reset-password"
target="_blank"
class="btn-primary"
>Change your password here</a
>
<p>
If you did not signup for hackatime then something weird is
happening (you are auto signed up when your join low-skies btw)
</p>
</main>

{{ template "tfooter.tpl.html" . }}
</body>
</html>

0 comments on commit 6aee74e

Please sign in to comment.