From 2d79482efed045347528dbcb59f40d21fcadac83 Mon Sep 17 00:00:00 2001 From: LinkinStar Date: Fri, 30 Dec 2022 11:22:41 +0800 Subject: [PATCH] feat(migration): Add upgrade configuration for new message email notification. --- internal/migrations/init.go | 2 +- internal/migrations/migrations.go | 1 + internal/migrations/v6.go | 41 +++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 internal/migrations/v6.go diff --git a/internal/migrations/init.go b/internal/migrations/init.go index dad2028a..88c796d8 100644 --- a/internal/migrations/init.go +++ b/internal/migrations/init.go @@ -260,7 +260,7 @@ func initConfigTable(engine *xorm.Engine) error { {ID: 30, Key: "answer.vote_up", Value: `0`}, {ID: 31, Key: "answer.vote_up_cancel", Value: `0`}, {ID: 32, Key: "question.follow", Value: `0`}, - {ID: 33, Key: "email.config", Value: `{"from_name":"","from_email":"","smtp_host":"","smtp_port":465,"smtp_password":"","smtp_username":"","smtp_authentication":true,"encryption":"","register_title":"[{{.SiteName}}] Confirm your new account","register_body":"Welcome to {{.SiteName}}

\n\nClick the following link to confirm and activate your new account:
\n{{.RegisterUrl}}

\n\nIf the above link is not clickable, try copying and pasting it into the address bar of your web browser.\n","pass_reset_title":"[{{.SiteName }}] Password reset","pass_reset_body":"Somebody asked to reset your password on [{{.SiteName}}].

\n\nIf it was not you, you can safely ignore this email.

\n\nClick the following link to choose a new password:
\n{{.PassResetUrl}}\n","change_title":"[{{.SiteName}}] Confirm your new email address","change_body":"Confirm your new email address for {{.SiteName}} by clicking on the following link:

\n\n{{.ChangeEmailUrl}}

\n\nIf you did not request this change, please ignore this email.\n","test_title":"[{{.SiteName}}] Test Email","test_body":"This is a test email."}`}, + {ID: 33, Key: "email.config", Value: `{"from_name":"","from_email":"","smtp_host":"","smtp_port":465,"smtp_password":"","smtp_username":"","smtp_authentication":true,"encryption":"","register_title":"[{{.SiteName}}] Confirm your new account","register_body":"Welcome to {{.SiteName}}

\n\nClick the following link to confirm and activate your new account:
\n{{.RegisterUrl}}

\n\nIf the above link is not clickable, try copying and pasting it into the address bar of your web browser.\n","pass_reset_title":"[{{.SiteName }}] Password reset","pass_reset_body":"Somebody asked to reset your password on [{{.SiteName}}].

\n\nIf it was not you, you can safely ignore this email.

\n\nClick the following link to choose a new password:
\n{{.PassResetUrl}}\n","change_title":"[{{.SiteName}}] Confirm your new email address","change_body":"Confirm your new email address for {{.SiteName}} by clicking on the following link:

\n\n{{.ChangeEmailUrl}}

\n\nIf you did not request this change, please ignore this email.\n","test_title":"[{{.SiteName}}] Test Email","test_body":"This is a test email.","new_answer_title":"[{{.SiteName}}] {{.DisplayName}} answered your question","new_answer_body":"{{.QuestionTitle}}

\n\n{{.DisplayName}}:
\n
{{.AnswerSummary}}

\nView it on {{.SiteName}}

\n\nYou are receiving this because you authored the thread. Unsubscribe","new_comment_title":"[{{.SiteName}}] {{.DisplayName}} commented on your post","new_comment_body":"{{.QuestionTitle}}

\n\n{{.DisplayName}}:
\n
{{.CommentSummary}}

\nView it on {{.SiteName}}

\n\nYou are receiving this because you authored the thread. Unsubscribe"}`}, {ID: 35, Key: "tag.follow", Value: `0`}, {ID: 36, Key: "rank.question.add", Value: `1`}, {ID: 37, Key: "rank.question.edit", Value: `200`}, diff --git a/internal/migrations/migrations.go b/internal/migrations/migrations.go index bbe0a958..7b53293e 100644 --- a/internal/migrations/migrations.go +++ b/internal/migrations/migrations.go @@ -55,6 +55,7 @@ var migrations = []Migration{ NewMigration("add activity timeline", addActivityTimeline, false), NewMigration("add user role", addRoleFeatures, false), NewMigration("add theme and private mode", addThemeAndPrivateMode, true), + NewMigration("add new answer notification", addNewAnswerNotification, true), } // GetCurrentDBVersion returns the current db version diff --git a/internal/migrations/v6.go b/internal/migrations/v6.go new file mode 100644 index 00000000..0f87b367 --- /dev/null +++ b/internal/migrations/v6.go @@ -0,0 +1,41 @@ +package migrations + +import ( + "encoding/json" + "fmt" + + "github.com/answerdev/answer/internal/entity" + "xorm.io/xorm" +) + +func addNewAnswerNotification(x *xorm.Engine) error { + cond := &entity.Config{Key: "email.config"} + exists, err := x.Get(cond) + if err != nil { + return fmt.Errorf("get email config failed: %w", err) + } + if !exists { + // This should be impossible except that the config was deleted manually by user. + _, err = x.InsertOne(&entity.Config{ + Key: "email.config", + Value: `{"from_name":"","from_email":"","smtp_host":"","smtp_port":465,"smtp_password":"","smtp_username":"","smtp_authentication":true,"encryption":"","register_title":"[{{.SiteName}}] Confirm your new account","register_body":"Welcome to {{.SiteName}}

\n\nClick the following link to confirm and activate your new account:
\n{{.RegisterUrl}}

\n\nIf the above link is not clickable, try copying and pasting it into the address bar of your web browser.\n","pass_reset_title":"[{{.SiteName }}] Password reset","pass_reset_body":"Somebody asked to reset your password on [{{.SiteName}}].

\n\nIf it was not you, you can safely ignore this email.

\n\nClick the following link to choose a new password:
\n{{.PassResetUrl}}\n","change_title":"[{{.SiteName}}] Confirm your new email address","change_body":"Confirm your new email address for {{.SiteName}} by clicking on the following link:

\n\n{{.ChangeEmailUrl}}

\n\nIf you did not request this change, please ignore this email.\n","test_title":"[{{.SiteName}}] Test Email","test_body":"This is a test email.","new_answer_title":"[{{.SiteName}}] {{.DisplayName}} answered your question","new_answer_body":"{{.QuestionTitle}}

\n\n{{.DisplayName}}:
\n
{{.AnswerSummary}}

\nView it on {{.SiteName}}

\n\nYou are receiving this because you authored the thread. Unsubscribe","new_comment_title":"[{{.SiteName}}] {{.DisplayName}} commented on your post","new_comment_body":"{{.QuestionTitle}}

\n\n{{.DisplayName}}:
\n
{{.CommentSummary}}

\nView it on {{.SiteName}}

\n\nYou are receiving this because you authored the thread. Unsubscribe"}`, + }) + if err != nil { + return fmt.Errorf("add email config failed: %v", err) + } + } + + m := make(map[string]interface{}) + _ = json.Unmarshal([]byte(cond.Value), &m) + m["new_answer_title"] = "[{{.SiteName}}] {{.DisplayName}} answered your question" + m["new_answer_body"] = "{{.QuestionTitle}}

\n\n{{.DisplayName}}:
\n
{{.AnswerSummary}}

\nView it on {{.SiteName}}

\n\nYou are receiving this because you authored the thread. Unsubscribe" + m["new_comment_title"] = "[{{.SiteName}}] {{.DisplayName}} commented on your post" + m["new_comment_body"] = "{{.QuestionTitle}}

\n\n{{.DisplayName}}:
\n
{{.CommentSummary}}

\nView it on {{.SiteName}}

\n\nYou are receiving this because you authored the thread. Unsubscribe" + + val, _ := json.Marshal(m) + _, err = x.ID(cond.ID).Update(&entity.Config{Value: string(val)}) + if err != nil { + return fmt.Errorf("update email config failed: %v", err) + } + return nil +}