notifications.go
author Mikael Berthe <mikael@lilotux.net>
Sat, 15 Apr 2017 21:08:34 +0200
changeset 125 2bbb72b9ebf6
parent 120 579912e9d0ef
child 130 c450bb73f59a
permissions -rw-r--r--
Rework the API wrappers to handle arrays of parameters This make some API calls work better (reports with several statuses, statuses with several attachments, relationships for multiple accounts...).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
95
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
package gondole
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
import (
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
	"strconv"
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
	"github.com/sendgrid/rest"
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
)
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
// GetNotifications returns the list of the user's notifications
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
func (g *Client) GetNotifications() ([]Notification, error) {
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
	var notifications []Notification
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 102
diff changeset
    12
	if err := g.apiCall("notifications", rest.Get, nil, &notifications); err != nil {
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 102
diff changeset
    13
		return nil, err
95
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
	}
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
	return notifications, nil
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
}
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
// GetNotification returns a notification
102
187aa2a668a5 Update comment
Mikael Berthe <mikael@lilotux.net>
parents: 99
diff changeset
    19
// The returned notification can be nil if there is an error or if the
187aa2a668a5 Update comment
Mikael Berthe <mikael@lilotux.net>
parents: 99
diff changeset
    20
// requested notification does not exist.
95
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
func (g *Client) GetNotification(id int) (*Notification, error) {
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 102
diff changeset
    22
	if id < 1 {
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 102
diff changeset
    23
		return nil, ErrInvalidID
95
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
	}
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 102
diff changeset
    26
	var endPoint = "notifications/" + strconv.Itoa(id)
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 102
diff changeset
    27
	var notification Notification
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 102
diff changeset
    28
	if err := g.apiCall(endPoint, rest.Get, nil, &notification); err != nil {
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 102
diff changeset
    29
		return nil, err
95
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
	}
99
6ec2a44a1bd1 Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    31
	if notification.ID == 0 {
6ec2a44a1bd1 Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    32
		return nil, ErrEntityNotFound
6ec2a44a1bd1 Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    33
	}
95
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
	return &notification, nil
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
}
96
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    36
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    37
// ClearNotifications deletes all notifications from the Mastodon server for
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    38
// the authenticated user
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    39
func (g *Client) ClearNotifications() error {
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 102
diff changeset
    40
	return g.apiCall("notifications/clear", rest.Post, nil, &Notification{})
96
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    41
}