vendor/github.com/McKael/madon/v3/notifications.go
changeset 265 05c40b36d3b2
parent 242 2a9ec03fe5a1
child 268 4dd196a4ee7c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/vendor/github.com/McKael/madon/v3/notifications.go	Sat Feb 04 12:58:35 2023 +0100
@@ -0,0 +1,90 @@
+/*
+Copyright 2017-2018 Mikael Berthe
+
+Licensed under the MIT license.  Please see the LICENSE file is this directory.
+*/
+
+package madon
+
+import (
+	"fmt"
+	"strconv"
+
+	"github.com/sendgrid/rest"
+)
+
+// GetNotifications returns the list of the user's notifications
+// excludeTypes is an array of notifications to exclude ("follow", "favourite",
+// "reblog", "mention").  It can be nil.
+// 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(excludeTypes []string, lopt *LimitParams) ([]Notification, error) {
+	var notifications []Notification
+	var links apiLinks
+	var params apiCallParams
+
+	if len(excludeTypes) > 0 {
+		params = make(apiCallParams)
+		for i, eType := range excludeTypes {
+			qID := fmt.Sprintf("[%d]exclude_types", i)
+			params[qID] = eType
+		}
+	}
+
+	if err := mc.apiCall("v1/notifications", rest.Get, params, lopt, &links, &notifications); 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("v1/notifications", rest.Get, nil, newlopt, &links, &notifSlice); 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 int64) (*Notification, error) {
+	if notificationID < 1 {
+		return nil, ErrInvalidID
+	}
+
+	var endPoint = "notifications/" + strconv.FormatInt(notificationID, 10)
+	var notification Notification
+	if err := mc.apiCall("v1/"+endPoint, rest.Get, nil, nil, nil, &notification); err != nil {
+		return nil, err
+	}
+	if notification.ID == 0 {
+		return nil, ErrEntityNotFound
+	}
+	return &notification, nil
+}
+
+// DismissNotification deletes a notification
+func (mc *Client) DismissNotification(notificationID int64) error {
+	if notificationID < 1 {
+		return ErrInvalidID
+	}
+
+	endPoint := "notifications/dismiss"
+	params := apiCallParams{"id": strconv.FormatInt(notificationID, 10)}
+	err := mc.apiCall("v1/"+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("v1/notifications/clear", rest.Post, nil, nil, nil, &Notification{})
+	return err
+}