2020-02-07 08:53:15 -05: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 (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Reaction contain one reaction
|
|
|
|
type Reaction struct {
|
|
|
|
User *User `json:"user"`
|
|
|
|
Reaction string `json:"content"`
|
|
|
|
Created time.Time `json:"created_at"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetIssueReactions get a list reactions of an issue
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) GetIssueReactions(owner, repo string, index int64) ([]*Reaction, *Response, error) {
|
2020-11-14 05:31:01 -05:00
|
|
|
if err := c.checkServerVersionGreaterThanOrEqual(version1_11_0); err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, nil, err
|
2020-02-07 08:53:15 -05:00
|
|
|
}
|
|
|
|
reactions := make([]*Reaction, 0, 10)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/%d/reactions", owner, repo, index), nil, nil, &reactions)
|
|
|
|
return reactions, resp, err
|
2020-02-07 08:53:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetIssueCommentReactions get a list of reactions from a comment of an issue
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) GetIssueCommentReactions(owner, repo string, commentID int64) ([]*Reaction, *Response, error) {
|
2020-11-14 05:31:01 -05:00
|
|
|
if err := c.checkServerVersionGreaterThanOrEqual(version1_11_0); err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, nil, err
|
2020-02-07 08:53:15 -05:00
|
|
|
}
|
|
|
|
reactions := make([]*Reaction, 0, 10)
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/issues/comments/%d/reactions", owner, repo, commentID), nil, nil, &reactions)
|
|
|
|
return reactions, resp, err
|
2020-02-07 08:53:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// editReactionOption contain the reaction type
|
|
|
|
type editReactionOption struct {
|
|
|
|
Reaction string `json:"content"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostIssueReaction add a reaction to an issue
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) PostIssueReaction(owner, repo string, index int64, reaction string) (*Reaction, *Response, error) {
|
2020-11-14 05:31:01 -05:00
|
|
|
if err := c.checkServerVersionGreaterThanOrEqual(version1_11_0); err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, nil, err
|
2020-02-07 08:53:15 -05:00
|
|
|
}
|
|
|
|
reactionResponse := new(Reaction)
|
|
|
|
body, err := json.Marshal(&editReactionOption{Reaction: reaction})
|
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, nil, err
|
2020-02-07 08:53:15 -05:00
|
|
|
}
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("POST",
|
|
|
|
fmt.Sprintf("/repos/%s/%s/issues/%d/reactions", owner, repo, index),
|
2020-02-07 08:53:15 -05:00
|
|
|
jsonHeader, bytes.NewReader(body), reactionResponse)
|
2020-09-15 22:01:41 -04:00
|
|
|
return reactionResponse, resp, err
|
2020-02-07 08:53:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteIssueReaction remove a reaction from an issue
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) DeleteIssueReaction(owner, repo string, index int64, reaction string) (*Response, error) {
|
2020-11-14 05:31:01 -05:00
|
|
|
if err := c.checkServerVersionGreaterThanOrEqual(version1_11_0); err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, err
|
2020-02-07 08:53:15 -05:00
|
|
|
}
|
|
|
|
body, err := json.Marshal(&editReactionOption{Reaction: reaction})
|
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, err
|
2020-02-07 08:53:15 -05:00
|
|
|
}
|
2020-09-15 22:01:41 -04:00
|
|
|
_, resp, err := c.getResponse("DELETE", fmt.Sprintf("/repos/%s/%s/issues/%d/reactions", owner, repo, index), jsonHeader, bytes.NewReader(body))
|
|
|
|
return resp, err
|
2020-02-07 08:53:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// PostIssueCommentReaction add a reaction to a comment of an issue
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) PostIssueCommentReaction(owner, repo string, commentID int64, reaction string) (*Reaction, *Response, error) {
|
2020-11-14 05:31:01 -05:00
|
|
|
if err := c.checkServerVersionGreaterThanOrEqual(version1_11_0); err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, nil, err
|
2020-02-07 08:53:15 -05:00
|
|
|
}
|
|
|
|
reactionResponse := new(Reaction)
|
|
|
|
body, err := json.Marshal(&editReactionOption{Reaction: reaction})
|
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, nil, err
|
2020-02-07 08:53:15 -05:00
|
|
|
}
|
2020-09-15 22:01:41 -04:00
|
|
|
resp, err := c.getParsedResponse("POST",
|
|
|
|
fmt.Sprintf("/repos/%s/%s/issues/comments/%d/reactions", owner, repo, commentID),
|
2020-02-07 08:53:15 -05:00
|
|
|
jsonHeader, bytes.NewReader(body), reactionResponse)
|
2020-09-15 22:01:41 -04:00
|
|
|
return reactionResponse, resp, err
|
2020-02-07 08:53:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteIssueCommentReaction remove a reaction from a comment of an issue
|
2020-09-15 22:01:41 -04:00
|
|
|
func (c *Client) DeleteIssueCommentReaction(owner, repo string, commentID int64, reaction string) (*Response, error) {
|
2020-11-14 05:31:01 -05:00
|
|
|
if err := c.checkServerVersionGreaterThanOrEqual(version1_11_0); err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, err
|
2020-02-07 08:53:15 -05:00
|
|
|
}
|
|
|
|
body, err := json.Marshal(&editReactionOption{Reaction: reaction})
|
|
|
|
if err != nil {
|
2020-09-15 22:01:41 -04:00
|
|
|
return nil, err
|
2020-02-07 08:53:15 -05:00
|
|
|
}
|
2020-09-15 22:01:41 -04:00
|
|
|
_, resp, err := c.getResponse("DELETE",
|
|
|
|
fmt.Sprintf("/repos/%s/%s/issues/comments/%d/reactions", owner, repo, commentID),
|
2020-02-07 08:53:15 -05:00
|
|
|
jsonHeader, bytes.NewReader(body))
|
2020-09-15 22:01:41 -04:00
|
|
|
return resp, err
|
2020-02-07 08:53:15 -05:00
|
|
|
}
|