Merge pull request '[notification] update enum & list API' (#52) from feature/notification into master
This commit is contained in:
commit
bae99585c1
|
@ -7,30 +7,30 @@ 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}}}"`
|
||||
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 }"`
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -1473,7 +1473,7 @@ func (p *DispatcherProcessor) sendMajorTaskNotification(taskItem *task2.Task) {
|
|||
}
|
||||
notification := &model.Notification{
|
||||
UserId: util.ToString(creatorID),
|
||||
NotificationType: model.NotificationTypeNotification,
|
||||
Type: model.NotificationTypeNotification,
|
||||
MessageType: model.MessageTypeMigration,
|
||||
Status: model.NotificationStatusNew,
|
||||
Title: title,
|
||||
|
|
Loading…
Reference in New Issue