diff --git a/model/notification.go b/model/notification.go index 711477e9..49b14f28 100644 --- a/model/notification.go +++ b/model/notification.go @@ -7,33 +7,33 @@ import ( type NotificationType string const ( - NotificationTypeNotification NotificationType = "NOTIFICATION" - NotificationTypeTodo NotificationType = "TODO" + NotificationTypeNotification NotificationType = "notification" + NotificationTypeTodo NotificationType = "todo" ) type MessageType string const ( - MessageTypeNews MessageType = "NEWS" - MessageTypeAlerting MessageType = "ALERTING" - MessageTypeMigration MessageType = "MIGRATION" + MessageTypeNews MessageType = "news" + MessageTypeAlerting MessageType = "alerting" + MessageTypeMigration MessageType = "migration" ) type NotificationStatus string const ( - NotificationStatusNew NotificationStatus = "NEW" - NotificationStatusRead NotificationStatus = "READ" + NotificationStatusNew NotificationStatus = "new" + NotificationStatusRead NotificationStatus = "read" ) type Notification struct { orm.ORMObjectBase - UserId string `json:"user_id,omitempty" elastic_mapping:"user_id: { type: keyword }"` - NotificationType NotificationType `json:"notification_type,omitempty" elastic_mapping:"notification_type:{type:keyword,fields:{text: {type: text}}}"` - MessageType MessageType `json:"message_type,omitempty" elastic_mapping:"message_type:{type:keyword,fields:{text: {type: text}}}"` - Status NotificationStatus `json:"status,omitempty" elastic_mapping:"status: { type: keyword }"` - Title string `json:"title,omitempty" elastic_mapping:"title: { type: keyword }"` - Body string `json:"body,omitempty" elastic_mapping:"body: { type: keyword }"` - Link string `json:"link,omitempty" elastic_mapping:"link: { type: keyword }"` + UserId string `json:"user_id,omitempty" elastic_mapping:"user_id: { type: keyword }"` + Type NotificationType `json:"type,omitempty" elastic_mapping:"type:{type:keyword,fields:{text: {type: text}}}"` + MessageType MessageType `json:"message_type,omitempty" elastic_mapping:"message_type:{type:keyword,fields:{text: {type: text}}}"` + Status NotificationStatus `json:"status,omitempty" elastic_mapping:"status: { type: keyword }"` + Title string `json:"title,omitempty" elastic_mapping:"title: { type: keyword }"` + Body string `json:"body,omitempty" elastic_mapping:"body: { type: keyword }"` + Link string `json:"link,omitempty" elastic_mapping:"link: { type: keyword }"` } diff --git a/plugin/api/notification/api.go b/plugin/api/notification/api.go index 5ac6a5a3..176ba865 100644 --- a/plugin/api/notification/api.go +++ b/plugin/api/notification/api.go @@ -10,6 +10,6 @@ type NotificationAPI struct { func InitAPI() { notification := NotificationAPI{} - api.HandleAPIMethod(api.GET, "/notification/_search", notification.RequireLogin(notification.listNotifications)) + api.HandleAPIMethod(api.POST, "/notification/_search", notification.RequireLogin(notification.listNotifications)) api.HandleAPIMethod(api.POST, "/notification/read", notification.RequireLogin(notification.setNotificationsRead)) } diff --git a/plugin/api/notification/notification.go b/plugin/api/notification/notification.go index 51e4a6aa..c0a0d1f4 100644 --- a/plugin/api/notification/notification.go +++ b/plugin/api/notification/notification.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "net/http" - "strconv" "time" log "github.com/cihub/seelog" @@ -15,6 +14,13 @@ import ( "infini.sh/framework/core/util" ) +type ListNotificationsRequest struct { + From int `json:"from"` + Size int `json:"size"` + Status []model.NotificationStatus `json:"status"` + Types []model.NotificationType `json:"types"` +} + func (h *NotificationAPI) listNotifications(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { user, err := rbac.FromUserContext(req.Context()) if err != nil { @@ -29,34 +35,40 @@ func (h *NotificationAPI) listNotifications(w http.ResponseWriter, req *http.Req return } - var ( - queryDSL = `{ - "sort": [ - { "created": {"order": "desc"} } - ], - "query": { - "bool": { "must": [ - { "term": {"user_id": { "value": "%s" } } }, - { "term": {"status": { "value": "%s" } } } - ] } - }, - "size": %d, "from": %d - }` - strSize = h.GetParameterOrDefault(req, "size", "20") - strFrom = h.GetParameterOrDefault(req, "from", "0") - ) - size, _ := strconv.Atoi(strSize) - if size <= 0 { - size = 20 + var reqData = ListNotificationsRequest{ + From: 0, + Size: 20, + Status: []model.NotificationStatus{model.NotificationStatusNew}, + Types: []model.NotificationType{model.NotificationTypeNotification}, } - from, _ := strconv.Atoi(strFrom) - if from < 0 { - from = 0 + err = h.DecodeJSON(req, &reqData) + if err != nil { + log.Error("failed to parse request: ", err) + h.WriteError(w, err.Error(), http.StatusBadRequest) + return } + var ( + queryDSL = util.MapStr{ + "sort": []util.MapStr{ + {"created": util.MapStr{"order": "desc"}}, + }, + "query": util.MapStr{ + "bool": util.MapStr{ + "must": []util.MapStr{ + {"term": util.MapStr{"user_id": util.MapStr{"value": user.UserId}}}, + {"terms": util.MapStr{"status": reqData.Status}}, + {"terms": util.MapStr{"type": reqData.Types}}, + }, + }, + }, + "size": reqData.Size, "from": reqData.From, + } + ) + q := orm.Query{} - queryDSL = fmt.Sprintf(queryDSL, user.UserId, model.NotificationStatusNew, size, from) - q.RawQuery = util.UnsafeStringToBytes(queryDSL) + log.Infof(util.MustToJSON(queryDSL)) + q.RawQuery = util.MustToJSONBytes(queryDSL) err, res := orm.Search(&model.Notification{}, &q) if err != nil { diff --git a/plugin/migration/pipeline.go b/plugin/migration/pipeline.go index 695ade42..2b12b8b9 100644 --- a/plugin/migration/pipeline.go +++ b/plugin/migration/pipeline.go @@ -1472,13 +1472,13 @@ func (p *DispatcherProcessor) sendMajorTaskNotification(taskItem *task2.Task) { return } notification := &model.Notification{ - UserId: util.ToString(creatorID), - NotificationType: model.NotificationTypeNotification, - MessageType: model.MessageTypeMigration, - Status: model.NotificationStatusNew, - Title: title, - Body: body, - Link: link, + UserId: util.ToString(creatorID), + Type: model.NotificationTypeNotification, + MessageType: model.MessageTypeMigration, + Status: model.NotificationStatusNew, + Title: title, + Body: body, + Link: link, } err = orm.Create(nil, notification) if err != nil {