mirror of
https://github.com/hrfee/jfa-go.git
synced 2024-12-23 01:20:11 +00:00
finish backend of custom emails
biggest bodge i've ever done but it works i guess.
This commit is contained in:
parent
eb406ef951
commit
cc4e12c405
181
api.go
181
api.go
@ -516,7 +516,7 @@ func (app *appContext) Announce(gc *gin.Context) {
|
|||||||
}
|
}
|
||||||
addresses = append(addresses, addr.(string))
|
addresses = append(addresses, addr.(string))
|
||||||
}
|
}
|
||||||
msg, err := app.email.constructAnnouncement(req.Subject, req.Message, app)
|
msg, err := app.email.constructTemplate(req.Subject, req.Message, app)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.err.Printf("Failed to construct announcement emails: %s", err)
|
app.err.Printf("Failed to construct announcement emails: %s", err)
|
||||||
respondBool(500, false, gc)
|
respondBool(500, false, gc)
|
||||||
@ -1286,9 +1286,99 @@ func (app *appContext) GetEmails(gc *gin.Context) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// @Summary Sets the corresponding custom email.
|
||||||
|
// @Produce json
|
||||||
|
// @Param customEmail body customEmail true "Content = email (in markdown)."
|
||||||
|
// @Success 200 {object} boolResponse
|
||||||
|
// @Failure 400 {object} boolResponse
|
||||||
|
// @Failure 500 {object} boolResponse
|
||||||
|
// @Router /config/emails/{id} [post]
|
||||||
|
// @tags Configuration
|
||||||
|
func (app *appContext) SetEmail(gc *gin.Context) {
|
||||||
|
var req customEmail
|
||||||
|
gc.BindJSON(&req)
|
||||||
|
id := gc.Param("id")
|
||||||
|
if req.Content == "" {
|
||||||
|
respondBool(400, false, gc)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if id == "UserCreated" {
|
||||||
|
app.storage.customEmails.UserCreated.Content = req.Content
|
||||||
|
app.storage.customEmails.UserCreated.Enabled = true
|
||||||
|
} else if id == "InviteExpiry" {
|
||||||
|
app.storage.customEmails.InviteExpiry.Content = req.Content
|
||||||
|
app.storage.customEmails.InviteExpiry.Enabled = true
|
||||||
|
} else if id == "PasswordReset" {
|
||||||
|
app.storage.customEmails.PasswordReset.Content = req.Content
|
||||||
|
app.storage.customEmails.PasswordReset.Enabled = true
|
||||||
|
} else if id == "UserDeleted" {
|
||||||
|
app.storage.customEmails.UserDeleted.Content = req.Content
|
||||||
|
app.storage.customEmails.UserDeleted.Enabled = true
|
||||||
|
} else if id == "InviteEmail" {
|
||||||
|
app.storage.customEmails.InviteEmail.Content = req.Content
|
||||||
|
app.storage.customEmails.InviteEmail.Enabled = true
|
||||||
|
} else if id == "WelcomeEmail" {
|
||||||
|
app.storage.customEmails.WelcomeEmail.Content = req.Content
|
||||||
|
app.storage.customEmails.WelcomeEmail.Enabled = true
|
||||||
|
} else if id == "EmailConfirmation" {
|
||||||
|
app.storage.customEmails.EmailConfirmation.Content = req.Content
|
||||||
|
app.storage.customEmails.EmailConfirmation.Enabled = true
|
||||||
|
} else {
|
||||||
|
respondBool(400, false, gc)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if app.storage.storeCustomEmails() != nil {
|
||||||
|
respondBool(500, false, gc)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
respondBool(200, true, gc)
|
||||||
|
}
|
||||||
|
|
||||||
|
// @Summary Enable/Disable custom email.
|
||||||
|
// @Produce json
|
||||||
|
// @Success 200 {object} boolResponse
|
||||||
|
// @Failure 400 {object} boolResponse
|
||||||
|
// @Failure 500 {object} boolResponse
|
||||||
|
// @Router /config/emails/{id}/{enable/disable} [post]
|
||||||
|
// @tags Configuration
|
||||||
|
func (app *appContext) SetEmailState(gc *gin.Context) {
|
||||||
|
id := gc.Param("id")
|
||||||
|
s := gc.Param("state")
|
||||||
|
enabled := false
|
||||||
|
if s == "enable" {
|
||||||
|
enabled = true
|
||||||
|
} else if s != "disable" {
|
||||||
|
respondBool(400, false, gc)
|
||||||
|
}
|
||||||
|
if id == "UserCreated" {
|
||||||
|
app.storage.customEmails.UserCreated.Enabled = enabled
|
||||||
|
} else if id == "InviteExpiry" {
|
||||||
|
app.storage.customEmails.InviteExpiry.Enabled = enabled
|
||||||
|
} else if id == "PasswordReset" {
|
||||||
|
app.storage.customEmails.PasswordReset.Enabled = enabled
|
||||||
|
} else if id == "UserDeleted" {
|
||||||
|
app.storage.customEmails.UserDeleted.Enabled = enabled
|
||||||
|
} else if id == "InviteEmail" {
|
||||||
|
app.storage.customEmails.InviteEmail.Enabled = enabled
|
||||||
|
} else if id == "WelcomeEmail" {
|
||||||
|
app.storage.customEmails.WelcomeEmail.Enabled = enabled
|
||||||
|
} else if id == "EmailConfirmation" {
|
||||||
|
app.storage.customEmails.EmailConfirmation.Enabled = enabled
|
||||||
|
} else {
|
||||||
|
respondBool(400, false, gc)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if app.storage.storeCustomEmails() != nil {
|
||||||
|
respondBool(500, false, gc)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
respondBool(200, true, gc)
|
||||||
|
}
|
||||||
|
|
||||||
// @Summary Returns the boilerplate email and list of used variables in it.
|
// @Summary Returns the boilerplate email and list of used variables in it.
|
||||||
// @Produce json
|
// @Produce json
|
||||||
// @Success 200 {object} emailDTO
|
// @Success 200 {object} customEmail
|
||||||
|
// @Failure 400 {object} boolResponse
|
||||||
// @Failure 500 {object} boolResponse
|
// @Failure 500 {object} boolResponse
|
||||||
// @Router /config/emails/{id} [get]
|
// @Router /config/emails/{id} [get]
|
||||||
// @tags Configuration
|
// @tags Configuration
|
||||||
@ -1297,78 +1387,119 @@ func (app *appContext) GetEmail(gc *gin.Context) {
|
|||||||
var content string
|
var content string
|
||||||
var err error
|
var err error
|
||||||
var msg *Email
|
var msg *Email
|
||||||
|
var variables []string
|
||||||
|
var writeVars func(variables []string)
|
||||||
|
newEmail := false
|
||||||
if id == "UserCreated" {
|
if id == "UserCreated" {
|
||||||
content = app.storage.customEmails.UserCreated
|
content = app.storage.customEmails.UserCreated.Content
|
||||||
if content == "" {
|
if content == "" {
|
||||||
|
newEmail = true
|
||||||
msg, err = app.email.constructCreated("", "", "", Invite{}, app, true)
|
msg, err = app.email.constructCreated("", "", "", Invite{}, app, true)
|
||||||
content = msg.text
|
content = msg.text
|
||||||
|
} else {
|
||||||
|
variables = app.storage.customEmails.UserCreated.Variables
|
||||||
}
|
}
|
||||||
|
writeVars = func(variables []string) { app.storage.customEmails.UserCreated.Variables = variables }
|
||||||
// app.storage.customEmails.UserCreated = content
|
// app.storage.customEmails.UserCreated = content
|
||||||
} else if id == "InviteExpiry" {
|
} else if id == "InviteExpiry" {
|
||||||
content = app.storage.customEmails.InviteExpiry
|
content = app.storage.customEmails.InviteExpiry.Content
|
||||||
if content == "" {
|
if content == "" {
|
||||||
|
newEmail = true
|
||||||
msg, err = app.email.constructExpiry("", Invite{}, app, true)
|
msg, err = app.email.constructExpiry("", Invite{}, app, true)
|
||||||
content = msg.text
|
content = msg.text
|
||||||
|
} else {
|
||||||
|
variables = app.storage.customEmails.InviteExpiry.Variables
|
||||||
}
|
}
|
||||||
|
writeVars = func(variables []string) { app.storage.customEmails.InviteExpiry.Variables = variables }
|
||||||
// app.storage.customEmails.InviteExpiry = content
|
// app.storage.customEmails.InviteExpiry = content
|
||||||
} else if id == "PasswordReset" {
|
} else if id == "PasswordReset" {
|
||||||
content = app.storage.customEmails.PasswordReset
|
content = app.storage.customEmails.PasswordReset.Content
|
||||||
if content == "" {
|
if content == "" {
|
||||||
|
newEmail = true
|
||||||
msg, err = app.email.constructReset(PasswordReset{}, app, true)
|
msg, err = app.email.constructReset(PasswordReset{}, app, true)
|
||||||
content = msg.text
|
content = msg.text
|
||||||
|
} else {
|
||||||
|
variables = app.storage.customEmails.PasswordReset.Variables
|
||||||
}
|
}
|
||||||
|
writeVars = func(variables []string) { app.storage.customEmails.PasswordReset.Variables = variables }
|
||||||
// app.storage.customEmails.PasswordReset = content
|
// app.storage.customEmails.PasswordReset = content
|
||||||
} else if id == "UserDeleted" {
|
} else if id == "UserDeleted" {
|
||||||
content = app.storage.customEmails.UserDeleted
|
content = app.storage.customEmails.UserDeleted.Content
|
||||||
if content == "" {
|
if content == "" {
|
||||||
|
newEmail = true
|
||||||
msg, err = app.email.constructDeleted("", app, true)
|
msg, err = app.email.constructDeleted("", app, true)
|
||||||
content = msg.text
|
content = msg.text
|
||||||
|
} else {
|
||||||
|
variables = app.storage.customEmails.UserDeleted.Variables
|
||||||
}
|
}
|
||||||
|
writeVars = func(variables []string) { app.storage.customEmails.UserDeleted.Variables = variables }
|
||||||
// app.storage.customEmails.UserDeleted = content
|
// app.storage.customEmails.UserDeleted = content
|
||||||
} else if id == "InviteEmail" {
|
} else if id == "InviteEmail" {
|
||||||
content = app.storage.customEmails.InviteEmail
|
content = app.storage.customEmails.InviteEmail.Content
|
||||||
if content == "" {
|
if content == "" {
|
||||||
|
newEmail = true
|
||||||
msg, err = app.email.constructInvite("", Invite{}, app, true)
|
msg, err = app.email.constructInvite("", Invite{}, app, true)
|
||||||
content = msg.text
|
content = msg.text
|
||||||
|
} else {
|
||||||
|
variables = app.storage.customEmails.InviteEmail.Variables
|
||||||
}
|
}
|
||||||
|
writeVars = func(variables []string) { app.storage.customEmails.InviteEmail.Variables = variables }
|
||||||
// app.storage.customEmails.InviteEmail = content
|
// app.storage.customEmails.InviteEmail = content
|
||||||
} else if id == "WelcomeEmail" {
|
} else if id == "WelcomeEmail" {
|
||||||
content = app.storage.customEmails.WelcomeEmail
|
content = app.storage.customEmails.WelcomeEmail.Content
|
||||||
if content == "" {
|
if content == "" {
|
||||||
|
newEmail = true
|
||||||
msg, err = app.email.constructWelcome("", app, true)
|
msg, err = app.email.constructWelcome("", app, true)
|
||||||
content = msg.text
|
content = msg.text
|
||||||
|
} else {
|
||||||
|
variables = app.storage.customEmails.WelcomeEmail.Variables
|
||||||
}
|
}
|
||||||
|
writeVars = func(variables []string) { app.storage.customEmails.WelcomeEmail.Variables = variables }
|
||||||
// app.storage.customEmails.WelcomeEmail = content
|
// app.storage.customEmails.WelcomeEmail = content
|
||||||
} else if id == "EmailConfirmation" {
|
} else if id == "EmailConfirmation" {
|
||||||
content = app.storage.customEmails.EmailConfirmation
|
content = app.storage.customEmails.EmailConfirmation.Content
|
||||||
if content == "" {
|
if content == "" {
|
||||||
|
newEmail = true
|
||||||
msg, err = app.email.constructConfirmation("", "", "", app, true)
|
msg, err = app.email.constructConfirmation("", "", "", app, true)
|
||||||
content = msg.text
|
content = msg.text
|
||||||
|
} else {
|
||||||
|
variables = app.storage.customEmails.EmailConfirmation.Variables
|
||||||
}
|
}
|
||||||
|
writeVars = func(variables []string) { app.storage.customEmails.EmailConfirmation.Variables = variables }
|
||||||
// app.storage.customEmails.EmailConfirmation = content
|
// app.storage.customEmails.EmailConfirmation = content
|
||||||
|
} else {
|
||||||
|
respondBool(400, false, gc)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
respondBool(500, false, gc)
|
respondBool(500, false, gc)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
variables := make([]string, strings.Count(content, "{"))
|
if newEmail {
|
||||||
i := 0
|
variables = make([]string, strings.Count(content, "{"))
|
||||||
found := false
|
i := 0
|
||||||
buf := ""
|
found := false
|
||||||
for _, c := range content {
|
buf := ""
|
||||||
if !found && c != '{' && c != '}' {
|
for _, c := range content {
|
||||||
continue
|
if !found && c != '{' && c != '}' {
|
||||||
}
|
continue
|
||||||
found = true
|
}
|
||||||
buf += string(c)
|
found = true
|
||||||
if c == '}' {
|
buf += string(c)
|
||||||
found = false
|
if c == '}' {
|
||||||
variables[i] = buf
|
found = false
|
||||||
buf = ""
|
variables[i] = buf
|
||||||
i++
|
buf = ""
|
||||||
|
i++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
writeVars(variables)
|
||||||
}
|
}
|
||||||
gc.JSON(200, emailDTO{Content: content, Variables: variables})
|
if app.storage.storeCustomEmails() != nil {
|
||||||
|
respondBool(500, false, gc)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
gc.JSON(200, customEmail{Content: content, Variables: variables})
|
||||||
}
|
}
|
||||||
|
|
||||||
// @Summary Logout by deleting refresh token from cookies.
|
// @Summary Logout by deleting refresh token from cookies.
|
||||||
|
@ -33,10 +33,11 @@ func (app *appContext) loadConfig() error {
|
|||||||
|
|
||||||
for _, key := range app.config.Section("files").Keys() {
|
for _, key := range app.config.Section("files").Keys() {
|
||||||
if name := key.Name(); name != "html_templates" && name != "lang_files" {
|
if name := key.Name(); name != "html_templates" && name != "lang_files" {
|
||||||
|
fmt.Println(name)
|
||||||
key.SetValue(key.MustString(filepath.Join(app.dataPath, (key.Name() + ".json"))))
|
key.SetValue(key.MustString(filepath.Join(app.dataPath, (key.Name() + ".json"))))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, key := range []string{"user_configuration", "user_displayprefs", "user_profiles", "ombi_template", "invites", "emails", "user_template"} {
|
for _, key := range []string{"user_configuration", "user_displayprefs", "user_profiles", "ombi_template", "invites", "emails", "user_template", "custom_emails"} {
|
||||||
app.config.Section("files").Key(key).SetValue(app.config.Section("files").Key(key).MustString(filepath.Join(app.dataPath, (key + ".json"))))
|
app.config.Section("files").Key(key).SetValue(app.config.Section("files").Key(key).MustString(filepath.Join(app.dataPath, (key + ".json"))))
|
||||||
}
|
}
|
||||||
app.URLBase = strings.TrimSuffix(app.config.Section("ui").Key("url_base").MustString(""), "/")
|
app.URLBase = strings.TrimSuffix(app.config.Section("ui").Key("url_base").MustString(""), "/")
|
||||||
|
109
email.go
109
email.go
@ -222,6 +222,7 @@ func (emailer *Emailer) constructConfirmation(code, username, key string, app *a
|
|||||||
"ifItWasNotYou": emailer.lang.Strings.get("ifItWasNotYou"),
|
"ifItWasNotYou": emailer.lang.Strings.get("ifItWasNotYou"),
|
||||||
"confirmEmail": emailer.lang.EmailConfirmation.get("confirmEmail"),
|
"confirmEmail": emailer.lang.EmailConfirmation.get("confirmEmail"),
|
||||||
"message": "",
|
"message": "",
|
||||||
|
"username": username,
|
||||||
}
|
}
|
||||||
if noSub {
|
if noSub {
|
||||||
template["helloUser"] = emailer.lang.Strings.get("helloUser")
|
template["helloUser"] = emailer.lang.Strings.get("helloUser")
|
||||||
@ -237,14 +238,25 @@ func (emailer *Emailer) constructConfirmation(code, username, key string, app *a
|
|||||||
template["confirmationURL"] = inviteLink
|
template["confirmationURL"] = inviteLink
|
||||||
template["message"] = message
|
template["message"] = message
|
||||||
}
|
}
|
||||||
email.html, email.text, err = emailer.construct(app, "email_confirmation", "email_", template)
|
if app.storage.customEmails.EmailConfirmation.Enabled {
|
||||||
|
content := app.storage.customEmails.EmailConfirmation.Content
|
||||||
|
for _, v := range app.storage.customEmails.EmailConfirmation.Variables {
|
||||||
|
replaceWith, ok := template[v[1:len(v)-1]]
|
||||||
|
if ok {
|
||||||
|
content = strings.ReplaceAll(content, v, replaceWith.(string))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
email, err = emailer.constructTemplate(email.subject, content, app)
|
||||||
|
} else {
|
||||||
|
email.html, email.text, err = emailer.construct(app, "email_confirmation", "email_", template)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return email, nil
|
return email, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (emailer *Emailer) constructAnnouncement(subject, md string, app *appContext) (*Email, error) {
|
func (emailer *Emailer) constructTemplate(subject, md string, app *appContext) (*Email, error) {
|
||||||
email := &Email{subject: subject}
|
email := &Email{subject: subject}
|
||||||
renderer := html.NewRenderer(html.RendererOptions{Flags: html.Smartypants})
|
renderer := html.NewRenderer(html.RendererOptions{Flags: html.Smartypants})
|
||||||
html := markdown.ToHTML([]byte(md), nil, renderer)
|
html := markdown.ToHTML([]byte(md), nil, renderer)
|
||||||
@ -277,6 +289,9 @@ func (emailer *Emailer) constructInvite(code string, invite Invite, app *appCont
|
|||||||
"toJoin": emailer.lang.InviteEmail.get("toJoin"),
|
"toJoin": emailer.lang.InviteEmail.get("toJoin"),
|
||||||
"linkButton": emailer.lang.InviteEmail.get("linkButton"),
|
"linkButton": emailer.lang.InviteEmail.get("linkButton"),
|
||||||
"message": "",
|
"message": "",
|
||||||
|
"date": d,
|
||||||
|
"time": t,
|
||||||
|
"expiresInMinutes": expiresIn,
|
||||||
}
|
}
|
||||||
if noSub {
|
if noSub {
|
||||||
template["inviteExpiry"] = emailer.lang.InviteEmail.get("inviteExpiry")
|
template["inviteExpiry"] = emailer.lang.InviteEmail.get("inviteExpiry")
|
||||||
@ -290,7 +305,18 @@ func (emailer *Emailer) constructInvite(code string, invite Invite, app *appCont
|
|||||||
template["message"] = message
|
template["message"] = message
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
email.html, email.text, err = emailer.construct(app, "invite_emails", "email_", template)
|
if app.storage.customEmails.InviteEmail.Enabled {
|
||||||
|
content := app.storage.customEmails.InviteEmail.Content
|
||||||
|
for _, v := range app.storage.customEmails.InviteEmail.Variables {
|
||||||
|
replaceWith, ok := template[v[1:len(v)-1]]
|
||||||
|
if ok {
|
||||||
|
content = strings.ReplaceAll(content, v, replaceWith.(string))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
email, err = emailer.constructTemplate(email.subject, content, app)
|
||||||
|
} else {
|
||||||
|
email.html, email.text, err = emailer.construct(app, "invite_emails", "email_", template)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -305,14 +331,27 @@ func (emailer *Emailer) constructExpiry(code string, invite Invite, app *appCont
|
|||||||
template := map[string]interface{}{
|
template := map[string]interface{}{
|
||||||
"inviteExpired": emailer.lang.InviteExpiry.get("inviteExpired"),
|
"inviteExpired": emailer.lang.InviteExpiry.get("inviteExpired"),
|
||||||
"notificationNotice": emailer.lang.InviteExpiry.get("notificationNotice"),
|
"notificationNotice": emailer.lang.InviteExpiry.get("notificationNotice"),
|
||||||
|
"code": "\"" + code + "\"",
|
||||||
|
"time": expiry,
|
||||||
}
|
}
|
||||||
if noSub {
|
if noSub {
|
||||||
template["expiredAt"] = emailer.lang.InviteExpiry.get("expiredAt")
|
template["expiredAt"] = emailer.lang.InviteExpiry.get("expiredAt")
|
||||||
} else {
|
} else {
|
||||||
template["expiredAt"] = emailer.lang.InviteExpiry.template("expiredAt", tmpl{"code": "\"" + code + "\"", "time": expiry})
|
template["expiredAt"] = emailer.lang.InviteExpiry.template("expiredAt", tmpl{"code": template["code"].(string), "time": template["time"].(string)})
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
email.html, email.text, err = emailer.construct(app, "notifications", "expiry_", template)
|
if app.storage.customEmails.InviteExpiry.Enabled {
|
||||||
|
content := app.storage.customEmails.InviteExpiry.Content
|
||||||
|
for _, v := range app.storage.customEmails.InviteExpiry.Variables {
|
||||||
|
replaceWith, ok := template[v[1:len(v)-1]]
|
||||||
|
if ok {
|
||||||
|
content = strings.ReplaceAll(content, v, replaceWith.(string))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
email, err = emailer.constructTemplate(email.subject, content, app)
|
||||||
|
} else {
|
||||||
|
email.html, email.text, err = emailer.construct(app, "notifications", "expiry_", template)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -328,6 +367,7 @@ func (emailer *Emailer) constructCreated(code, username, address string, invite
|
|||||||
"addressString": emailer.lang.Strings.get("emailAddress"),
|
"addressString": emailer.lang.Strings.get("emailAddress"),
|
||||||
"timeString": emailer.lang.UserCreated.get("time"),
|
"timeString": emailer.lang.UserCreated.get("time"),
|
||||||
"notificationNotice": "",
|
"notificationNotice": "",
|
||||||
|
"code": "\"" + code + "\"",
|
||||||
}
|
}
|
||||||
if noSub {
|
if noSub {
|
||||||
template["aUserWasCreated"] = emailer.lang.UserCreated.get("aUserWasCreated")
|
template["aUserWasCreated"] = emailer.lang.UserCreated.get("aUserWasCreated")
|
||||||
@ -343,14 +383,25 @@ func (emailer *Emailer) constructCreated(code, username, address string, invite
|
|||||||
} else {
|
} else {
|
||||||
tplAddress = address
|
tplAddress = address
|
||||||
}
|
}
|
||||||
template["aUserWasCreated"] = emailer.lang.UserCreated.template("aUserWasCreated", tmpl{"code": "\"" + code + "\""})
|
template["aUserWasCreated"] = emailer.lang.UserCreated.template("aUserWasCreated", tmpl{"code": template["code"].(string)})
|
||||||
template["name"] = username
|
template["name"] = username
|
||||||
template["address"] = tplAddress
|
template["address"] = tplAddress
|
||||||
template["time"] = created
|
template["time"] = created
|
||||||
template["notificationNotice"] = emailer.lang.UserCreated.get("notificationNotice")
|
template["notificationNotice"] = emailer.lang.UserCreated.get("notificationNotice")
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
email.html, email.text, err = emailer.construct(app, "notifications", "created_", template)
|
if app.storage.customEmails.UserCreated.Enabled {
|
||||||
|
content := app.storage.customEmails.UserCreated.Content
|
||||||
|
for _, v := range app.storage.customEmails.UserCreated.Variables {
|
||||||
|
replaceWith, ok := template[v[1:len(v)-1]]
|
||||||
|
if ok {
|
||||||
|
content = strings.ReplaceAll(content, v, replaceWith.(string))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
email, err = emailer.constructTemplate(email.subject, content, app)
|
||||||
|
} else {
|
||||||
|
email.html, email.text, err = emailer.construct(app, "notifications", "created_", template)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -369,6 +420,10 @@ func (emailer *Emailer) constructReset(pwr PasswordReset, app *appContext, noSub
|
|||||||
"ifItWasNotYou": emailer.lang.Strings.get("ifItWasNotYou"),
|
"ifItWasNotYou": emailer.lang.Strings.get("ifItWasNotYou"),
|
||||||
"pinString": emailer.lang.PasswordReset.get("pin"),
|
"pinString": emailer.lang.PasswordReset.get("pin"),
|
||||||
"message": "",
|
"message": "",
|
||||||
|
"username": pwr.Username,
|
||||||
|
"date": d,
|
||||||
|
"time": t,
|
||||||
|
"expiresInMinutes": expiresIn,
|
||||||
}
|
}
|
||||||
if noSub {
|
if noSub {
|
||||||
template["helloUser"] = emailer.lang.Strings.get("helloUser")
|
template["helloUser"] = emailer.lang.Strings.get("helloUser")
|
||||||
@ -384,7 +439,18 @@ func (emailer *Emailer) constructReset(pwr PasswordReset, app *appContext, noSub
|
|||||||
template["message"] = message
|
template["message"] = message
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
email.html, email.text, err = emailer.construct(app, "password_resets", "email_", template)
|
if app.storage.customEmails.PasswordReset.Enabled {
|
||||||
|
content := app.storage.customEmails.PasswordReset.Content
|
||||||
|
for _, v := range app.storage.customEmails.PasswordReset.Variables {
|
||||||
|
replaceWith, ok := template[v[1:len(v)-1]]
|
||||||
|
if ok {
|
||||||
|
content = strings.ReplaceAll(content, v, replaceWith.(string))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
email, err = emailer.constructTemplate(email.subject, content, app)
|
||||||
|
} else {
|
||||||
|
email.html, email.text, err = emailer.construct(app, "password_resets", "email_", template)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -410,7 +476,18 @@ func (emailer *Emailer) constructDeleted(reason string, app *appContext, noSub b
|
|||||||
template["message"] = app.config.Section("email").Key("message").String()
|
template["message"] = app.config.Section("email").Key("message").String()
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
email.html, email.text, err = emailer.construct(app, "deletion", "email_", template)
|
if app.storage.customEmails.UserDeleted.Enabled {
|
||||||
|
content := app.storage.customEmails.UserDeleted.Content
|
||||||
|
for _, v := range app.storage.customEmails.UserDeleted.Variables {
|
||||||
|
replaceWith, ok := template[v[1:len(v)-1]]
|
||||||
|
if ok {
|
||||||
|
content = strings.ReplaceAll(content, v, replaceWith.(string))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
email, err = emailer.constructTemplate(email.subject, content, app)
|
||||||
|
} else {
|
||||||
|
email.html, email.text, err = emailer.construct(app, "deletion", "email_", template)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -439,7 +516,18 @@ func (emailer *Emailer) constructWelcome(username string, app *appContext, noSub
|
|||||||
template["message"] = app.config.Section("email").Key("message").String()
|
template["message"] = app.config.Section("email").Key("message").String()
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
email.html, email.text, err = emailer.construct(app, "welcome_email", "email_", template)
|
if app.storage.customEmails.WelcomeEmail.Enabled {
|
||||||
|
content := app.storage.customEmails.WelcomeEmail.Content
|
||||||
|
for _, v := range app.storage.customEmails.WelcomeEmail.Variables {
|
||||||
|
replaceWith, ok := template[v[1:len(v)-1]]
|
||||||
|
if ok {
|
||||||
|
content = strings.ReplaceAll(content, v, replaceWith.(string))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
email, err = emailer.constructTemplate(email.subject, content, app)
|
||||||
|
} else {
|
||||||
|
email.html, email.text, err = emailer.construct(app, "welcome_email", "email_", template)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -448,5 +536,6 @@ func (emailer *Emailer) constructWelcome(username string, app *appContext, noSub
|
|||||||
|
|
||||||
// calls the send method in the underlying emailClient.
|
// calls the send method in the underlying emailClient.
|
||||||
func (emailer *Emailer) send(email *Email, address ...string) error {
|
func (emailer *Emailer) send(email *Email, address ...string) error {
|
||||||
|
fmt.Printf("%+v\n", email)
|
||||||
return emailer.sender.send(emailer.fromName, emailer.fromAddr, email, address...)
|
return emailer.sender.send(emailer.fromName, emailer.fromAddr, email, address...)
|
||||||
}
|
}
|
||||||
|
8
lang.go
8
lang.go
@ -120,14 +120,18 @@ func (ls *setupLangs) getOptions() [][2]string {
|
|||||||
type langSection map[string]string
|
type langSection map[string]string
|
||||||
type tmpl map[string]string
|
type tmpl map[string]string
|
||||||
|
|
||||||
func (el langSection) template(field string, vals tmpl) string {
|
func templateString(text string, vals tmpl) string {
|
||||||
text := el.get(field)
|
|
||||||
for key, val := range vals {
|
for key, val := range vals {
|
||||||
text = strings.ReplaceAll(text, "{"+key+"}", val)
|
text = strings.ReplaceAll(text, "{"+key+"}", val)
|
||||||
}
|
}
|
||||||
return text
|
return text
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (el langSection) template(field string, vals tmpl) string {
|
||||||
|
text := el.get(field)
|
||||||
|
return templateString(text, vals)
|
||||||
|
}
|
||||||
|
|
||||||
func (el langSection) format(field string, vals ...string) string {
|
func (el langSection) format(field string, vals ...string) string {
|
||||||
text := el.get(field)
|
text := el.get(field)
|
||||||
for _, val := range vals {
|
for _, val := range vals {
|
||||||
|
@ -176,10 +176,6 @@ type settings struct {
|
|||||||
type langDTO map[string]string
|
type langDTO map[string]string
|
||||||
|
|
||||||
type emailListDTO map[string]string
|
type emailListDTO map[string]string
|
||||||
type emailDTO struct {
|
|
||||||
Content string `json:"content"`
|
|
||||||
Variables []string `json:"Variables"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type emailSetDTO struct {
|
type emailSetDTO struct {
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
|
@ -141,6 +141,8 @@ func (app *appContext) loadRoutes(router *gin.Engine) {
|
|||||||
api.POST(p+"/users/announce", app.Announce)
|
api.POST(p+"/users/announce", app.Announce)
|
||||||
api.GET(p+"/config/emails", app.GetEmails)
|
api.GET(p+"/config/emails", app.GetEmails)
|
||||||
api.GET(p+"/config/emails/:id", app.GetEmail)
|
api.GET(p+"/config/emails/:id", app.GetEmail)
|
||||||
|
api.POST(p+"/config/emails/:id", app.SetEmail)
|
||||||
|
api.POST(p+"/config/emails/:id/:state", app.SetEmailState)
|
||||||
api.GET(p+"/config", app.GetConfig)
|
api.GET(p+"/config", app.GetConfig)
|
||||||
api.POST(p+"/config", app.ModifyConfig)
|
api.POST(p+"/config", app.ModifyConfig)
|
||||||
api.POST(p+"/restart", app.restart)
|
api.POST(p+"/restart", app.restart)
|
||||||
|
20
storage.go
20
storage.go
@ -27,13 +27,19 @@ type Storage struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type customEmails struct {
|
type customEmails struct {
|
||||||
UserCreated string `json:"userCreated"`
|
UserCreated customEmail `json:"userCreated"`
|
||||||
InviteExpiry string `json:"inviteExpiry"`
|
InviteExpiry customEmail `json:"inviteExpiry"`
|
||||||
PasswordReset string `json:"passwordReset"`
|
PasswordReset customEmail `json:"passwordReset"`
|
||||||
UserDeleted string `json:"userDeleted"`
|
UserDeleted customEmail `json:"userDeleted"`
|
||||||
InviteEmail string `json:"inviteEmail"`
|
InviteEmail customEmail `json:"inviteEmail"`
|
||||||
WelcomeEmail string `json:"welcomeEmail"`
|
WelcomeEmail customEmail `json:"welcomeEmail"`
|
||||||
EmailConfirmation string `json:"emailConfirmation"`
|
EmailConfirmation customEmail `json:"emailConfirmation"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type customEmail struct {
|
||||||
|
Enabled bool `json:"enabled,omitempty"`
|
||||||
|
Content string `json:"content"`
|
||||||
|
Variables []string `json:"variables,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// timePattern: %Y-%m-%dT%H:%M:%S.%f
|
// timePattern: %Y-%m-%dT%H:%M:%S.%f
|
||||||
|
Loading…
Reference in New Issue
Block a user