1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "fmt"
11 "time"
12 )
13
14
15 type Notification struct {
16 ID *string `json:"id,omitempty"`
17 Repository *Repository `json:"repository,omitempty"`
18 Subject *NotificationSubject `json:"subject,omitempty"`
19
20
21
22
23 Reason *string `json:"reason,omitempty"`
24
25 Unread *bool `json:"unread,omitempty"`
26 UpdatedAt *time.Time `json:"updated_at,omitempty"`
27 LastReadAt *time.Time `json:"last_read_at,omitempty"`
28 URL *string `json:"url,omitempty"`
29 }
30
31
32 type NotificationSubject struct {
33 Title *string `json:"title,omitempty"`
34 URL *string `json:"url,omitempty"`
35 LatestCommentURL *string `json:"latest_comment_url,omitempty"`
36 Type *string `json:"type,omitempty"`
37 }
38
39
40
41 type NotificationListOptions struct {
42 All bool `url:"all,omitempty"`
43 Participating bool `url:"participating,omitempty"`
44 Since time.Time `url:"since,omitempty"`
45 Before time.Time `url:"before,omitempty"`
46
47 ListOptions
48 }
49
50
51
52
53 func (s *ActivityService) ListNotifications(ctx context.Context, opts *NotificationListOptions) ([]*Notification, *Response, error) {
54 u := fmt.Sprintf("notifications")
55 u, err := addOptions(u, opts)
56 if err != nil {
57 return nil, nil, err
58 }
59
60 req, err := s.client.NewRequest("GET", u, nil)
61 if err != nil {
62 return nil, nil, err
63 }
64
65 var notifications []*Notification
66 resp, err := s.client.Do(ctx, req, ¬ifications)
67 if err != nil {
68 return nil, resp, err
69 }
70
71 return notifications, resp, nil
72 }
73
74
75
76
77
78 func (s *ActivityService) ListRepositoryNotifications(ctx context.Context, owner, repo string, opts *NotificationListOptions) ([]*Notification, *Response, error) {
79 u := fmt.Sprintf("repos/%v/%v/notifications", owner, repo)
80 u, err := addOptions(u, opts)
81 if err != nil {
82 return nil, nil, err
83 }
84
85 req, err := s.client.NewRequest("GET", u, nil)
86 if err != nil {
87 return nil, nil, err
88 }
89
90 var notifications []*Notification
91 resp, err := s.client.Do(ctx, req, ¬ifications)
92 if err != nil {
93 return nil, resp, err
94 }
95
96 return notifications, resp, nil
97 }
98
99 type markReadOptions struct {
100 LastReadAt time.Time `json:"last_read_at,omitempty"`
101 }
102
103
104
105
106 func (s *ActivityService) MarkNotificationsRead(ctx context.Context, lastRead time.Time) (*Response, error) {
107 opts := &markReadOptions{
108 LastReadAt: lastRead,
109 }
110 req, err := s.client.NewRequest("PUT", "notifications", opts)
111 if err != nil {
112 return nil, err
113 }
114
115 return s.client.Do(ctx, req, nil)
116 }
117
118
119
120
121
122 func (s *ActivityService) MarkRepositoryNotificationsRead(ctx context.Context, owner, repo string, lastRead time.Time) (*Response, error) {
123 opts := &markReadOptions{
124 LastReadAt: lastRead,
125 }
126 u := fmt.Sprintf("repos/%v/%v/notifications", owner, repo)
127 req, err := s.client.NewRequest("PUT", u, opts)
128 if err != nil {
129 return nil, err
130 }
131
132 return s.client.Do(ctx, req, nil)
133 }
134
135
136
137
138 func (s *ActivityService) GetThread(ctx context.Context, id string) (*Notification, *Response, error) {
139 u := fmt.Sprintf("notifications/threads/%v", id)
140
141 req, err := s.client.NewRequest("GET", u, nil)
142 if err != nil {
143 return nil, nil, err
144 }
145
146 notification := new(Notification)
147 resp, err := s.client.Do(ctx, req, notification)
148 if err != nil {
149 return nil, resp, err
150 }
151
152 return notification, resp, nil
153 }
154
155
156
157
158 func (s *ActivityService) MarkThreadRead(ctx context.Context, id string) (*Response, error) {
159 u := fmt.Sprintf("notifications/threads/%v", id)
160
161 req, err := s.client.NewRequest("PATCH", u, nil)
162 if err != nil {
163 return nil, err
164 }
165
166 return s.client.Do(ctx, req, nil)
167 }
168
169
170
171
172
173 func (s *ActivityService) GetThreadSubscription(ctx context.Context, id string) (*Subscription, *Response, error) {
174 u := fmt.Sprintf("notifications/threads/%v/subscription", id)
175
176 req, err := s.client.NewRequest("GET", u, nil)
177 if err != nil {
178 return nil, nil, err
179 }
180
181 sub := new(Subscription)
182 resp, err := s.client.Do(ctx, req, sub)
183 if err != nil {
184 return nil, resp, err
185 }
186
187 return sub, resp, nil
188 }
189
190
191
192
193
194 func (s *ActivityService) SetThreadSubscription(ctx context.Context, id string, subscription *Subscription) (*Subscription, *Response, error) {
195 u := fmt.Sprintf("notifications/threads/%v/subscription", id)
196
197 req, err := s.client.NewRequest("PUT", u, subscription)
198 if err != nil {
199 return nil, nil, err
200 }
201
202 sub := new(Subscription)
203 resp, err := s.client.Do(ctx, req, sub)
204 if err != nil {
205 return nil, resp, err
206 }
207
208 return sub, resp, nil
209 }
210
211
212
213
214
215 func (s *ActivityService) DeleteThreadSubscription(ctx context.Context, id string) (*Response, error) {
216 u := fmt.Sprintf("notifications/threads/%v/subscription", id)
217 req, err := s.client.NewRequest("DELETE", u, nil)
218 if err != nil {
219 return nil, err
220 }
221
222 return s.client.Do(ctx, req, nil)
223 }
224
View as plain text