verification_send_task.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package tasks
  15. import (
  16. "context"
  17. "fmt"
  18. "strings"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/log"
  21. api "yunion.io/x/onecloud/pkg/apis/notify"
  22. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  23. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  24. "yunion.io/x/onecloud/pkg/notify/models"
  25. "yunion.io/x/onecloud/pkg/util/logclient"
  26. )
  27. type VerificationSendTask struct {
  28. taskman.STask
  29. }
  30. func init() {
  31. taskman.RegisterTask(VerificationSendTask{})
  32. }
  33. func (self *VerificationSendTask) taskFailed(ctx context.Context, receiver *models.SReceiver, reason string) {
  34. log.Errorf("fail to send verification: %s", reason)
  35. logclient.AddActionLogWithContext(ctx, receiver, logclient.ACT_SEND_VERIFICATION, reason, self.UserCred, false)
  36. self.SetStageFailed(ctx, jsonutils.NewString(reason))
  37. }
  38. func (self *VerificationSendTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
  39. receiver := obj.(*models.SReceiver)
  40. contactType, _ := self.Params.GetString("contact_type")
  41. verification, err := models.VerificationManager.Get(receiver.GetId(), contactType)
  42. if err != nil {
  43. self.taskFailed(ctx, receiver, fmt.Sprintf("VerificationManager.Get for receiver_id %q and contact_type %q: %s", receiver.GetId(), contactType, err.Error()))
  44. return
  45. }
  46. if err != nil {
  47. self.taskFailed(ctx, receiver, fmt.Sprintf("fail to get contact(type: %s): %s", contactType, err.Error()))
  48. return
  49. }
  50. emailMsg := &api.SEmailMessage{}
  51. notifyReceiver := api.SNotifyReceiver{}
  52. // build message
  53. var message string
  54. switch contactType {
  55. case api.EMAIL:
  56. info, err := models.TemplateManager.GetCompanyInfo(ctx)
  57. if err != nil {
  58. self.taskFailed(ctx, receiver, fmt.Sprintf("fail to get company info: %s", err.Error()))
  59. return
  60. }
  61. data := struct {
  62. models.SCompanyInfo
  63. ReceiverName string
  64. Code string
  65. }{
  66. ReceiverName: receiver.Name,
  67. Code: verification.Token,
  68. SCompanyInfo: info,
  69. }
  70. message = jsonutils.Marshal(data).String()
  71. notifyReceiver = api.SNotifyReceiver{
  72. Contact: receiver.Email,
  73. DomainId: receiver.DomainId,
  74. }
  75. emailMsg.To = append(emailMsg.To, receiver.Email)
  76. case api.MOBILE:
  77. message = fmt.Sprintf("[\"%s\"]", verification.Token)
  78. mobileArr := strings.Split(receiver.Mobile, " ")
  79. mobile := strings.Join(mobileArr, "")
  80. notifyReceiver = api.SNotifyReceiver{
  81. Contact: mobile,
  82. DomainId: receiver.DomainId,
  83. }
  84. default:
  85. // no way
  86. }
  87. tLang, err := receiver.GetTemplateLang(ctx)
  88. if err != nil {
  89. self.taskFailed(ctx, receiver, fmt.Sprintf("unable to GetTemplateLang for receiver %q: %v", receiver.Id, err))
  90. }
  91. param, err := models.TemplateManager.FillWithTemplate(ctx, tLang, api.SsNotification{
  92. ContactType: contactType,
  93. Topic: "VERIFY",
  94. Message: message,
  95. })
  96. if err != nil {
  97. self.taskFailed(ctx, receiver, err.Error())
  98. return
  99. }
  100. param.RemoteTemplateParam = api.SRemoteTemplateParam{Code: verification.Token}
  101. param.Receivers = notifyReceiver
  102. if len(emailMsg.Subject) == 0 {
  103. emailMsg.Subject = param.Title
  104. }
  105. if len(emailMsg.Body) == 0 {
  106. emailMsg.Body = param.Message
  107. }
  108. param.EmailMsg = *emailMsg
  109. driver := models.GetDriver(contactType)
  110. err = driver.Send(ctx, param)
  111. // err = models.NotifyService.Send(ctx, contactType, param)
  112. if err != nil {
  113. self.taskFailed(ctx, receiver, err.Error())
  114. return
  115. }
  116. logclient.AddActionLogWithContext(ctx, receiver, logclient.ACT_SEND_VERIFICATION, "", self.UserCred, true)
  117. self.SetStageComplete(ctx, nil)
  118. }