notification_group.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 models // import "yunion.io/x/onecloud/pkg/notify/models"
  15. import (
  16. "context"
  17. "fmt"
  18. "strings"
  19. "time"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/log"
  22. "yunion.io/x/pkg/errors"
  23. "yunion.io/x/sqlchemy"
  24. apis "yunion.io/x/onecloud/pkg/apis/notify"
  25. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  26. )
  27. type SNotificationGroupManager struct {
  28. db.SModelBaseManager
  29. }
  30. var NotificationGroupManager *SNotificationGroupManager
  31. func init() {
  32. NotificationGroupManager = &SNotificationGroupManager{
  33. SModelBaseManager: db.NewModelBaseManager(
  34. SNotificationGroup{},
  35. "notification_group_tbl",
  36. "notification_group",
  37. "notification_groups",
  38. ),
  39. }
  40. NotificationGroupManager.SetVirtualObject(NotificationGroupManager)
  41. }
  42. // 站内信
  43. type SNotificationGroup struct {
  44. db.SModelBase
  45. Id string `width:"128" nullable:"false" create:"required" list:"user" index:"true" get:"user"`
  46. GroupKey string `width:"128" nullable:"false" create:"required" list:"user" get:"user"`
  47. Title string
  48. // swagger:ignore
  49. Message string `length:"medium"`
  50. ReceiverId string `width:"128" nullable:"false" create:"required" list:"user" get:"user"`
  51. Body jsonutils.JSONObject
  52. Header jsonutils.JSONObject
  53. MsgKey string
  54. ContactType string `width:"32" nullable:"false" create:"required" list:"user" get:"user"`
  55. Contact string `width:"128" nullable:"false" create:"required" list:"user" get:"user"`
  56. CreatedAt time.Time
  57. DomainId string `width:"128" nullable:"false" create:"required" list:"user" get:"user"`
  58. }
  59. func (ng *SNotificationGroupManager) TaskCreate(ctx context.Context, contactType string, args apis.SendParams) error {
  60. if contactType == apis.WEBCONSOLE {
  61. return nil
  62. }
  63. insertNotificationGroup := SNotificationGroup{
  64. Id: db.DefaultUUIDGenerator(),
  65. ContactType: contactType,
  66. Body: args.Body,
  67. Header: args.Header,
  68. MsgKey: args.MsgKey,
  69. ReceiverId: args.ReceiverId,
  70. Title: args.Title,
  71. Message: args.Message,
  72. GroupKey: args.GroupKey,
  73. Contact: args.Receivers.Contact,
  74. CreatedAt: time.Now(),
  75. DomainId: args.DomainId,
  76. }
  77. if contactType == apis.EMAIL {
  78. insertNotificationGroup.Title = args.EmailMsg.Subject
  79. insertNotificationGroup.Message = args.EmailMsg.Body
  80. insertNotificationGroup.Contact = args.EmailMsg.To[0]
  81. }
  82. return NotificationGroupManager.TableSpec().Insert(ctx, &insertNotificationGroup)
  83. }
  84. func (ng *SNotificationGroupManager) TaskSend(ctx context.Context, input apis.SNotificationGroupSearchInput) (*apis.SendParams, error) {
  85. q := ng.Query()
  86. q = q.Between("created_at", input.StartTime, input.EndTime)
  87. q = q.Equals("group_key", input.GroupKey)
  88. q = q.Equals("receiver_id", input.ReceiverId)
  89. q = q.Equals("contact_type", input.ContactType)
  90. ngs := []SNotificationGroup{}
  91. err := db.FetchModelObjects(ng, q, &ngs)
  92. if err != nil {
  93. return nil, errors.Wrap(err, "fetch notification groups")
  94. }
  95. if len(ngs) <= 1 {
  96. return nil, errors.Wrapf(errors.ErrNotFound, "notification groups just found :%d", len(ngs))
  97. }
  98. sendParams := &apis.SendParams{
  99. Body: ngs[0].Body,
  100. Header: ngs[0].Header,
  101. MsgKey: ngs[0].MsgKey,
  102. Title: ngs[0].Title,
  103. ReceiverId: ngs[0].ReceiverId,
  104. Receivers: apis.SNotifyReceiver{
  105. Contact: ngs[0].Contact,
  106. },
  107. DomainId: ngs[0].DomainId,
  108. }
  109. msg := ""
  110. joinStr := " \n"
  111. sendParams.Message = msg
  112. if input.ContactType == apis.EMAIL {
  113. joinStr = " <br>"
  114. }
  115. deleteIds := []string{}
  116. for _, ng := range ngs {
  117. msg += fmt.Sprintf("%s %s", ng.Message, joinStr)
  118. deleteIds = append(deleteIds, fmt.Sprintf("'%s'", ng.Id))
  119. }
  120. defer func() {
  121. _, err = sqlchemy.Exec(fmt.Sprintf("delete from %s where id in (%s) ", ng.TableSpec().Name(), strings.Join(deleteIds, ",")))
  122. if err != nil {
  123. log.Errorln("clean notification_groups err:", err)
  124. }
  125. }()
  126. sendParams.Message = msg
  127. if input.ContactType == apis.EMAIL {
  128. sendParams.EmailMsg = apis.SEmailMessage{
  129. Subject: sendParams.Title,
  130. Body: msg,
  131. To: []string{ngs[0].Contact},
  132. }
  133. }
  134. return sendParams, nil
  135. }