author | Mikael Berthe <mikael@lilotux.net> |
Sat, 29 Sep 2018 11:46:33 +0200 | |
changeset 246 | 3dcfc59d90b9 |
parent 243 | 7386c6a454a8 |
permissions | -rw-r--r-- |
130 | 1 |
/* |
207 | 2 |
Copyright 2017-2018 Mikael Berthe |
130 | 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 ( |
|
212
21d7dc38c0e4
GetNotifications: Add excludeTypes
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
10 |
"fmt" |
95 | 11 |
"strconv" |
12 |
||
13 |
"github.com/sendgrid/rest" |
|
14 |
) |
|
15 |
||
16 |
// GetNotifications returns the list of the user's notifications |
|
212
21d7dc38c0e4
GetNotifications: Add excludeTypes
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
17 |
// excludeTypes is an array of notifications to exclude ("follow", "favourite", |
21d7dc38c0e4
GetNotifications: Add excludeTypes
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
18 |
// "reblog", "mention"). It can be nil. |
156
70aadba26338
Add field "All" to LimitParams, change Limit behaviour
Mikael Berthe <mikael@lilotux.net>
parents:
155
diff
changeset
|
19 |
// 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
|
20 |
// has nothing to return. |
70aadba26338
Add field "All" to LimitParams, change Limit behaviour
Mikael Berthe <mikael@lilotux.net>
parents:
155
diff
changeset
|
21 |
// 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
|
22 |
// limit is reached. |
212
21d7dc38c0e4
GetNotifications: Add excludeTypes
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
23 |
func (mc *Client) GetNotifications(excludeTypes []string, lopt *LimitParams) ([]Notification, error) { |
95 | 24 |
var notifications []Notification |
155 | 25 |
var links apiLinks |
212
21d7dc38c0e4
GetNotifications: Add excludeTypes
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
26 |
var params apiCallParams |
21d7dc38c0e4
GetNotifications: Add excludeTypes
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
27 |
|
21d7dc38c0e4
GetNotifications: Add excludeTypes
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
28 |
if len(excludeTypes) > 0 { |
21d7dc38c0e4
GetNotifications: Add excludeTypes
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
29 |
params = make(apiCallParams) |
21d7dc38c0e4
GetNotifications: Add excludeTypes
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
30 |
for i, eType := range excludeTypes { |
243
7386c6a454a8
Change the way parameter lists are handled internally
Mikael Berthe <mikael@lilotux.net>
parents:
238
diff
changeset
|
31 |
qID := fmt.Sprintf("[%d]exclude_types", i) |
212
21d7dc38c0e4
GetNotifications: Add excludeTypes
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
32 |
params[qID] = eType |
21d7dc38c0e4
GetNotifications: Add excludeTypes
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
33 |
} |
21d7dc38c0e4
GetNotifications: Add excludeTypes
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
34 |
} |
21d7dc38c0e4
GetNotifications: Add excludeTypes
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
35 |
|
238
1c0042e76902
Do not use a global API version
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
36 |
if err := mc.apiCall("v1/notifications", rest.Get, params, lopt, &links, ¬ifications); err != nil { |
120 | 37 |
return nil, err |
95 | 38 |
} |
155 | 39 |
if lopt != nil { // Fetch more pages to reach our limit |
40 |
var notifSlice []Notification |
|
156
70aadba26338
Add field "All" to LimitParams, change Limit behaviour
Mikael Berthe <mikael@lilotux.net>
parents:
155
diff
changeset
|
41 |
for (lopt.All || lopt.Limit > len(notifications)) && links.next != nil { |
155 | 42 |
newlopt := links.next |
43 |
links = apiLinks{} |
|
238
1c0042e76902
Do not use a global API version
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
44 |
if err := mc.apiCall("v1/notifications", rest.Get, nil, newlopt, &links, ¬ifSlice); err != nil { |
155 | 45 |
return nil, err |
46 |
} |
|
47 |
notifications = append(notifications, notifSlice...) |
|
48 |
notifSlice = notifSlice[:0] // Clear struct |
|
49 |
} |
|
50 |
} |
|
95 | 51 |
return notifications, nil |
52 |
} |
|
53 |
||
54 |
// GetNotification returns a notification |
|
102 | 55 |
// The returned notification can be nil if there is an error or if the |
56 |
// requested notification does not exist. |
|
159
408aa794d9bb
s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents:
156
diff
changeset
|
57 |
func (mc *Client) GetNotification(notificationID int64) (*Notification, error) { |
132
639bbcddb4fe
Make identifiers less ambiguous
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
58 |
if notificationID < 1 { |
120 | 59 |
return nil, ErrInvalidID |
95 | 60 |
} |
61 |
||
159
408aa794d9bb
s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents:
156
diff
changeset
|
62 |
var endPoint = "notifications/" + strconv.FormatInt(notificationID, 10) |
120 | 63 |
var notification Notification |
238
1c0042e76902
Do not use a global API version
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
64 |
if err := mc.apiCall("v1/"+endPoint, rest.Get, nil, nil, nil, ¬ification); err != nil { |
120 | 65 |
return nil, err |
95 | 66 |
} |
99
6ec2a44a1bd1
Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
67 |
if notification.ID == 0 { |
6ec2a44a1bd1
Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
68 |
return nil, ErrEntityNotFound |
6ec2a44a1bd1
Return error rather than empty entities
Mikael Berthe <mikael@lilotux.net>
parents:
98
diff
changeset
|
69 |
} |
95 | 70 |
return ¬ification, nil |
71 |
} |
|
96 | 72 |
|
148
ae2cbcf18b55
Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
73 |
// DismissNotification deletes a notification |
159
408aa794d9bb
s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents:
156
diff
changeset
|
74 |
func (mc *Client) DismissNotification(notificationID int64) error { |
148
ae2cbcf18b55
Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
75 |
if notificationID < 1 { |
ae2cbcf18b55
Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
76 |
return ErrInvalidID |
ae2cbcf18b55
Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
77 |
} |
ae2cbcf18b55
Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
78 |
|
ae2cbcf18b55
Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
79 |
endPoint := "notifications/dismiss" |
159
408aa794d9bb
s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents:
156
diff
changeset
|
80 |
params := apiCallParams{"id": strconv.FormatInt(notificationID, 10)} |
238
1c0042e76902
Do not use a global API version
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
81 |
err := mc.apiCall("v1/"+endPoint, rest.Post, params, nil, nil, &Notification{}) |
155 | 82 |
return err |
148
ae2cbcf18b55
Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
83 |
} |
ae2cbcf18b55
Add DismissNotification (Mastodon 1.3+)
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
84 |
|
96 | 85 |
// ClearNotifications deletes all notifications from the Mastodon server for |
86 |
// the authenticated user |
|
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
132
diff
changeset
|
87 |
func (mc *Client) ClearNotifications() error { |
238
1c0042e76902
Do not use a global API version
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
88 |
err := mc.apiCall("v1/notifications/clear", rest.Post, nil, nil, nil, &Notification{}) |
155 | 89 |
return err |
96 | 90 |
} |