activity_notifications.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. package github
  6. import (
  7. "context"
  8. "fmt"
  9. "time"
  10. )
  11. // Notification identifies a GitHub notification for a user.
  12. type Notification struct {
  13. ID *string `json:"id,omitempty"`
  14. Repository *Repository `json:"repository,omitempty"`
  15. Subject *NotificationSubject `json:"subject,omitempty"`
  16. // Reason identifies the event that triggered the notification.
  17. //
  18. // GitHub API docs: https://developer.github.com/v3/activity/notifications/#notification-reasons
  19. Reason *string `json:"reason,omitempty"`
  20. Unread *bool `json:"unread,omitempty"`
  21. UpdatedAt *time.Time `json:"updated_at,omitempty"`
  22. LastReadAt *time.Time `json:"last_read_at,omitempty"`
  23. URL *string `json:"url,omitempty"`
  24. }
  25. // NotificationSubject identifies the subject of a notification.
  26. type NotificationSubject struct {
  27. Title *string `json:"title,omitempty"`
  28. URL *string `json:"url,omitempty"`
  29. LatestCommentURL *string `json:"latest_comment_url,omitempty"`
  30. Type *string `json:"type,omitempty"`
  31. }
  32. // NotificationListOptions specifies the optional parameters to the
  33. // ActivityService.ListNotifications method.
  34. type NotificationListOptions struct {
  35. All bool `url:"all,omitempty"`
  36. Participating bool `url:"participating,omitempty"`
  37. Since time.Time `url:"since,omitempty"`
  38. Before time.Time `url:"before,omitempty"`
  39. ListOptions
  40. }
  41. // ListNotifications lists all notifications for the authenticated user.
  42. //
  43. // GitHub API docs: https://developer.github.com/v3/activity/notifications/#list-your-notifications
  44. func (s *ActivityService) ListNotifications(ctx context.Context, opt *NotificationListOptions) ([]*Notification, *Response, error) {
  45. u := fmt.Sprintf("notifications")
  46. u, err := addOptions(u, opt)
  47. if err != nil {
  48. return nil, nil, err
  49. }
  50. req, err := s.client.NewRequest("GET", u, nil)
  51. if err != nil {
  52. return nil, nil, err
  53. }
  54. var notifications []*Notification
  55. resp, err := s.client.Do(ctx, req, &notifications)
  56. if err != nil {
  57. return nil, resp, err
  58. }
  59. return notifications, resp, nil
  60. }
  61. // ListRepositoryNotifications lists all notifications in a given repository
  62. // for the authenticated user.
  63. //
  64. // GitHub API docs: https://developer.github.com/v3/activity/notifications/#list-your-notifications-in-a-repository
  65. func (s *ActivityService) ListRepositoryNotifications(ctx context.Context, owner, repo string, opt *NotificationListOptions) ([]*Notification, *Response, error) {
  66. u := fmt.Sprintf("repos/%v/%v/notifications", owner, repo)
  67. u, err := addOptions(u, opt)
  68. if err != nil {
  69. return nil, nil, err
  70. }
  71. req, err := s.client.NewRequest("GET", u, nil)
  72. if err != nil {
  73. return nil, nil, err
  74. }
  75. var notifications []*Notification
  76. resp, err := s.client.Do(ctx, req, &notifications)
  77. if err != nil {
  78. return nil, resp, err
  79. }
  80. return notifications, resp, nil
  81. }
  82. type markReadOptions struct {
  83. LastReadAt time.Time `json:"last_read_at,omitempty"`
  84. }
  85. // MarkNotificationsRead marks all notifications up to lastRead as read.
  86. //
  87. // GitHub API docs: https://developer.github.com/v3/activity/notifications/#mark-as-read
  88. func (s *ActivityService) MarkNotificationsRead(ctx context.Context, lastRead time.Time) (*Response, error) {
  89. opts := &markReadOptions{
  90. LastReadAt: lastRead,
  91. }
  92. req, err := s.client.NewRequest("PUT", "notifications", opts)
  93. if err != nil {
  94. return nil, err
  95. }
  96. return s.client.Do(ctx, req, nil)
  97. }
  98. // MarkRepositoryNotificationsRead marks all notifications up to lastRead in
  99. // the specified repository as read.
  100. //
  101. // GitHub API docs: https://developer.github.com/v3/activity/notifications/#mark-notifications-as-read-in-a-repository
  102. func (s *ActivityService) MarkRepositoryNotificationsRead(ctx context.Context, owner, repo string, lastRead time.Time) (*Response, error) {
  103. opts := &markReadOptions{
  104. LastReadAt: lastRead,
  105. }
  106. u := fmt.Sprintf("repos/%v/%v/notifications", owner, repo)
  107. req, err := s.client.NewRequest("PUT", u, opts)
  108. if err != nil {
  109. return nil, err
  110. }
  111. return s.client.Do(ctx, req, nil)
  112. }
  113. // GetThread gets the specified notification thread.
  114. //
  115. // GitHub API docs: https://developer.github.com/v3/activity/notifications/#view-a-single-thread
  116. func (s *ActivityService) GetThread(ctx context.Context, id string) (*Notification, *Response, error) {
  117. u := fmt.Sprintf("notifications/threads/%v", id)
  118. req, err := s.client.NewRequest("GET", u, nil)
  119. if err != nil {
  120. return nil, nil, err
  121. }
  122. notification := new(Notification)
  123. resp, err := s.client.Do(ctx, req, notification)
  124. if err != nil {
  125. return nil, resp, err
  126. }
  127. return notification, resp, nil
  128. }
  129. // MarkThreadRead marks the specified thread as read.
  130. //
  131. // GitHub API docs: https://developer.github.com/v3/activity/notifications/#mark-a-thread-as-read
  132. func (s *ActivityService) MarkThreadRead(ctx context.Context, id string) (*Response, error) {
  133. u := fmt.Sprintf("notifications/threads/%v", id)
  134. req, err := s.client.NewRequest("PATCH", u, nil)
  135. if err != nil {
  136. return nil, err
  137. }
  138. return s.client.Do(ctx, req, nil)
  139. }
  140. // GetThreadSubscription checks to see if the authenticated user is subscribed
  141. // to a thread.
  142. //
  143. // GitHub API docs: https://developer.github.com/v3/activity/notifications/#get-a-thread-subscription
  144. func (s *ActivityService) GetThreadSubscription(ctx context.Context, id string) (*Subscription, *Response, error) {
  145. u := fmt.Sprintf("notifications/threads/%v/subscription", id)
  146. req, err := s.client.NewRequest("GET", u, nil)
  147. if err != nil {
  148. return nil, nil, err
  149. }
  150. sub := new(Subscription)
  151. resp, err := s.client.Do(ctx, req, sub)
  152. if err != nil {
  153. return nil, resp, err
  154. }
  155. return sub, resp, nil
  156. }
  157. // SetThreadSubscription sets the subscription for the specified thread for the
  158. // authenticated user.
  159. //
  160. // GitHub API docs: https://developer.github.com/v3/activity/notifications/#set-a-thread-subscription
  161. func (s *ActivityService) SetThreadSubscription(ctx context.Context, id string, subscription *Subscription) (*Subscription, *Response, error) {
  162. u := fmt.Sprintf("notifications/threads/%v/subscription", id)
  163. req, err := s.client.NewRequest("PUT", u, subscription)
  164. if err != nil {
  165. return nil, nil, err
  166. }
  167. sub := new(Subscription)
  168. resp, err := s.client.Do(ctx, req, sub)
  169. if err != nil {
  170. return nil, resp, err
  171. }
  172. return sub, resp, nil
  173. }
  174. // DeleteThreadSubscription deletes the subscription for the specified thread
  175. // for the authenticated user.
  176. //
  177. // GitHub API docs: https://developer.github.com/v3/activity/notifications/#delete-a-thread-subscription
  178. func (s *ActivityService) DeleteThreadSubscription(ctx context.Context, id string) (*Response, error) {
  179. u := fmt.Sprintf("notifications/threads/%v/subscription", id)
  180. req, err := s.client.NewRequest("DELETE", u, nil)
  181. if err != nil {
  182. return nil, err
  183. }
  184. return s.client.Do(ctx, req, nil)
  185. }