github.com/google/go-github/v70@v70.0.0/github/activity_notifications.go (about) 1 // Copyright 2014 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "context" 10 "fmt" 11 "time" 12 ) 13 14 // Notification identifies a GitHub notification for a user. 15 type Notification struct { 16 ID *string `json:"id,omitempty"` 17 Repository *Repository `json:"repository,omitempty"` 18 Subject *NotificationSubject `json:"subject,omitempty"` 19 20 // Reason identifies the event that triggered the notification. 21 // 22 // GitHub API docs: https://docs.github.com/rest/activity#notification-reasons 23 Reason *string `json:"reason,omitempty"` 24 25 Unread *bool `json:"unread,omitempty"` 26 UpdatedAt *Timestamp `json:"updated_at,omitempty"` 27 LastReadAt *Timestamp `json:"last_read_at,omitempty"` 28 URL *string `json:"url,omitempty"` 29 } 30 31 // NotificationSubject identifies the subject of a notification. 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 // NotificationListOptions specifies the optional parameters to the 40 // ActivityService.ListNotifications method. 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 // ListNotifications lists all notifications for the authenticated user. 51 // 52 // GitHub API docs: https://docs.github.com/rest/activity/notifications#list-notifications-for-the-authenticated-user 53 // 54 //meta:operation GET /notifications 55 func (s *ActivityService) ListNotifications(ctx context.Context, opts *NotificationListOptions) ([]*Notification, *Response, error) { 56 u := "notifications" 57 u, err := addOptions(u, opts) 58 if err != nil { 59 return nil, nil, err 60 } 61 62 req, err := s.client.NewRequest("GET", u, nil) 63 if err != nil { 64 return nil, nil, err 65 } 66 67 var notifications []*Notification 68 resp, err := s.client.Do(ctx, req, ¬ifications) 69 if err != nil { 70 return nil, resp, err 71 } 72 73 return notifications, resp, nil 74 } 75 76 // ListRepositoryNotifications lists all notifications in a given repository 77 // for the authenticated user. 78 // 79 // GitHub API docs: https://docs.github.com/rest/activity/notifications#list-repository-notifications-for-the-authenticated-user 80 // 81 //meta:operation GET /repos/{owner}/{repo}/notifications 82 func (s *ActivityService) ListRepositoryNotifications(ctx context.Context, owner, repo string, opts *NotificationListOptions) ([]*Notification, *Response, error) { 83 u := fmt.Sprintf("repos/%v/%v/notifications", owner, repo) 84 u, err := addOptions(u, opts) 85 if err != nil { 86 return nil, nil, err 87 } 88 89 req, err := s.client.NewRequest("GET", u, nil) 90 if err != nil { 91 return nil, nil, err 92 } 93 94 var notifications []*Notification 95 resp, err := s.client.Do(ctx, req, ¬ifications) 96 if err != nil { 97 return nil, resp, err 98 } 99 100 return notifications, resp, nil 101 } 102 103 type markReadOptions struct { 104 LastReadAt Timestamp `json:"last_read_at,omitempty"` 105 } 106 107 // MarkNotificationsRead marks all notifications up to lastRead as read. 108 // 109 // GitHub API docs: https://docs.github.com/rest/activity/notifications#mark-notifications-as-read 110 // 111 //meta:operation PUT /notifications 112 func (s *ActivityService) MarkNotificationsRead(ctx context.Context, lastRead Timestamp) (*Response, error) { 113 opts := &markReadOptions{ 114 LastReadAt: lastRead, 115 } 116 req, err := s.client.NewRequest("PUT", "notifications", opts) 117 if err != nil { 118 return nil, err 119 } 120 121 return s.client.Do(ctx, req, nil) 122 } 123 124 // MarkRepositoryNotificationsRead marks all notifications up to lastRead in 125 // the specified repository as read. 126 // 127 // GitHub API docs: https://docs.github.com/rest/activity/notifications#mark-repository-notifications-as-read 128 // 129 //meta:operation PUT /repos/{owner}/{repo}/notifications 130 func (s *ActivityService) MarkRepositoryNotificationsRead(ctx context.Context, owner, repo string, lastRead Timestamp) (*Response, error) { 131 opts := &markReadOptions{ 132 LastReadAt: lastRead, 133 } 134 u := fmt.Sprintf("repos/%v/%v/notifications", owner, repo) 135 req, err := s.client.NewRequest("PUT", u, opts) 136 if err != nil { 137 return nil, err 138 } 139 140 return s.client.Do(ctx, req, nil) 141 } 142 143 // GetThread gets the specified notification thread. 144 // 145 // GitHub API docs: https://docs.github.com/rest/activity/notifications#get-a-thread 146 // 147 //meta:operation GET /notifications/threads/{thread_id} 148 func (s *ActivityService) GetThread(ctx context.Context, id string) (*Notification, *Response, error) { 149 u := fmt.Sprintf("notifications/threads/%v", id) 150 151 req, err := s.client.NewRequest("GET", u, nil) 152 if err != nil { 153 return nil, nil, err 154 } 155 156 notification := new(Notification) 157 resp, err := s.client.Do(ctx, req, notification) 158 if err != nil { 159 return nil, resp, err 160 } 161 162 return notification, resp, nil 163 } 164 165 // MarkThreadRead marks the specified thread as read. 166 // 167 // GitHub API docs: https://docs.github.com/rest/activity/notifications#mark-a-thread-as-read 168 // 169 //meta:operation PATCH /notifications/threads/{thread_id} 170 func (s *ActivityService) MarkThreadRead(ctx context.Context, id string) (*Response, error) { 171 u := fmt.Sprintf("notifications/threads/%v", id) 172 173 req, err := s.client.NewRequest("PATCH", u, nil) 174 if err != nil { 175 return nil, err 176 } 177 178 return s.client.Do(ctx, req, nil) 179 } 180 181 // MarkThreadDone marks the specified thread as done. 182 // Marking a thread as "done" is equivalent to marking a notification in your notification inbox on GitHub as done. 183 // 184 // GitHub API docs: https://docs.github.com/rest/activity/notifications#mark-a-thread-as-done 185 // 186 //meta:operation DELETE /notifications/threads/{thread_id} 187 func (s *ActivityService) MarkThreadDone(ctx context.Context, id int64) (*Response, error) { 188 u := fmt.Sprintf("notifications/threads/%v", id) 189 190 req, err := s.client.NewRequest("DELETE", u, nil) 191 if err != nil { 192 return nil, err 193 } 194 195 return s.client.Do(ctx, req, nil) 196 } 197 198 // GetThreadSubscription checks to see if the authenticated user is subscribed 199 // to a thread. 200 // 201 // GitHub API docs: https://docs.github.com/rest/activity/notifications#get-a-thread-subscription-for-the-authenticated-user 202 // 203 //meta:operation GET /notifications/threads/{thread_id}/subscription 204 func (s *ActivityService) GetThreadSubscription(ctx context.Context, id string) (*Subscription, *Response, error) { 205 u := fmt.Sprintf("notifications/threads/%v/subscription", id) 206 207 req, err := s.client.NewRequest("GET", u, nil) 208 if err != nil { 209 return nil, nil, err 210 } 211 212 sub := new(Subscription) 213 resp, err := s.client.Do(ctx, req, sub) 214 if err != nil { 215 return nil, resp, err 216 } 217 218 return sub, resp, nil 219 } 220 221 // SetThreadSubscription sets the subscription for the specified thread for the 222 // authenticated user. 223 // 224 // GitHub API docs: https://docs.github.com/rest/activity/notifications#set-a-thread-subscription 225 // 226 //meta:operation PUT /notifications/threads/{thread_id}/subscription 227 func (s *ActivityService) SetThreadSubscription(ctx context.Context, id string, subscription *Subscription) (*Subscription, *Response, error) { 228 u := fmt.Sprintf("notifications/threads/%v/subscription", id) 229 230 req, err := s.client.NewRequest("PUT", u, subscription) 231 if err != nil { 232 return nil, nil, err 233 } 234 235 sub := new(Subscription) 236 resp, err := s.client.Do(ctx, req, sub) 237 if err != nil { 238 return nil, resp, err 239 } 240 241 return sub, resp, nil 242 } 243 244 // DeleteThreadSubscription deletes the subscription for the specified thread 245 // for the authenticated user. 246 // 247 // GitHub API docs: https://docs.github.com/rest/activity/notifications#delete-a-thread-subscription 248 // 249 //meta:operation DELETE /notifications/threads/{thread_id}/subscription 250 func (s *ActivityService) DeleteThreadSubscription(ctx context.Context, id string) (*Response, error) { 251 u := fmt.Sprintf("notifications/threads/%v/subscription", id) 252 req, err := s.client.NewRequest("DELETE", u, nil) 253 if err != nil { 254 return nil, err 255 } 256 257 return s.client.Do(ctx, req, nil) 258 }