mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-21 06:09:39 +08:00
feat(notify): add params for create notification
1. tag, usually used to filter 2. metadata 3. ignore_nonexistent_receiver
This commit is contained in:
@@ -58,6 +58,8 @@ const (
|
||||
NOTIFICATION_STATUS_OK = "ok"
|
||||
NOTIFICATION_STATUS_PART_OK = "part_ok"
|
||||
|
||||
NOTIFICATION_TAG_ALERT = "alert"
|
||||
|
||||
TEMPLATE_TYPE_TITLE = "title"
|
||||
TEMPLATE_TYPE_CONTENT = "content"
|
||||
TEMPLATE_TYPE_REMOTE = "remote"
|
||||
|
||||
@@ -46,6 +46,12 @@ type NotificationCreateInput struct {
|
||||
// description: message content or jsonobject
|
||||
// required: ture
|
||||
Message string `json:"message"`
|
||||
// description: notification tag
|
||||
// required: false
|
||||
// example: alert
|
||||
Tag string `json:"tag"`
|
||||
Metadata map[string]interface{} `json:"metadata"`
|
||||
IgnoreNonexistentReceiver bool `json:"ignore_nonexistent_receiver"`
|
||||
}
|
||||
|
||||
type ReceiveDetail struct {
|
||||
@@ -73,4 +79,5 @@ type NotificationListInput struct {
|
||||
|
||||
ContactType string
|
||||
ReceiverId string
|
||||
Tag string
|
||||
}
|
||||
|
||||
@@ -70,6 +70,7 @@ type SNotification struct {
|
||||
Message string `create:"required"`
|
||||
ReceivedAt time.Time `nullable:"true" list:"user" get:"user"`
|
||||
SendTimes int
|
||||
Tag string `width:"16" nullable:"true" index:"true" create:"optional"`
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -77,6 +78,9 @@ const (
|
||||
)
|
||||
|
||||
func (nm *SNotificationManager) ValidateCreateData(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, input api.NotificationCreateInput) (api.NotificationCreateInput, error) {
|
||||
if len(input.Tag) == 0 && utils.IsInStringArray(input.Tag, []string{api.NOTIFICATION_TAG_ALERT}) {
|
||||
return input, httperrors.NewInputParameterError("invalid tag")
|
||||
}
|
||||
// compatible
|
||||
if len(input.Receivers) != 0 && input.ContactType == api.WEBCONSOLE {
|
||||
input.Contacts = input.Receivers
|
||||
@@ -115,9 +119,14 @@ func (nm *SNotificationManager) ValidateCreateData(ctx context.Context, userCred
|
||||
if idSet.Has(re) || nameSet.Has(re) {
|
||||
continue
|
||||
}
|
||||
return input, httperrors.NewInputParameterError("no such receiver whose uid is %q", re)
|
||||
if !input.IgnoreNonexistentReceiver {
|
||||
return input, httperrors.NewInputParameterError("no such receiver whose uid is %q", re)
|
||||
}
|
||||
}
|
||||
input.Receivers = idSet.UnsortedList()
|
||||
if len(input.Receivers)+len(input.Contacts) == 0 {
|
||||
return input, httperrors.NewInputParameterError("no valid receiver or contact")
|
||||
}
|
||||
}
|
||||
nowStr := time.Now().Format("2006-01-02 15:04:05")
|
||||
if len(input.Priority) == 0 {
|
||||
@@ -160,6 +169,15 @@ func (n *SNotification) CustomizeCreate(ctx context.Context, userCred mcclient.T
|
||||
}
|
||||
|
||||
func (n *SNotification) PostCreate(ctx context.Context, userCred mcclient.TokenCredential, ownerId mcclient.IIdentityProvider, query jsonutils.JSONObject, data jsonutils.JSONObject) {
|
||||
if data.Contains("metadata") {
|
||||
metadata := make(map[string]interface{})
|
||||
err := data.Unmarshal(&metadata, "metadata")
|
||||
if err != nil {
|
||||
log.Errorf("unable to unmarshal to metadata: %v", err)
|
||||
} else {
|
||||
n.SetAllMetadata(ctx, metadata, userCred)
|
||||
}
|
||||
}
|
||||
n.SetStatus(userCred, api.NOTIFICATION_STATUS_RECEIVED, "")
|
||||
task, err := taskman.TaskManager.NewTask(ctx, "NotificationSendTask", n, userCred, nil, "", "")
|
||||
if err != nil {
|
||||
@@ -442,6 +460,9 @@ func (nm *SNotificationManager) ListItemFilter(ctx context.Context, q *sqlchemy.
|
||||
subq := ReceiverNotificationManager.Query("notification_id").Equals("receiver_id", input.ReceiverId).SubQuery()
|
||||
q = q.Join(subq, sqlchemy.Equals(q.Field("id"), subq.Field("notification_id")))
|
||||
}
|
||||
if len(input.Tag) > 0 {
|
||||
q = q.Equals("tag", input.Tag)
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user