author | Mikael Berthe <mikael@lilotux.net> |
Sat, 29 Apr 2017 12:16:16 +0200 | |
changeset 155 | 0c581e0108da |
parent 149 | 5f922977d7c7 |
child 156 | 70aadba26338 |
permissions | -rw-r--r-- |
130 | 1 |
/* |
2 |
Copyright 2017 Mikael Berthe |
|
3 |
||
4 |
Licensed under the MIT license. Please see the LICENSE file is this directory. |
|
5 |
*/ |
|
6 |
||
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
132
diff
changeset
|
7 |
package madon |
95 | 8 |
|
9 |
import ( |
|
10 |
"strconv" |
|
11 |
||
12 |
"github.com/sendgrid/rest" |
|
13 |
) |
|
14 |
||
15 |
// GetNotifications returns the list of the user's notifications |
|
149
5f922977d7c7
Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents:
148
diff
changeset
|
16 |
func (mc *Client) GetNotifications(lopt *LimitParams) ([]Notification, error) { |
95 | 17 |
var notifications []Notification |
155 | 18 |
var links apiLinks |
19 |
if err := mc.apiCall("notifications", rest.Get, nil, lopt, &links, ¬ifications); err != nil { |
|
120 | 20 |
return nil, err |
95 | 21 |
} |
155 | 22 |
if lopt != nil { // Fetch more pages to reach our limit |
23 |
var notifSlice []Notification |
|
24 |
for lopt.Limit > len(notifications) && links.next != nil { |
|
25 |
newlopt := links.next |
|
26 |
links = apiLinks{} |
|
27 |
if err := mc.apiCall("notifications", rest.Get, nil, newlopt, &links, ¬ifSlice); err != nil { |
|
28 |
return nil, err |
|
29 |
} |
|
30 |
notifications = append(notifications, notifSlice...) |
|
31 |
notifSlice = notifSlice[:0] // Clear struct |
|
32 |
} |
|
33 |
} |
|
95 | 34 |
return notifications, nil |
35 |
} |
|
36 |
||
37 |
// GetNotification returns a notification |
|
102 | 38 |
// The returned notification can be nil if there is an error or if the |
39 |
// requested notification does not exist. |
|
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
132
diff
changeset
|
40 |
func (mc *Client) GetNotification(notificationID int) (*Notification, error) { |
132
639bbcddb4fe
Make identifiers less ambiguous
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
41 |
if notificationID < 1 { |
120 | 42 |
return nil, ErrInvalidID |
95 | 43 |
} |
44 |
||
132
639bbcddb4fe
Make identifiers less ambiguous
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
45 |
var endPoint = "notifications/" + strconv.Itoa(notificationID) |
120 | 46 |
var notification Notification |
155 | 47 |
if err := mc.apiCall(endPoint, rest.Get, nil, nil, nil, ¬ification); err != nil { |
120 | 48 |
return nil, err |
95 | 49 |
} |
99
6ec2a44a1bd1
Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
50 |
if notification.ID == 0 { |
6ec2a44a1bd1
Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
51 |
return nil, ErrEntityNotFound |
6ec2a44a1bd1
Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
52 |
} |
95 | 53 |
return ¬ification, nil |
54 |
} |
|
96 | 55 |
|
148
ae2cbcf18b55
Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
56 |
// DismissNotification deletes a notification |
ae2cbcf18b55
Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
57 |
func (mc *Client) DismissNotification(notificationID int) error { |
ae2cbcf18b55
Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
58 |
if notificationID < 1 { |
ae2cbcf18b55
Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
59 |
return ErrInvalidID |
ae2cbcf18b55
Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
60 |
} |
ae2cbcf18b55
Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
61 |
|
ae2cbcf18b55
Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
62 |
endPoint := "notifications/dismiss" |
ae2cbcf18b55
Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
63 |
params := apiCallParams{"id": strconv.Itoa(notificationID)} |
155 | 64 |
err := mc.apiCall(endPoint, rest.Post, params, nil, nil, &Notification{}) |
65 |
return err |
|
148
ae2cbcf18b55
Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
66 |
} |
ae2cbcf18b55
Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
67 |
|
96 | 68 |
// ClearNotifications deletes all notifications from the Mastodon server for |
69 |
// the authenticated user |
|
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
132
diff
changeset
|
70 |
func (mc *Client) ClearNotifications() error { |
155 | 71 |
err := mc.apiCall("notifications/clear", rest.Post, nil, nil, nil, &Notification{}) |
72 |
return err |
|
96 | 73 |
} |