2019-12-17 15:38:55 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2020-03-15 18:30:23 +00:00
|
|
|
package consoleapi
|
2019-12-17 15:38:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2019-12-28 09:34:09 +00:00
|
|
|
"strconv"
|
2019-12-17 15:38:55 +00:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/storj/storagenode/notifications"
|
|
|
|
)
|
|
|
|
|
2020-03-15 18:30:23 +00:00
|
|
|
// ErrNotificationsAPI - console notifications api error type.
|
|
|
|
var ErrNotificationsAPI = errs.Class("notifications console web error")
|
2019-12-17 15:38:55 +00:00
|
|
|
|
2020-03-15 18:30:23 +00:00
|
|
|
// Notifications is an api controller that exposes all notifications related api.
|
2019-12-17 15:38:55 +00:00
|
|
|
type Notifications struct {
|
|
|
|
service *notifications.Service
|
|
|
|
|
|
|
|
log *zap.Logger
|
|
|
|
}
|
|
|
|
|
2019-12-20 19:11:01 +00:00
|
|
|
// Page contains notifications and related information.
|
|
|
|
type Page struct {
|
|
|
|
Notifications []notifications.Notification `json:"notifications"`
|
|
|
|
|
|
|
|
Offset uint64 `json:"offset"`
|
|
|
|
Limit uint `json:"limit"`
|
|
|
|
CurrentPage uint `json:"currentPage"`
|
|
|
|
PageCount uint `json:"pageCount"`
|
|
|
|
}
|
|
|
|
|
2020-03-15 18:30:23 +00:00
|
|
|
// NewNotifications is a constructor for notifications controller.
|
2019-12-17 15:38:55 +00:00
|
|
|
func NewNotifications(log *zap.Logger, service *notifications.Service) *Notifications {
|
|
|
|
return &Notifications{
|
|
|
|
log: log,
|
|
|
|
service: service,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadNotification updates specific notification in database as read.
|
|
|
|
func (notification *Notifications) ReadNotification(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
2020-03-15 18:30:23 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
w.Header().Set(contentType, applicationJSON)
|
2019-12-17 15:38:55 +00:00
|
|
|
|
|
|
|
params := mux.Vars(r)
|
|
|
|
id, ok := params["id"]
|
|
|
|
if !ok {
|
2020-03-15 18:30:23 +00:00
|
|
|
notification.serveJSONError(w, http.StatusInternalServerError, ErrNotificationsAPI.Wrap(err))
|
2019-12-17 15:38:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
notificationID, err := uuid.Parse(id)
|
|
|
|
if err != nil {
|
2020-03-15 18:30:23 +00:00
|
|
|
notification.serveJSONError(w, http.StatusInternalServerError, ErrNotificationsAPI.Wrap(err))
|
2019-12-17 15:38:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = notification.service.Read(ctx, *notificationID)
|
|
|
|
if err != nil {
|
2020-03-15 18:30:23 +00:00
|
|
|
notification.serveJSONError(w, http.StatusInternalServerError, ErrNotificationsAPI.Wrap(err))
|
2019-12-17 15:38:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadAllNotifications updates all notifications in database as read.
|
|
|
|
func (notification *Notifications) ReadAllNotifications(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
2020-03-15 18:30:23 +00:00
|
|
|
var err error
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
w.Header().Set(contentType, applicationJSON)
|
2019-12-17 15:38:55 +00:00
|
|
|
|
2020-03-15 18:30:23 +00:00
|
|
|
err = notification.service.ReadAll(ctx)
|
2019-12-17 15:38:55 +00:00
|
|
|
if err != nil {
|
2020-03-15 18:30:23 +00:00
|
|
|
notification.serveJSONError(w, http.StatusInternalServerError, ErrNotificationsAPI.Wrap(err))
|
2019-12-17 15:38:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListNotifications returns listed page of notifications from database.
|
|
|
|
func (notification *Notifications) ListNotifications(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
var err error
|
2020-03-15 18:30:23 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
w.Header().Set(contentType, applicationJSON)
|
2019-12-17 15:38:55 +00:00
|
|
|
|
2019-12-28 09:34:09 +00:00
|
|
|
limit, err := strconv.ParseUint(r.URL.Query().Get("limit"), 10, 32)
|
|
|
|
if err != nil {
|
2020-03-15 18:30:23 +00:00
|
|
|
notification.serveJSONError(w, http.StatusBadRequest, ErrNotificationsAPI.Wrap(err))
|
2019-12-28 09:34:09 +00:00
|
|
|
return
|
2019-12-17 15:38:55 +00:00
|
|
|
}
|
|
|
|
|
2019-12-28 09:34:09 +00:00
|
|
|
page, err := strconv.ParseUint(r.URL.Query().Get("page"), 10, 32)
|
|
|
|
if err != nil {
|
2020-03-15 18:30:23 +00:00
|
|
|
notification.serveJSONError(w, http.StatusBadRequest, ErrNotificationsAPI.Wrap(err))
|
2019-12-28 09:34:09 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cursor := notifications.Cursor{
|
|
|
|
Limit: uint(limit),
|
|
|
|
Page: uint(page),
|
|
|
|
}
|
|
|
|
|
|
|
|
notificationList, err := notification.service.List(ctx, cursor)
|
2019-12-17 15:38:55 +00:00
|
|
|
if err != nil {
|
2020-03-15 18:30:23 +00:00
|
|
|
notification.serveJSONError(w, http.StatusInternalServerError, ErrNotificationsAPI.Wrap(err))
|
2019-12-17 15:38:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-28 09:34:09 +00:00
|
|
|
unreadCount, err := notification.service.UnreadAmount(ctx)
|
2019-12-17 15:38:55 +00:00
|
|
|
if err != nil {
|
2020-03-15 18:30:23 +00:00
|
|
|
notification.serveJSONError(w, http.StatusInternalServerError, ErrNotificationsAPI.Wrap(err))
|
2019-12-17 15:38:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-28 09:34:09 +00:00
|
|
|
var result struct {
|
2019-12-20 19:11:01 +00:00
|
|
|
Page Page `json:"page"`
|
|
|
|
UnreadCount int `json:"unreadCount"`
|
|
|
|
TotalCount int `json:"totalCount"`
|
2019-12-28 09:34:09 +00:00
|
|
|
}
|
|
|
|
|
2019-12-20 19:11:01 +00:00
|
|
|
result.Page.Notifications = notificationList.Notifications
|
|
|
|
result.Page.Limit = notificationList.Limit
|
|
|
|
result.Page.CurrentPage = notificationList.CurrentPage
|
|
|
|
result.Page.Offset = notificationList.Offset
|
|
|
|
result.Page.PageCount = notificationList.PageCount
|
2019-12-28 09:34:09 +00:00
|
|
|
result.UnreadCount = unreadCount
|
2019-12-20 19:11:01 +00:00
|
|
|
result.TotalCount = int(notificationList.TotalCount)
|
2019-12-28 09:34:09 +00:00
|
|
|
|
2020-03-15 18:30:23 +00:00
|
|
|
if err := json.NewEncoder(w).Encode(result); err != nil {
|
|
|
|
notification.log.Error("failed to encode json list notifications response", zap.Error(ErrNotificationsAPI.Wrap(err)))
|
|
|
|
return
|
2019-12-17 15:38:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 18:30:23 +00:00
|
|
|
// serveJSONError writes JSON error to response output stream.
|
|
|
|
func (notification *Notifications) serveJSONError(w http.ResponseWriter, status int, err error) {
|
2019-12-17 15:38:55 +00:00
|
|
|
w.WriteHeader(status)
|
|
|
|
|
2020-03-15 18:30:23 +00:00
|
|
|
var response struct {
|
|
|
|
Error string `json:"error"`
|
|
|
|
}
|
|
|
|
|
|
|
|
response.Error = err.Error()
|
2019-12-17 15:38:55 +00:00
|
|
|
|
2020-03-15 18:30:23 +00:00
|
|
|
err = json.NewEncoder(w).Encode(response)
|
|
|
|
if err != nil {
|
|
|
|
notification.log.Error("failed to write json error response", zap.Error(ErrNotificationsAPI.Wrap(err)))
|
|
|
|
return
|
2019-12-17 15:38:55 +00:00
|
|
|
}
|
|
|
|
}
|