2020-02-04 08:03:15 +00:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package gitea
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NotificationThread expose Notification on API
|
|
|
|
type NotificationThread struct {
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
Repository *Repository `json:"repository"`
|
|
|
|
Subject *NotificationSubject `json:"subject"`
|
|
|
|
Unread bool `json:"unread"`
|
|
|
|
Pinned bool `json:"pinned"`
|
|
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotificationSubject contains the notification subject (Issue/Pull/Commit)
|
|
|
|
type NotificationSubject struct {
|
2021-10-14 09:24:30 +01:00
|
|
|
Title string `json:"title"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
HTMLURL string `json:"html_url"`
|
|
|
|
LatestCommentURL string `json:"latest_comment_url"`
|
|
|
|
LatestCommentHTMLURL string `json:"latest_comment_html_url"`
|
|
|
|
Type NotifySubjectType `json:"type"`
|
|
|
|
State NotifySubjectState `json:"state"`
|
2020-02-04 08:03:15 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 13:28:16 +01:00
|
|
|
// NotifyStatus notification status type
|
|
|
|
type NotifyStatus string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// NotifyStatusUnread was not read
|
|
|
|
NotifyStatusUnread NotifyStatus = "unread"
|
|
|
|
// NotifyStatusRead was already read by user
|
|
|
|
NotifyStatusRead NotifyStatus = "read"
|
|
|
|
// NotifyStatusPinned notification is pinned by user
|
|
|
|
NotifyStatusPinned NotifyStatus = "pinned"
|
|
|
|
)
|
|
|
|
|
2021-07-01 12:39:50 +01:00
|
|
|
// NotifySubjectType represent type of notification subject
|
|
|
|
type NotifySubjectType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// NotifySubjectIssue an issue is subject of an notification
|
|
|
|
NotifySubjectIssue NotifySubjectType = "Issue"
|
|
|
|
// NotifySubjectPull an pull is subject of an notification
|
|
|
|
NotifySubjectPull NotifySubjectType = "Pull"
|
|
|
|
// NotifySubjectCommit an commit is subject of an notification
|
|
|
|
NotifySubjectCommit NotifySubjectType = "Commit"
|
|
|
|
// NotifySubjectRepository an repository is subject of an notification
|
|
|
|
NotifySubjectRepository NotifySubjectType = "Repository"
|
|
|
|
)
|
|
|
|
|
2021-06-30 22:06:50 +01:00
|
|
|
// NotifySubjectState reflect state of notification subject
|
|
|
|
type NotifySubjectState string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// NotifySubjectOpen if subject is a pull/issue and is open at the moment
|
|
|
|
NotifySubjectOpen NotifySubjectState = "open"
|
|
|
|
// NotifySubjectClosed if subject is a pull/issue and is closed at the moment
|
|
|
|
NotifySubjectClosed NotifySubjectState = "closed"
|
|
|
|
// NotifySubjectMerged if subject is a pull and got merged
|
|
|
|
NotifySubjectMerged NotifySubjectState = "merged"
|
|
|
|
)
|
|
|
|
|
2020-02-04 08:03:15 +00:00
|
|
|
// ListNotificationOptions represents the filter options
|
|
|
|
type ListNotificationOptions struct {
|
2020-05-20 09:26:47 +01:00
|
|
|
ListOptions
|
2021-07-01 12:39:50 +01:00
|
|
|
Since time.Time
|
|
|
|
Before time.Time
|
|
|
|
Status []NotifyStatus
|
|
|
|
SubjectTypes []NotifySubjectType
|
2020-02-04 08:03:15 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 13:28:16 +01:00
|
|
|
// MarkNotificationOptions represents the filter & modify options
|
2020-02-04 08:03:15 +00:00
|
|
|
type MarkNotificationOptions struct {
|
|
|
|
LastReadAt time.Time
|
2020-07-21 13:28:16 +01:00
|
|
|
Status []NotifyStatus
|
|
|
|
ToStatus NotifyStatus
|
2020-02-04 08:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// QueryEncode encode options to url query
|
|
|
|
func (opt *ListNotificationOptions) QueryEncode() string {
|
2020-05-20 09:26:47 +01:00
|
|
|
query := opt.getURLQuery()
|
2020-02-04 08:03:15 +00:00
|
|
|
if !opt.Since.IsZero() {
|
|
|
|
query.Add("since", opt.Since.Format(time.RFC3339))
|
|
|
|
}
|
|
|
|
if !opt.Before.IsZero() {
|
|
|
|
query.Add("before", opt.Before.Format(time.RFC3339))
|
|
|
|
}
|
2020-07-21 13:28:16 +01:00
|
|
|
for _, s := range opt.Status {
|
|
|
|
query.Add("status-types", string(s))
|
|
|
|
}
|
2021-07-01 12:39:50 +01:00
|
|
|
for _, s := range opt.SubjectTypes {
|
|
|
|
query.Add("subject-type", string(s))
|
|
|
|
}
|
2020-02-04 08:03:15 +00:00
|
|
|
return query.Encode()
|
|
|
|
}
|
|
|
|
|
2020-07-21 13:28:16 +01:00
|
|
|
// Validate the CreateUserOption struct
|
|
|
|
func (opt ListNotificationOptions) Validate(c *Client) error {
|
|
|
|
if len(opt.Status) != 0 {
|
2020-11-12 20:59:04 +00:00
|
|
|
return c.checkServerVersionGreaterThanOrEqual(version1_12_3)
|
2020-07-21 13:28:16 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-04 08:03:15 +00:00
|
|
|
// QueryEncode encode options to url query
|
|
|
|
func (opt *MarkNotificationOptions) QueryEncode() string {
|
|
|
|
query := make(url.Values)
|
|
|
|
if !opt.LastReadAt.IsZero() {
|
|
|
|
query.Add("last_read_at", opt.LastReadAt.Format(time.RFC3339))
|
|
|
|
}
|
2020-07-21 13:28:16 +01:00
|
|
|
for _, s := range opt.Status {
|
|
|
|
query.Add("status-types", string(s))
|
|
|
|
}
|
|
|
|
if len(opt.ToStatus) != 0 {
|
|
|
|
query.Add("to-status", string(opt.ToStatus))
|
|
|
|
}
|
2020-02-04 08:03:15 +00:00
|
|
|
return query.Encode()
|
|
|
|
}
|
|
|
|
|
2020-07-21 13:28:16 +01:00
|
|
|
// Validate the CreateUserOption struct
|
|
|
|
func (opt MarkNotificationOptions) Validate(c *Client) error {
|
|
|
|
if len(opt.Status) != 0 || len(opt.ToStatus) != 0 {
|
2020-11-12 20:59:04 +00:00
|
|
|
return c.checkServerVersionGreaterThanOrEqual(version1_12_3)
|
2020-07-21 13:28:16 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-04 08:03:15 +00:00
|
|
|
// CheckNotifications list users's notification threads
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) CheckNotifications() (int64, *Response, error) {
|
2020-11-12 20:59:04 +00:00
|
|
|
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
return 0, nil, err
|
2020-02-04 08:03:15 +00:00
|
|
|
}
|
|
|
|
new := struct {
|
|
|
|
New int64 `json:"new"`
|
|
|
|
}{}
|
|
|
|
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("GET", "/notifications/new", jsonHeader, nil, &new)
|
|
|
|
return new.New, resp, err
|
2020-02-04 08:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetNotification get notification thread by ID
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) GetNotification(id int64) (*NotificationThread, *Response, error) {
|
2020-11-12 20:59:04 +00:00
|
|
|
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
return nil, nil, err
|
2020-02-04 08:03:15 +00:00
|
|
|
}
|
2020-05-20 09:26:47 +01:00
|
|
|
thread := new(NotificationThread)
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/notifications/threads/%d", id), nil, nil, thread)
|
|
|
|
return thread, resp, err
|
2020-02-04 08:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadNotification mark notification thread as read by ID
|
2020-07-21 13:28:16 +01:00
|
|
|
// It optionally takes a second argument if status has to be set other than 'read'
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) ReadNotification(id int64, status ...NotifyStatus) (*Response, error) {
|
2020-11-12 20:59:04 +00:00
|
|
|
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
return nil, err
|
2020-02-04 08:03:15 +00:00
|
|
|
}
|
2020-07-21 13:28:16 +01:00
|
|
|
link := fmt.Sprintf("/notifications/threads/%d", id)
|
|
|
|
if len(status) != 0 {
|
|
|
|
link += fmt.Sprintf("?to-status=%s", status[0])
|
|
|
|
}
|
2020-09-14 03:37:09 +01:00
|
|
|
_, resp, err := c.getResponse("PATCH", link, nil, nil)
|
|
|
|
return resp, err
|
2020-02-04 08:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListNotifications list users's notification threads
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) ListNotifications(opt ListNotificationOptions) ([]*NotificationThread, *Response, error) {
|
2020-11-12 20:59:04 +00:00
|
|
|
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if err := opt.Validate(c); err != nil {
|
|
|
|
return nil, nil, err
|
2020-02-04 08:03:15 +00:00
|
|
|
}
|
|
|
|
link, _ := url.Parse("/notifications")
|
|
|
|
link.RawQuery = opt.QueryEncode()
|
|
|
|
threads := make([]*NotificationThread, 0, 10)
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("GET", link.String(), nil, nil, &threads)
|
|
|
|
return threads, resp, err
|
2020-02-04 08:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadNotifications mark notification threads as read
|
2020-09-14 03:37:09 +01:00
|
|
|
func (c *Client) ReadNotifications(opt MarkNotificationOptions) (*Response, error) {
|
2020-11-12 20:59:04 +00:00
|
|
|
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := opt.Validate(c); err != nil {
|
|
|
|
return nil, err
|
2020-02-04 08:03:15 +00:00
|
|
|
}
|
|
|
|
link, _ := url.Parse("/notifications")
|
|
|
|
link.RawQuery = opt.QueryEncode()
|
2020-09-14 03:37:09 +01:00
|
|
|
_, resp, err := c.getResponse("PUT", link.String(), nil, nil)
|
|
|
|
return resp, err
|
2020-02-04 08:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListRepoNotifications list users's notification threads on a specific repo
|
2021-03-21 20:20:32 +00:00
|
|
|
func (c *Client) ListRepoNotifications(owner, repo string, opt ListNotificationOptions) ([]*NotificationThread, *Response, error) {
|
|
|
|
if err := escapeValidatePathSegments(&owner, &repo); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2020-11-12 20:59:04 +00:00
|
|
|
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if err := opt.Validate(c); err != nil {
|
|
|
|
return nil, nil, err
|
2020-02-04 08:03:15 +00:00
|
|
|
}
|
2021-03-21 20:20:32 +00:00
|
|
|
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/notifications", owner, repo))
|
2020-02-04 08:03:15 +00:00
|
|
|
link.RawQuery = opt.QueryEncode()
|
|
|
|
threads := make([]*NotificationThread, 0, 10)
|
2020-09-14 03:37:09 +01:00
|
|
|
resp, err := c.getParsedResponse("GET", link.String(), nil, nil, &threads)
|
|
|
|
return threads, resp, err
|
2020-02-04 08:03:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ReadRepoNotifications mark notification threads as read on a specific repo
|
2021-03-21 20:20:32 +00:00
|
|
|
func (c *Client) ReadRepoNotifications(owner, repo string, opt MarkNotificationOptions) (*Response, error) {
|
|
|
|
if err := escapeValidatePathSegments(&owner, &repo); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-12 20:59:04 +00:00
|
|
|
if err := c.checkServerVersionGreaterThanOrEqual(version1_12_0); err != nil {
|
2020-09-14 03:37:09 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := opt.Validate(c); err != nil {
|
|
|
|
return nil, err
|
2020-02-04 08:03:15 +00:00
|
|
|
}
|
2021-03-21 20:20:32 +00:00
|
|
|
link, _ := url.Parse(fmt.Sprintf("/repos/%s/%s/notifications", owner, repo))
|
2020-02-04 08:03:15 +00:00
|
|
|
link.RawQuery = opt.QueryEncode()
|
2020-09-14 03:37:09 +01:00
|
|
|
_, resp, err := c.getResponse("PUT", link.String(), nil, nil)
|
|
|
|
return resp, err
|
2020-02-04 08:03:15 +00:00
|
|
|
}
|