123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- // Copyright 2014 The go-github AUTHORS. All rights reserved.
- //
- // Use of this source code is governed by a BSD-style
- // license that can be found in the LICENSE file.
- package github
- import (
- "context"
- "fmt"
- "time"
- )
- // Notification identifies a GitHub notification for a user.
- type Notification struct {
- ID *string `json:"id,omitempty"`
- Repository *Repository `json:"repository,omitempty"`
- Subject *NotificationSubject `json:"subject,omitempty"`
- // Reason identifies the event that triggered the notification.
- //
- // GitHub API docs: https://developer.github.com/v3/activity/notifications/#notification-reasons
- Reason *string `json:"reason,omitempty"`
- Unread *bool `json:"unread,omitempty"`
- UpdatedAt *time.Time `json:"updated_at,omitempty"`
- LastReadAt *time.Time `json:"last_read_at,omitempty"`
- URL *string `json:"url,omitempty"`
- }
- // NotificationSubject identifies the subject of a notification.
- type NotificationSubject struct {
- Title *string `json:"title,omitempty"`
- URL *string `json:"url,omitempty"`
- LatestCommentURL *string `json:"latest_comment_url,omitempty"`
- Type *string `json:"type,omitempty"`
- }
- // NotificationListOptions specifies the optional parameters to the
- // ActivityService.ListNotifications method.
- type NotificationListOptions struct {
- All bool `url:"all,omitempty"`
- Participating bool `url:"participating,omitempty"`
- Since time.Time `url:"since,omitempty"`
- Before time.Time `url:"before,omitempty"`
- ListOptions
- }
- // ListNotifications lists all notifications for the authenticated user.
- //
- // GitHub API docs: https://developer.github.com/v3/activity/notifications/#list-your-notifications
- func (s *ActivityService) ListNotifications(ctx context.Context, opt *NotificationListOptions) ([]*Notification, *Response, error) {
- u := fmt.Sprintf("notifications")
- u, err := addOptions(u, opt)
- if err != nil {
- return nil, nil, err
- }
- req, err := s.client.NewRequest("GET", u, nil)
- if err != nil {
- return nil, nil, err
- }
- var notifications []*Notification
- resp, err := s.client.Do(ctx, req, ¬ifications)
- if err != nil {
- return nil, resp, err
- }
- return notifications, resp, nil
- }
- // ListRepositoryNotifications lists all notifications in a given repository
- // for the authenticated user.
- //
- // GitHub API docs: https://developer.github.com/v3/activity/notifications/#list-your-notifications-in-a-repository
- func (s *ActivityService) ListRepositoryNotifications(ctx context.Context, owner, repo string, opt *NotificationListOptions) ([]*Notification, *Response, error) {
- u := fmt.Sprintf("repos/%v/%v/notifications", owner, repo)
- u, err := addOptions(u, opt)
- if err != nil {
- return nil, nil, err
- }
- req, err := s.client.NewRequest("GET", u, nil)
- if err != nil {
- return nil, nil, err
- }
- var notifications []*Notification
- resp, err := s.client.Do(ctx, req, ¬ifications)
- if err != nil {
- return nil, resp, err
- }
- return notifications, resp, nil
- }
- type markReadOptions struct {
- LastReadAt time.Time `json:"last_read_at,omitempty"`
- }
- // MarkNotificationsRead marks all notifications up to lastRead as read.
- //
- // GitHub API docs: https://developer.github.com/v3/activity/notifications/#mark-as-read
- func (s *ActivityService) MarkNotificationsRead(ctx context.Context, lastRead time.Time) (*Response, error) {
- opts := &markReadOptions{
- LastReadAt: lastRead,
- }
- req, err := s.client.NewRequest("PUT", "notifications", opts)
- if err != nil {
- return nil, err
- }
- return s.client.Do(ctx, req, nil)
- }
- // MarkRepositoryNotificationsRead marks all notifications up to lastRead in
- // the specified repository as read.
- //
- // GitHub API docs: https://developer.github.com/v3/activity/notifications/#mark-notifications-as-read-in-a-repository
- func (s *ActivityService) MarkRepositoryNotificationsRead(ctx context.Context, owner, repo string, lastRead time.Time) (*Response, error) {
- opts := &markReadOptions{
- LastReadAt: lastRead,
- }
- u := fmt.Sprintf("repos/%v/%v/notifications", owner, repo)
- req, err := s.client.NewRequest("PUT", u, opts)
- if err != nil {
- return nil, err
- }
- return s.client.Do(ctx, req, nil)
- }
- // GetThread gets the specified notification thread.
- //
- // GitHub API docs: https://developer.github.com/v3/activity/notifications/#view-a-single-thread
- func (s *ActivityService) GetThread(ctx context.Context, id string) (*Notification, *Response, error) {
- u := fmt.Sprintf("notifications/threads/%v", id)
- req, err := s.client.NewRequest("GET", u, nil)
- if err != nil {
- return nil, nil, err
- }
- notification := new(Notification)
- resp, err := s.client.Do(ctx, req, notification)
- if err != nil {
- return nil, resp, err
- }
- return notification, resp, nil
- }
- // MarkThreadRead marks the specified thread as read.
- //
- // GitHub API docs: https://developer.github.com/v3/activity/notifications/#mark-a-thread-as-read
- func (s *ActivityService) MarkThreadRead(ctx context.Context, id string) (*Response, error) {
- u := fmt.Sprintf("notifications/threads/%v", id)
- req, err := s.client.NewRequest("PATCH", u, nil)
- if err != nil {
- return nil, err
- }
- return s.client.Do(ctx, req, nil)
- }
- // GetThreadSubscription checks to see if the authenticated user is subscribed
- // to a thread.
- //
- // GitHub API docs: https://developer.github.com/v3/activity/notifications/#get-a-thread-subscription
- func (s *ActivityService) GetThreadSubscription(ctx context.Context, id string) (*Subscription, *Response, error) {
- u := fmt.Sprintf("notifications/threads/%v/subscription", id)
- req, err := s.client.NewRequest("GET", u, nil)
- if err != nil {
- return nil, nil, err
- }
- sub := new(Subscription)
- resp, err := s.client.Do(ctx, req, sub)
- if err != nil {
- return nil, resp, err
- }
- return sub, resp, nil
- }
- // SetThreadSubscription sets the subscription for the specified thread for the
- // authenticated user.
- //
- // GitHub API docs: https://developer.github.com/v3/activity/notifications/#set-a-thread-subscription
- func (s *ActivityService) SetThreadSubscription(ctx context.Context, id string, subscription *Subscription) (*Subscription, *Response, error) {
- u := fmt.Sprintf("notifications/threads/%v/subscription", id)
- req, err := s.client.NewRequest("PUT", u, subscription)
- if err != nil {
- return nil, nil, err
- }
- sub := new(Subscription)
- resp, err := s.client.Do(ctx, req, sub)
- if err != nil {
- return nil, resp, err
- }
- return sub, resp, nil
- }
- // DeleteThreadSubscription deletes the subscription for the specified thread
- // for the authenticated user.
- //
- // GitHub API docs: https://developer.github.com/v3/activity/notifications/#delete-a-thread-subscription
- func (s *ActivityService) DeleteThreadSubscription(ctx context.Context, id string) (*Response, error) {
- u := fmt.Sprintf("notifications/threads/%v/subscription", id)
- req, err := s.client.NewRequest("DELETE", u, nil)
- if err != nil {
- return nil, err
- }
- return s.client.Do(ctx, req, nil)
- }
|