From 006ac48d6eb1f4de60b6fe86ff3dee270238ff63 Mon Sep 17 00:00:00 2001 From: Rain Date: Thu, 19 Mar 2020 14:16:30 +0800 Subject: [PATCH 1/2] fix(notify): Compatible with changes about list in schema --- pkg/apis/notify/notification.go | 3 --- pkg/notify/models/mod_notification.go | 5 ++--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/pkg/apis/notify/notification.go b/pkg/apis/notify/notification.go index a51ddf3b93..4381e23220 100644 --- a/pkg/apis/notify/notification.go +++ b/pkg/apis/notify/notification.go @@ -23,8 +23,5 @@ import ( type NotificationDetails struct { apis.ResourceBaseDetails - Id string `json:"id"` - Name string `json:"name"` - UserList jsonutils.JSONObject `json:"user_list"` } diff --git a/pkg/notify/models/mod_notification.go b/pkg/notify/models/mod_notification.go index 636bc4ac32..18f7ce1e94 100644 --- a/pkg/notify/models/mod_notification.go +++ b/pkg/notify/models/mod_notification.go @@ -96,7 +96,7 @@ type SNotification struct { SendAt time.Time `nullable:"false"` SendBy string `width:"128" nullable:"false"` // ClusterID identify message with same topic, msg, priority - ClusterID string `width:"128" charset:"ascii" primary:"true" create:"optional"` + ClusterID string `width:"128" charset:"ascii" primary:"true" create:"optional" list:"user" get:"user"` } type UserDetail struct { @@ -165,7 +165,6 @@ func (self *SNotification) getMoreDetails(ctx context.Context, query jsonutils.J userDetails = []UserDetail{userDetail} } - out.Id = self.ClusterID out.UserList = jsonutils.Marshal(userDetails) return out, nil } @@ -289,7 +288,7 @@ func (self *SNotificationManager) ListItemFilter(ctx context.Context, q *sqlchem q = q.Equals("uid", userCred.GetUserId()) } - q = q.GroupBy("cluster_id") + q = q.GroupBy("cluster_id").Desc("received_at") return q, nil } From 5fdf9846f71362edee87dfc33b4900722784d4fd Mon Sep 17 00:00:00 2001 From: Rain Date: Thu, 19 Mar 2020 15:44:29 +0800 Subject: [PATCH 2/2] feat: Unified template --- .../share/template/content/IMAGE_ACTIVED | 1 + .../content/VERIFY.email} | 0 .../share/template/remote/VERIFY.mobile | 1 + .../yunion/share/template/title/IMAGE_ACTIVED | 1 + .../yunion/share/template/title/VERIFY.email | 1 + pkg/notify/dispatcher.go | 2 +- pkg/notify/models/mod_template.go | 88 +++++++++---------- pkg/notify/template/doc.go | 15 ---- pkg/notify/template/template.go | 23 ----- 9 files changed, 45 insertions(+), 87 deletions(-) create mode 100644 build/notify/root/opt/yunion/share/template/content/IMAGE_ACTIVED rename build/notify/root/opt/yunion/share/{notify/email_verify_template => template/content/VERIFY.email} (100%) create mode 100644 build/notify/root/opt/yunion/share/template/remote/VERIFY.mobile create mode 100644 build/notify/root/opt/yunion/share/template/title/IMAGE_ACTIVED create mode 100644 build/notify/root/opt/yunion/share/template/title/VERIFY.email delete mode 100644 pkg/notify/template/doc.go delete mode 100644 pkg/notify/template/template.go diff --git a/build/notify/root/opt/yunion/share/template/content/IMAGE_ACTIVED b/build/notify/root/opt/yunion/share/template/content/IMAGE_ACTIVED new file mode 100644 index 0000000000..b6e80f5c29 --- /dev/null +++ b/build/notify/root/opt/yunion/share/template/content/IMAGE_ACTIVED @@ -0,0 +1 @@ +{{.os_type}} 镜像 {{.name}} 上传完成 diff --git a/build/notify/root/opt/yunion/share/notify/email_verify_template b/build/notify/root/opt/yunion/share/template/content/VERIFY.email similarity index 100% rename from build/notify/root/opt/yunion/share/notify/email_verify_template rename to build/notify/root/opt/yunion/share/template/content/VERIFY.email diff --git a/build/notify/root/opt/yunion/share/template/remote/VERIFY.mobile b/build/notify/root/opt/yunion/share/template/remote/VERIFY.mobile new file mode 100644 index 0000000000..9fee136f0f --- /dev/null +++ b/build/notify/root/opt/yunion/share/template/remote/VERIFY.mobile @@ -0,0 +1 @@ +SMS_126915025 diff --git a/build/notify/root/opt/yunion/share/template/title/IMAGE_ACTIVED b/build/notify/root/opt/yunion/share/template/title/IMAGE_ACTIVED new file mode 100644 index 0000000000..8b6d4288e1 --- /dev/null +++ b/build/notify/root/opt/yunion/share/template/title/IMAGE_ACTIVED @@ -0,0 +1 @@ +镜像 {{.name}} 上传完成 diff --git a/build/notify/root/opt/yunion/share/template/title/VERIFY.email b/build/notify/root/opt/yunion/share/template/title/VERIFY.email new file mode 100644 index 0000000000..60d3c8e429 --- /dev/null +++ b/build/notify/root/opt/yunion/share/template/title/VERIFY.email @@ -0,0 +1 @@ +Yunion Verify diff --git a/pkg/notify/dispatcher.go b/pkg/notify/dispatcher.go index 65204629ee..2f4aeb82e7 100644 --- a/pkg/notify/dispatcher.go +++ b/pkg/notify/dispatcher.go @@ -268,7 +268,7 @@ func (self *NotifyModelDispatcher) Verify(ctx context.Context, params map[string data := jsonutils.NewDict() data.Set("status", jsonutils.NewString(models.VERIFICATION_VERIFIED)) data.Set("verified_at", jsonutils.NewTimeString(current)) - _, err = self.Update(ctx, verifition.CID, jsonutils.JSONNull, data, nil) + _, err = self.Update(ctx, verifition.CID, jsonutils.NewDict(), data, nil) if err != nil { return httperrors.NewGeneralError(err) } diff --git a/pkg/notify/models/mod_template.go b/pkg/notify/models/mod_template.go index 57f6d93e9f..9f6bc59150 100644 --- a/pkg/notify/models/mod_template.go +++ b/pkg/notify/models/mod_template.go @@ -19,13 +19,13 @@ import ( "context" "database/sql" "encoding/json" + "fmt" "io/ioutil" - "os" + "path/filepath" "strings" ptem "text/template" "yunion.io/x/jsonutils" - "yunion.io/x/log" "yunion.io/x/pkg/errors" "yunion.io/x/sqlchemy" @@ -34,7 +34,6 @@ import ( "yunion.io/x/onecloud/pkg/mcclient" "yunion.io/x/onecloud/pkg/notify/options" "yunion.io/x/onecloud/pkg/notify/rpc/apis" - "yunion.io/x/onecloud/pkg/notify/template" ) type STemplateManager struct { @@ -58,6 +57,7 @@ const ( TEMPLATE_TYPE_TITLE = "title" TEMPLATE_TYPE_CONTENT = "content" TEMPLATE_TYPE_REMOTE = "remote" + CONTACTTYPE_ALL = "all" ) type STemplate struct { @@ -75,58 +75,50 @@ func (tm *STemplateManager) GetEmailUrl() string { return options.Options.VerifyEmailUrl } -var initTemlateList []STemplate +var templatePath = "/opt/yunion/share/template" -func (tm *STemplateManager) defaultTemplate() []STemplate { - if len(initTemlateList) != 0 { - return initTemlateList - } +func (tm *STemplateManager) defaultTemplate() ([]STemplate, error) { + templates := make([]STemplate, 0, 4) - initTemlateList = []STemplate{ - { - ContactType: "email", - Topic: "IMAGE_ACTIVED", - TemplateType: TEMPLATE_TYPE_TITLE, - Content: template.IMAGE_ACTIVED_TITLE, - }, - { - ContactType: "email", - Topic: "IMAGE_ACTIVED", - TemplateType: TEMPLATE_TYPE_CONTENT, - Content: template.IMAGE_ACTIVED_CONTENT, - }, - } - content, err := ioutil.ReadFile(template.EMAIL_VERIFY_CONTENT_PATH) - if err == nil { - initTemlateList = append(initTemlateList, - STemplate{ - ContactType: "email", - Topic: "VERIFY", - TemplateType: TEMPLATE_TYPE_CONTENT, + for _, templateType := range []string{"title", "content", "remote"} { + contactType, topic := CONTACTTYPE_ALL, "" + titleTemplatePath := fmt.Sprintf("%s/%s", templatePath, templateType) + files, err := ioutil.ReadDir(titleTemplatePath) + if err != nil { + return templates, errors.Wrapf(err, "Read Dir '%s'", titleTemplatePath) + } + for _, file := range files { + if file.IsDir() { + continue + } + spliteName := strings.Split(file.Name(), ".") + topic = spliteName[0] + if len(spliteName) > 1 { + contactType = spliteName[1] + } + fullPath := filepath.Join(titleTemplatePath, file.Name()) + content, err := ioutil.ReadFile(fullPath) + if err != nil { + return templates, err + } + templates = append(templates, STemplate{ + ContactType: contactType, + Topic: topic, + TemplateType: templateType, Content: string(content), - }, - STemplate{ - ContactType: "email", - Topic: "VERIFY", - TemplateType: TEMPLATE_TYPE_TITLE, - Content: template.EMAIL_VERIFY_TITLE, - }, - ) - } else { - if os.IsNotExist(err) { - log.Errorf("The path of email verify template is invalid") - } else { - log.Errorf("open %s error: %s", template.EMAIL_VERIFY_CONTENT_PATH, err.Error()) + }) } } - - return initTemlateList + return templates, nil } func (tm *STemplateManager) InitializeData() error { - for _, template := range tm.defaultTemplate() { - q := tm.Query().Equals("contact_type", template.ContactType).Equals("topic", template.Topic).Equals("template_type", - template.TemplateType) + templates, err := tm.defaultTemplate() + if err != nil { + return err + } + for _, template := range templates { + q := tm.Query().Equals("contact_type", template.ContactType).Equals("topic", template.Topic).Equals("template_type", template.TemplateType) count, _ := q.CountWithError() if count > 0 { continue @@ -144,7 +136,7 @@ func (tm *STemplateManager) InitializeData() error { func (tm *STemplateManager) NotifyFilter(contactType, topic, msg string) (params apis.SendParams, err error) { params.Topic = topic templates := make([]STemplate, 0, 3) - q := tm.Query().Equals("contact_type", contactType).Equals("topic", strings.ToUpper(topic)) + q := tm.Query().Equals("topic", strings.ToUpper(topic)).In("contact_type", []string{CONTACTTYPE_ALL, contactType}) err = db.FetchModelObjects(tm, q, &templates) if errors.Cause(err) == sql.ErrNoRows || len(templates) == 0 { // no such template, return as is diff --git a/pkg/notify/template/doc.go b/pkg/notify/template/doc.go deleted file mode 100644 index fe2add7fa7..0000000000 --- a/pkg/notify/template/doc.go +++ /dev/null @@ -1,15 +0,0 @@ -// Copyright 2019 Yunion -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package template // import "yunion.io/x/onecloud/pkg/notify/template" diff --git a/pkg/notify/template/template.go b/pkg/notify/template/template.go deleted file mode 100644 index 383e8e7678..0000000000 --- a/pkg/notify/template/template.go +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright 2019 Yunion -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package template - -const ( - EMAIL_VERIFY_CONTENT_PATH = "/opt/yunion/share/notify/email_verify_template" - EMAIL_VERIFY_TITLE = "Yunion Verify" - - IMAGE_ACTIVED_TITLE = "镜像 {{.name}} 上传完成" - IMAGE_ACTIVED_CONTENT = "{{.os_type}} 镜像 {{.name}} 上传完成" -)