2018-10-17 09:15:27 +00:00
|
|
|
package gonextcloud
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
req "github.com/levigross/grequests"
|
2018-10-26 13:35:50 +00:00
|
|
|
"gitlab.bertha.cloud/partitio/Nextcloud-Partitio/gonextcloud/types"
|
2018-10-17 09:15:27 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//Notifications contains all Notifications available actions
|
|
|
|
type Notifications struct {
|
|
|
|
c *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
//List returns all the notifications
|
|
|
|
func (n *Notifications) List() ([]types.Notification, error) {
|
|
|
|
if err := n.Available(); err != nil {
|
2018-10-17 09:15:27 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-10-21 12:53:12 +00:00
|
|
|
res, err := n.c.baseRequest(http.MethodGet, routes.notifications, nil)
|
2018-10-17 09:15:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var r types.NotificationsListResponse
|
|
|
|
res.JSON(&r)
|
|
|
|
return r.Ocs.Data, nil
|
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//Get returns the notification corresponding to the id
|
|
|
|
func (n *Notifications) Get(id int) (types.Notification, error) {
|
|
|
|
if err := n.Available(); err != nil {
|
2018-10-17 09:15:27 +00:00
|
|
|
return types.Notification{}, err
|
|
|
|
}
|
2018-10-21 12:53:12 +00:00
|
|
|
res, err := n.c.baseRequest(http.MethodGet, routes.notifications, nil, strconv.Itoa(id))
|
2018-10-17 09:15:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return types.Notification{}, err
|
|
|
|
}
|
|
|
|
var r types.NotificationResponse
|
|
|
|
res.JSON(&r)
|
|
|
|
return r.Ocs.Data, nil
|
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//Delete deletes the notification corresponding to the id
|
|
|
|
func (n *Notifications) Delete(id int) error {
|
|
|
|
if err := n.Available(); err != nil {
|
2018-10-17 09:15:27 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-10-21 12:53:12 +00:00
|
|
|
_, err := n.c.baseRequest(http.MethodDelete, routes.notifications, nil, strconv.Itoa(id))
|
2018-10-17 09:15:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//DeleteAll deletes all notifications
|
|
|
|
func (n *Notifications) DeleteAll() error {
|
|
|
|
if err := n.Available(); err != nil {
|
2018-10-17 09:15:27 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-10-21 12:53:12 +00:00
|
|
|
_, err := n.c.baseRequest(http.MethodDelete, routes.notifications, nil)
|
2018-10-17 09:15:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//Create creates a notification (if the user is an admin)
|
|
|
|
func (n *Notifications) Create(userID, title, message string) error {
|
|
|
|
if err := n.AdminAvailable(); err != nil {
|
2018-10-17 09:15:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
ro := &req.RequestOptions{
|
|
|
|
Data: map[string]string{
|
|
|
|
"shortMessage": title,
|
|
|
|
"longMessage": message,
|
|
|
|
},
|
|
|
|
}
|
2018-10-21 12:53:12 +00:00
|
|
|
_, err := n.c.baseRequest(http.MethodPost, routes.adminNotifications, ro, userID)
|
2018-10-17 09:15:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-21 12:53:12 +00:00
|
|
|
//AdminAvailable returns an error if the admin-notifications app is not installed
|
|
|
|
func (n *Notifications) AdminAvailable() error {
|
|
|
|
if len(n.c.capabilities.Notifications.AdminNotifications) == 0 {
|
2018-10-17 09:15:27 +00:00
|
|
|
return errors.New("'admin notifications' not available on this instance")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-10-21 12:53:12 +00:00
|
|
|
|
|
|
|
//Available returns an error if the notifications app is not installed
|
|
|
|
func (n *Notifications) Available() error {
|
|
|
|
if len(n.c.capabilities.Notifications.OcsEndpoints) == 0 {
|
2018-10-17 09:15:27 +00:00
|
|
|
return errors.New("notifications not available on this instance")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|