notifications.go
author Mikael Berthe <mikael@lilotux.net>
Mon, 17 Apr 2017 10:28:10 +0200
changeset 130 c450bb73f59a
parent 120 579912e9d0ef
child 132 639bbcddb4fe
permissions -rw-r--r--
Update credits
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
130
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     1
/*
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     2
Copyright 2017 Mikael Berthe
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     3
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     4
Licensed under the MIT license.  Please see the LICENSE file is this directory.
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     5
*/
c450bb73f59a Update credits
Mikael Berthe <mikael@lilotux.net>
parents: 120
diff changeset
     6
95
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
package gondole
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
import (
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
	"strconv"
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
	"github.com/sendgrid/rest"
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
)
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
// GetNotifications returns the list of the user's notifications
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
func (g *Client) GetNotifications() ([]Notification, error) {
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
	var notifications []Notification
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 102
diff changeset
    18
	if err := g.apiCall("notifications", rest.Get, nil, &notifications); err != nil {
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 102
diff changeset
    19
		return nil, err
95
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
	}
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
	return notifications, nil
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
}
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
// GetNotification returns a notification
102
187aa2a668a5 Update comment
Mikael Berthe <mikael@lilotux.net>
parents: 99
diff changeset
    25
// 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
    26
// requested notification does not exist.
95
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
func (g *Client) GetNotification(id int) (*Notification, error) {
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 102
diff changeset
    28
	if id < 1 {
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 102
diff changeset
    29
		return nil, ErrInvalidID
95
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
	}
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 102
diff changeset
    32
	var endPoint = "notifications/" + strconv.Itoa(id)
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 102
diff changeset
    33
	var notification Notification
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 102
diff changeset
    34
	if err := g.apiCall(endPoint, rest.Get, nil, &notification); err != nil {
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 102
diff changeset
    35
		return nil, err
95
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
	}
99
6ec2a44a1bd1 Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    37
	if notification.ID == 0 {
6ec2a44a1bd1 Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    38
		return nil, ErrEntityNotFound
6ec2a44a1bd1 Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    39
	}
95
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
	return &notification, nil
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
}
96
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    42
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    43
// ClearNotifications deletes all notifications from the Mastodon server for
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    44
// the authenticated user
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    45
func (g *Client) ClearNotifications() error {
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 102
diff changeset
    46
	return g.apiCall("notifications/clear", rest.Post, nil, &Notification{})
96
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    47
}