[notification] update _search defaults, fix /read (#53)
[notification] update _search defaults, fix /read Co-authored-by: Kassian Sun <kassiansun@outlook.com>
This commit is contained in:
parent
061b528309
commit
400bd2d002
|
@ -10,6 +10,6 @@ type NotificationAPI struct {
|
||||||
|
|
||||||
func InitAPI() {
|
func InitAPI() {
|
||||||
notification := NotificationAPI{}
|
notification := NotificationAPI{}
|
||||||
api.HandleAPIMethod(api.POST, "/notification/_search", notification.RequireLogin(notification.listNotifications))
|
api.HandleAPIMethod(api.POST, "/notification/_search", notification.RequireLogin(notification.searchNotifications))
|
||||||
api.HandleAPIMethod(api.POST, "/notification/read", notification.RequireLogin(notification.setNotificationsRead))
|
api.HandleAPIMethod(api.POST, "/notification/read", notification.RequireLogin(notification.setNotificationsRead))
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,14 +14,14 @@ import (
|
||||||
"infini.sh/framework/core/util"
|
"infini.sh/framework/core/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ListNotificationsRequest struct {
|
type SearchNotificationsRequest struct {
|
||||||
From int `json:"from"`
|
From int `json:"from"`
|
||||||
Size int `json:"size"`
|
Size int `json:"size"`
|
||||||
Status []model.NotificationStatus `json:"status"`
|
Status []model.NotificationStatus `json:"status"`
|
||||||
Types []model.NotificationType `json:"types"`
|
Types []model.NotificationType `json:"types"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *NotificationAPI) listNotifications(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
func (h *NotificationAPI) searchNotifications(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
|
||||||
user, err := rbac.FromUserContext(req.Context())
|
user, err := rbac.FromUserContext(req.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("failed to get user from context, err: %v", err)
|
log.Error("failed to get user from context, err: %v", err)
|
||||||
|
@ -35,11 +35,9 @@ func (h *NotificationAPI) listNotifications(w http.ResponseWriter, req *http.Req
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var reqData = ListNotificationsRequest{
|
var reqData = SearchNotificationsRequest{
|
||||||
From: 0,
|
From: 0,
|
||||||
Size: 20,
|
Size: 20,
|
||||||
Status: []model.NotificationStatus{model.NotificationStatusNew},
|
|
||||||
Types: []model.NotificationType{model.NotificationTypeNotification},
|
|
||||||
}
|
}
|
||||||
err = h.DecodeJSON(req, &reqData)
|
err = h.DecodeJSON(req, &reqData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -48,6 +46,16 @@ func (h *NotificationAPI) listNotifications(w http.ResponseWriter, req *http.Req
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
musts := []util.MapStr{
|
||||||
|
{"term": util.MapStr{"user_id": util.MapStr{"value": user.UserId}}},
|
||||||
|
}
|
||||||
|
if len(reqData.Status) > 0 {
|
||||||
|
musts = append(musts, util.MapStr{"terms": util.MapStr{"status": reqData.Status}})
|
||||||
|
}
|
||||||
|
if len(reqData.Types) > 0 {
|
||||||
|
musts = append(musts, util.MapStr{"terms": util.MapStr{"type": reqData.Types}})
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
queryDSL = util.MapStr{
|
queryDSL = util.MapStr{
|
||||||
"sort": []util.MapStr{
|
"sort": []util.MapStr{
|
||||||
|
@ -55,11 +63,7 @@ func (h *NotificationAPI) listNotifications(w http.ResponseWriter, req *http.Req
|
||||||
},
|
},
|
||||||
"query": util.MapStr{
|
"query": util.MapStr{
|
||||||
"bool": util.MapStr{
|
"bool": util.MapStr{
|
||||||
"must": []util.MapStr{
|
"must": musts,
|
||||||
{"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,
|
"size": reqData.Size, "from": reqData.From,
|
||||||
|
@ -67,7 +71,6 @@ func (h *NotificationAPI) listNotifications(w http.ResponseWriter, req *http.Req
|
||||||
)
|
)
|
||||||
|
|
||||||
q := orm.Query{}
|
q := orm.Query{}
|
||||||
log.Infof(util.MustToJSON(queryDSL))
|
|
||||||
q.RawQuery = util.MustToJSONBytes(queryDSL)
|
q.RawQuery = util.MustToJSONBytes(queryDSL)
|
||||||
|
|
||||||
err, res := orm.Search(&model.Notification{}, &q)
|
err, res := orm.Search(&model.Notification{}, &q)
|
||||||
|
@ -99,7 +102,10 @@ func (h *NotificationAPI) setNotificationsRead(w http.ResponseWriter, req *http.
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var reqData = SetNotificationsReadRequest{}
|
var reqData = SetNotificationsReadRequest{
|
||||||
|
Ids: []string{},
|
||||||
|
Types: []model.NotificationType{},
|
||||||
|
}
|
||||||
err = h.DecodeJSON(req, &reqData)
|
err = h.DecodeJSON(req, &reqData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("failed to parse request: ", err)
|
log.Error("failed to parse request: ", err)
|
||||||
|
@ -115,6 +121,7 @@ func (h *NotificationAPI) setNotificationsRead(w http.ResponseWriter, req *http.
|
||||||
util.MapStr{
|
util.MapStr{
|
||||||
"bool": util.MapStr{
|
"bool": util.MapStr{
|
||||||
"must": []util.MapStr{
|
"must": []util.MapStr{
|
||||||
|
{"term": util.MapStr{"user_id": util.MapStr{"value": user.UserId}}},
|
||||||
{
|
{
|
||||||
"terms": util.MapStr{
|
"terms": util.MapStr{
|
||||||
"_id": reqData.Ids,
|
"_id": reqData.Ids,
|
||||||
|
@ -133,9 +140,10 @@ func (h *NotificationAPI) setNotificationsRead(w http.ResponseWriter, req *http.
|
||||||
util.MapStr{
|
util.MapStr{
|
||||||
"bool": util.MapStr{
|
"bool": util.MapStr{
|
||||||
"must": []util.MapStr{
|
"must": []util.MapStr{
|
||||||
|
{"term": util.MapStr{"user_id": util.MapStr{"value": user.UserId}}},
|
||||||
{
|
{
|
||||||
"terms": util.MapStr{
|
"terms": util.MapStr{
|
||||||
"notification_type": reqData.Types,
|
"type": reqData.Types,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue