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
|
type NotificationType string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
NotificationTypeNotification NotificationType = "NOTIFICATION"
|
NotificationTypeNotification NotificationType = "notification"
|
||||||
NotificationTypeTodo NotificationType = "TODO"
|
NotificationTypeTodo NotificationType = "todo"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MessageType string
|
type MessageType string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
MessageTypeNews MessageType = "NEWS"
|
MessageTypeNews MessageType = "news"
|
||||||
MessageTypeAlerting MessageType = "ALERTING"
|
MessageTypeAlerting MessageType = "alerting"
|
||||||
MessageTypeMigration MessageType = "MIGRATION"
|
MessageTypeMigration MessageType = "migration"
|
||||||
)
|
)
|
||||||
|
|
||||||
type NotificationStatus string
|
type NotificationStatus string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
NotificationStatusNew NotificationStatus = "NEW"
|
NotificationStatusNew NotificationStatus = "new"
|
||||||
NotificationStatusRead NotificationStatus = "READ"
|
NotificationStatusRead NotificationStatus = "read"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Notification struct {
|
type Notification struct {
|
||||||
orm.ORMObjectBase
|
orm.ORMObjectBase
|
||||||
|
|
||||||
UserId string `json:"user_id,omitempty" elastic_mapping:"user_id: { type: keyword }"`
|
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}}}"`
|
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 }"`
|
Status NotificationStatus `json:"status,omitempty" elastic_mapping:"status: { type: keyword }"`
|
||||||
Title string `json:"title,omitempty" elastic_mapping:"title: { type: keyword }"`
|
Title string `json:"title,omitempty" elastic_mapping:"title: { type: keyword }"`
|
||||||
|
|
|
@ -10,6 +10,6 @@ type NotificationAPI struct {
|
||||||
|
|
||||||
func InitAPI() {
|
func InitAPI() {
|
||||||
notification := NotificationAPI{}
|
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))
|
api.HandleAPIMethod(api.POST, "/notification/read", notification.RequireLogin(notification.setNotificationsRead))
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/cihub/seelog"
|
log "github.com/cihub/seelog"
|
||||||
|
@ -15,6 +14,13 @@ import (
|
||||||
"infini.sh/framework/core/util"
|
"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) {
|
func (h *NotificationAPI) listNotifications(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 {
|
||||||
|
@ -29,34 +35,40 @@ func (h *NotificationAPI) listNotifications(w http.ResponseWriter, req *http.Req
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var reqData = ListNotificationsRequest{
|
||||||
queryDSL = `{
|
From: 0,
|
||||||
"sort": [
|
Size: 20,
|
||||||
{ "created": {"order": "desc"} }
|
Status: []model.NotificationStatus{model.NotificationStatusNew},
|
||||||
],
|
Types: []model.NotificationType{model.NotificationTypeNotification},
|
||||||
"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
|
|
||||||
}
|
}
|
||||||
from, _ := strconv.Atoi(strFrom)
|
err = h.DecodeJSON(req, &reqData)
|
||||||
if from < 0 {
|
if err != nil {
|
||||||
from = 0
|
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{}
|
q := orm.Query{}
|
||||||
queryDSL = fmt.Sprintf(queryDSL, user.UserId, model.NotificationStatusNew, size, from)
|
log.Infof(util.MustToJSON(queryDSL))
|
||||||
q.RawQuery = util.UnsafeStringToBytes(queryDSL)
|
q.RawQuery = util.MustToJSONBytes(queryDSL)
|
||||||
|
|
||||||
err, res := orm.Search(&model.Notification{}, &q)
|
err, res := orm.Search(&model.Notification{}, &q)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -1473,7 +1473,7 @@ func (p *DispatcherProcessor) sendMajorTaskNotification(taskItem *task2.Task) {
|
||||||
}
|
}
|
||||||
notification := &model.Notification{
|
notification := &model.Notification{
|
||||||
UserId: util.ToString(creatorID),
|
UserId: util.ToString(creatorID),
|
||||||
NotificationType: model.NotificationTypeNotification,
|
Type: model.NotificationTypeNotification,
|
||||||
MessageType: model.MessageTypeMigration,
|
MessageType: model.MessageTypeMigration,
|
||||||
Status: model.NotificationStatusNew,
|
Status: model.NotificationStatusNew,
|
||||||
Title: title,
|
Title: title,
|
||||||
|
|
Loading…
Reference in New Issue