notifications.go
author Mikael Berthe <mikael@lilotux.net>
Wed, 10 May 2017 20:12:26 +0200
changeset 179 fbe21b4aabda
parent 159 408aa794d9bb
child 207 301d5b94be3f
permissions -rw-r--r--
Version 1.5.0
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
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 132
diff changeset
     7
package madon
95
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
156
70aadba26338 Add field "All" to LimitParams, change Limit behaviour
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
    16
// If lopt.All is true, several requests will be made until the API server
70aadba26338 Add field "All" to LimitParams, change Limit behaviour
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
    17
// has nothing to return.
70aadba26338 Add field "All" to LimitParams, change Limit behaviour
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
    18
// If lopt.Limit is set (and not All), several queries can be made until the
70aadba26338 Add field "All" to LimitParams, change Limit behaviour
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
    19
// limit is reached.
149
5f922977d7c7 Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents: 148
diff changeset
    20
func (mc *Client) GetNotifications(lopt *LimitParams) ([]Notification, error) {
95
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
	var notifications []Notification
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    22
	var links apiLinks
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    23
	if err := mc.apiCall("notifications", rest.Get, nil, lopt, &links, &notifications); err != nil {
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 102
diff changeset
    24
		return nil, err
95
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
	}
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    26
	if lopt != nil { // Fetch more pages to reach our limit
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    27
		var notifSlice []Notification
156
70aadba26338 Add field "All" to LimitParams, change Limit behaviour
Mikael Berthe <mikael@lilotux.net>
parents: 155
diff changeset
    28
		for (lopt.All || lopt.Limit > len(notifications)) && links.next != nil {
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    29
			newlopt := links.next
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    30
			links = apiLinks{}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    31
			if err := mc.apiCall("notifications", rest.Get, nil, newlopt, &links, &notifSlice); err != nil {
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    32
				return nil, err
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    33
			}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    34
			notifications = append(notifications, notifSlice...)
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    35
			notifSlice = notifSlice[:0] // Clear struct
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    36
		}
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    37
	}
95
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
	return notifications, nil
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
}
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
// GetNotification returns a notification
102
187aa2a668a5 Update comment
Mikael Berthe <mikael@lilotux.net>
parents: 99
diff changeset
    42
// 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
    43
// requested notification does not exist.
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 156
diff changeset
    44
func (mc *Client) GetNotification(notificationID int64) (*Notification, error) {
132
639bbcddb4fe Make identifiers less ambiguous
Mikael Berthe <mikael@lilotux.net>
parents: 130
diff changeset
    45
	if notificationID < 1 {
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 102
diff changeset
    46
		return nil, ErrInvalidID
95
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    47
	}
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    48
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 156
diff changeset
    49
	var endPoint = "notifications/" + strconv.FormatInt(notificationID, 10)
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 102
diff changeset
    50
	var notification Notification
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    51
	if err := mc.apiCall(endPoint, rest.Get, nil, nil, nil, &notification); err != nil {
120
579912e9d0ef Refactor API calls
Mikael Berthe <mikael@lilotux.net>
parents: 102
diff changeset
    52
		return nil, err
95
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    53
	}
99
6ec2a44a1bd1 Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    54
	if notification.ID == 0 {
6ec2a44a1bd1 Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    55
		return nil, ErrEntityNotFound
6ec2a44a1bd1 Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents: 98
diff changeset
    56
	}
95
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    57
	return &notification, nil
94f139efff39 Add GetNotifications()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    58
}
96
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    59
148
ae2cbcf18b55 Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
    60
// DismissNotification deletes a notification
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 156
diff changeset
    61
func (mc *Client) DismissNotification(notificationID int64) error {
148
ae2cbcf18b55 Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
    62
	if notificationID < 1 {
ae2cbcf18b55 Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
    63
		return ErrInvalidID
ae2cbcf18b55 Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
    64
	}
ae2cbcf18b55 Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
    65
ae2cbcf18b55 Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
    66
	endPoint := "notifications/dismiss"
159
408aa794d9bb s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents: 156
diff changeset
    67
	params := apiCallParams{"id": strconv.FormatInt(notificationID, 10)}
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    68
	err := mc.apiCall(endPoint, rest.Post, params, nil, nil, &Notification{})
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    69
	return err
148
ae2cbcf18b55 Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
    70
}
ae2cbcf18b55 Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents: 138
diff changeset
    71
96
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    72
// ClearNotifications deletes all notifications from the Mastodon server for
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    73
// the authenticated user
138
23d3a518d0ad Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents: 132
diff changeset
    74
func (mc *Client) ClearNotifications() error {
155
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    75
	err := mc.apiCall("notifications/clear", rest.Post, nil, nil, nil, &Notification{})
0c581e0108da Use links from headers
Mikael Berthe <mikael@lilotux.net>
parents: 149
diff changeset
    76
	return err
96
5496be0e3d4f Add ClearNotifications()
Mikael Berthe <mikael@lilotux.net>
parents: 95
diff changeset
    77
}