github.com/google/go-github/v64@v64.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, &notifications)
    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, &notifications)
    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  // GetThreadSubscription checks to see if the authenticated user is subscribed
   182  // to a thread.
   183  //
   184  // GitHub API docs: https://docs.github.com/rest/activity/notifications#get-a-thread-subscription-for-the-authenticated-user
   185  //
   186  //meta:operation GET /notifications/threads/{thread_id}/subscription
   187  func (s *ActivityService) GetThreadSubscription(ctx context.Context, id string) (*Subscription, *Response, error) {
   188  	u := fmt.Sprintf("notifications/threads/%v/subscription", id)
   189  
   190  	req, err := s.client.NewRequest("GET", u, nil)
   191  	if err != nil {
   192  		return nil, nil, err
   193  	}
   194  
   195  	sub := new(Subscription)
   196  	resp, err := s.client.Do(ctx, req, sub)
   197  	if err != nil {
   198  		return nil, resp, err
   199  	}
   200  
   201  	return sub, resp, nil
   202  }
   203  
   204  // SetThreadSubscription sets the subscription for the specified thread for the
   205  // authenticated user.
   206  //
   207  // GitHub API docs: https://docs.github.com/rest/activity/notifications#set-a-thread-subscription
   208  //
   209  //meta:operation PUT /notifications/threads/{thread_id}/subscription
   210  func (s *ActivityService) SetThreadSubscription(ctx context.Context, id string, subscription *Subscription) (*Subscription, *Response, error) {
   211  	u := fmt.Sprintf("notifications/threads/%v/subscription", id)
   212  
   213  	req, err := s.client.NewRequest("PUT", u, subscription)
   214  	if err != nil {
   215  		return nil, nil, err
   216  	}
   217  
   218  	sub := new(Subscription)
   219  	resp, err := s.client.Do(ctx, req, sub)
   220  	if err != nil {
   221  		return nil, resp, err
   222  	}
   223  
   224  	return sub, resp, nil
   225  }
   226  
   227  // DeleteThreadSubscription deletes the subscription for the specified thread
   228  // for the authenticated user.
   229  //
   230  // GitHub API docs: https://docs.github.com/rest/activity/notifications#delete-a-thread-subscription
   231  //
   232  //meta:operation DELETE /notifications/threads/{thread_id}/subscription
   233  func (s *ActivityService) DeleteThreadSubscription(ctx context.Context, id string) (*Response, error) {
   234  	u := fmt.Sprintf("notifications/threads/%v/subscription", id)
   235  	req, err := s.client.NewRequest("DELETE", u, nil)
   236  	if err != nil {
   237  		return nil, err
   238  	}
   239  
   240  	return s.client.Do(ctx, req, nil)
   241  }