author | Mikael Berthe <mikael@lilotux.net> |
Sat, 29 Apr 2017 17:27:15 +0200 | |
changeset 156 | 70aadba26338 |
parent 155 | 0c581e0108da |
child 159 | 408aa794d9bb |
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 |
|
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 | 21 |
var notifications []Notification |
155 | 22 |
var links apiLinks |
23 |
if err := mc.apiCall("notifications", rest.Get, nil, lopt, &links, ¬ifications); err != nil { |
|
120 | 24 |
return nil, err |
95 | 25 |
} |
155 | 26 |
if lopt != nil { // Fetch more pages to reach our limit |
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 | 29 |
newlopt := links.next |
30 |
links = apiLinks{} |
|
31 |
if err := mc.apiCall("notifications", rest.Get, nil, newlopt, &links, ¬ifSlice); err != nil { |
|
32 |
return nil, err |
|
33 |
} |
|
34 |
notifications = append(notifications, notifSlice...) |
|
35 |
notifSlice = notifSlice[:0] // Clear struct |
|
36 |
} |
|
37 |
} |
|
95 | 38 |
return notifications, nil |
39 |
} |
|
40 |
||
41 |
// GetNotification returns a notification |
|
102 | 42 |
// The returned notification can be nil if there is an error or if the |
43 |
// requested notification does not exist. |
|
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
132
diff
changeset
|
44 |
func (mc *Client) GetNotification(notificationID int) (*Notification, error) { |
132
639bbcddb4fe
Make identifiers less ambiguous
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
45 |
if notificationID < 1 { |
120 | 46 |
return nil, ErrInvalidID |
95 | 47 |
} |
48 |
||
132
639bbcddb4fe
Make identifiers less ambiguous
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
49 |
var endPoint = "notifications/" + strconv.Itoa(notificationID) |
120 | 50 |
var notification Notification |
155 | 51 |
if err := mc.apiCall(endPoint, rest.Get, nil, nil, nil, ¬ification); err != nil { |
120 | 52 |
return nil, err |
95 | 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 | 57 |
return ¬ification, nil |
58 |
} |
|
96 | 59 |
|
148
ae2cbcf18b55
Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
60 |
// DismissNotification deletes a notification |
ae2cbcf18b55
Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
61 |
func (mc *Client) DismissNotification(notificationID int) error { |
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" |
ae2cbcf18b55
Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
67 |
params := apiCallParams{"id": strconv.Itoa(notificationID)} |
155 | 68 |
err := mc.apiCall(endPoint, rest.Post, params, nil, nil, &Notification{}) |
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 | 72 |
// ClearNotifications deletes all notifications from the Mastodon server for |
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 | 75 |
err := mc.apiCall("notifications/clear", rest.Post, nil, nil, nil, &Notification{}) |
76 |
return err |
|
96 | 77 |
} |