Add field "All" to LimitParams, change Limit behaviour
If All is true, the library will send several requests (if needed) until
the API server has sent all the results.
If not, and if a Limit is set, the library will try to fetch at least
this number of results.
/*
Copyright 2017 Mikael Berthe
Licensed under the MIT license. Please see the LICENSE file is this directory.
*/
package madon
import (
"strconv"
"github.com/sendgrid/rest"
)
// GetNotifications returns the list of the user's notifications
// If lopt.All is true, several requests will be made until the API server
// has nothing to return.
// If lopt.Limit is set (and not All), several queries can be made until the
// limit is reached.
func (mc *Client) GetNotifications(lopt *LimitParams) ([]Notification, error) {
var notifications []Notification
var links apiLinks
if err := mc.apiCall("notifications", rest.Get, nil, lopt, &links, ¬ifications); err != nil {
return nil, err
}
if lopt != nil { // Fetch more pages to reach our limit
var notifSlice []Notification
for (lopt.All || lopt.Limit > len(notifications)) && links.next != nil {
newlopt := links.next
links = apiLinks{}
if err := mc.apiCall("notifications", rest.Get, nil, newlopt, &links, ¬ifSlice); err != nil {
return nil, err
}
notifications = append(notifications, notifSlice...)
notifSlice = notifSlice[:0] // Clear struct
}
}
return notifications, nil
}
// GetNotification returns a notification
// The returned notification can be nil if there is an error or if the
// requested notification does not exist.
func (mc *Client) GetNotification(notificationID int) (*Notification, error) {
if notificationID < 1 {
return nil, ErrInvalidID
}
var endPoint = "notifications/" + strconv.Itoa(notificationID)
var notification Notification
if err := mc.apiCall(endPoint, rest.Get, nil, nil, nil, ¬ification); err != nil {
return nil, err
}
if notification.ID == 0 {
return nil, ErrEntityNotFound
}
return ¬ification, nil
}
// DismissNotification deletes a notification
func (mc *Client) DismissNotification(notificationID int) error {
if notificationID < 1 {
return ErrInvalidID
}
endPoint := "notifications/dismiss"
params := apiCallParams{"id": strconv.Itoa(notificationID)}
err := mc.apiCall(endPoint, rest.Post, params, nil, nil, &Notification{})
return err
}
// ClearNotifications deletes all notifications from the Mastodon server for
// the authenticated user
func (mc *Client) ClearNotifications() error {
err := mc.apiCall("notifications/clear", rest.Post, nil, nil, nil, &Notification{})
return err
}