1 /* |
|
2 Copyright 2017-2018 Mikael Berthe |
|
3 |
|
4 Licensed under the MIT license. Please see the LICENSE file is this directory. |
|
5 */ |
|
6 |
|
7 package madon |
|
8 |
|
9 import ( |
|
10 "fmt" |
|
11 "strconv" |
|
12 |
|
13 "github.com/sendgrid/rest" |
|
14 ) |
|
15 |
|
16 // GetNotifications returns the list of the user's notifications |
|
17 // excludeTypes is an array of notifications to exclude ("follow", "favourite", |
|
18 // "reblog", "mention"). It can be nil. |
|
19 // If lopt.All is true, several requests will be made until the API server |
|
20 // has nothing to return. |
|
21 // If lopt.Limit is set (and not All), several queries can be made until the |
|
22 // limit is reached. |
|
23 func (mc *Client) GetNotifications(excludeTypes []string, lopt *LimitParams) ([]Notification, error) { |
|
24 var notifications []Notification |
|
25 var links apiLinks |
|
26 var params apiCallParams |
|
27 |
|
28 if len(excludeTypes) > 0 { |
|
29 params = make(apiCallParams) |
|
30 for i, eType := range excludeTypes { |
|
31 qID := fmt.Sprintf("[%d]exclude_types", i) |
|
32 params[qID] = eType |
|
33 } |
|
34 } |
|
35 |
|
36 if err := mc.apiCall("v1/notifications", rest.Get, params, lopt, &links, ¬ifications); err != nil { |
|
37 return nil, err |
|
38 } |
|
39 if lopt != nil { // Fetch more pages to reach our limit |
|
40 var notifSlice []Notification |
|
41 for (lopt.All || lopt.Limit > len(notifications)) && links.next != nil { |
|
42 newlopt := links.next |
|
43 links = apiLinks{} |
|
44 if err := mc.apiCall("v1/notifications", rest.Get, nil, newlopt, &links, ¬ifSlice); err != nil { |
|
45 return nil, err |
|
46 } |
|
47 notifications = append(notifications, notifSlice...) |
|
48 notifSlice = notifSlice[:0] // Clear struct |
|
49 } |
|
50 } |
|
51 return notifications, nil |
|
52 } |
|
53 |
|
54 // GetNotification returns a notification |
|
55 // The returned notification can be nil if there is an error or if the |
|
56 // requested notification does not exist. |
|
57 func (mc *Client) GetNotification(notificationID int64) (*Notification, error) { |
|
58 if notificationID < 1 { |
|
59 return nil, ErrInvalidID |
|
60 } |
|
61 |
|
62 var endPoint = "notifications/" + strconv.FormatInt(notificationID, 10) |
|
63 var notification Notification |
|
64 if err := mc.apiCall("v1/"+endPoint, rest.Get, nil, nil, nil, ¬ification); err != nil { |
|
65 return nil, err |
|
66 } |
|
67 if notification.ID == 0 { |
|
68 return nil, ErrEntityNotFound |
|
69 } |
|
70 return ¬ification, nil |
|
71 } |
|
72 |
|
73 // DismissNotification deletes a notification |
|
74 func (mc *Client) DismissNotification(notificationID int64) error { |
|
75 if notificationID < 1 { |
|
76 return ErrInvalidID |
|
77 } |
|
78 |
|
79 endPoint := "notifications/dismiss" |
|
80 params := apiCallParams{"id": strconv.FormatInt(notificationID, 10)} |
|
81 err := mc.apiCall("v1/"+endPoint, rest.Post, params, nil, nil, &Notification{}) |
|
82 return err |
|
83 } |
|
84 |
|
85 // ClearNotifications deletes all notifications from the Mastodon server for |
|
86 // the authenticated user |
|
87 func (mc *Client) ClearNotifications() error { |
|
88 err := mc.apiCall("v1/notifications/clear", rest.Post, nil, nil, nil, &Notification{}) |
|
89 return err |
|
90 } |
|