...

Source file src/github.com/google/go-github/v45/github/activity_notifications.go

Documentation: github.com/google/go-github/v45/github

     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/en/rest/activity#notification-reasons
    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  // 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/en/rest/activity/notifications#list-notifications-for-the-authenticated-user
    53  func (s *ActivityService) ListNotifications(ctx context.Context, opts *NotificationListOptions) ([]*Notification, *Response, error) {
    54  	u := "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, &notifications)
    67  	if err != nil {
    68  		return nil, resp, err
    69  	}
    70  
    71  	return notifications, resp, nil
    72  }
    73  
    74  // ListRepositoryNotifications lists all notifications in a given repository
    75  // for the authenticated user.
    76  //
    77  // GitHub API docs: https://docs.github.com/en/rest/activity/notifications#list-repository-notifications-for-the-authenticated-user
    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, &notifications)
    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  // MarkNotificationsRead marks all notifications up to lastRead as read.
   104  //
   105  // GitHub API docs: https://docs.github.com/en/rest/activity#mark-as-read
   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  // MarkRepositoryNotificationsRead marks all notifications up to lastRead in
   119  // the specified repository as read.
   120  //
   121  // GitHub API docs: https://docs.github.com/en/rest/activity/notifications#mark-repository-notifications-as-read
   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  // GetThread gets the specified notification thread.
   136  //
   137  // GitHub API docs: https://docs.github.com/en/rest/activity/notifications#get-a-thread
   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  // MarkThreadRead marks the specified thread as read.
   156  //
   157  // GitHub API docs: https://docs.github.com/en/rest/activity/notifications#mark-a-thread-as-read
   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  // GetThreadSubscription checks to see if the authenticated user is subscribed
   170  // to a thread.
   171  //
   172  // GitHub API docs: https://docs.github.com/en/rest/activity/notifications#get-a-thread-subscription-for-the-authenticated-user
   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  // SetThreadSubscription sets the subscription for the specified thread for the
   191  // authenticated user.
   192  //
   193  // GitHub API docs: https://docs.github.com/en/rest/activity/notifications#set-a-thread-subscription
   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  // DeleteThreadSubscription deletes the subscription for the specified thread
   212  // for the authenticated user.
   213  //
   214  // GitHub API docs: https://docs.github.com/en/rest/activity/notifications#delete-a-thread-subscription
   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