-
Notifications
You must be signed in to change notification settings - Fork 81
/
comment.go
120 lines (99 loc) · 3.2 KB
/
comment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package notionapi
import (
"context"
"encoding/json"
"log"
"net/http"
"time"
)
type CommentID string
func (cID CommentID) String() string {
return string(cID)
}
type CommentService interface {
Create(ctx context.Context, request *CommentCreateRequest) (*Comment, error)
Get(context.Context, BlockID, *Pagination) (*CommentQueryResponse, error)
}
type CommentClient struct {
apiClient *Client
}
// Creates a comment in a page or existing discussion thread.
//
// There are two locations you can add a new comment to:
// 1. A page
// 2. An existing discussion thread
//
// If the intention is to add a new comment to a page, a parent object must be
// provided in the body params. Alternatively, if a new comment is being added
// to an existing discussion thread, the discussion_id string must be provided
// in the body params. Exactly one of these parameters must be provided.
//
// See https://developers.notion.com/reference/create-a-comment
func (cc *CommentClient) Create(ctx context.Context, requestBody *CommentCreateRequest) (*Comment, error) {
res, err := cc.apiClient.request(ctx, http.MethodPost, "comments", nil, requestBody)
if err != nil {
return nil, err
}
defer func() {
if errClose := res.Body.Close(); errClose != nil {
log.Println("failed to close body, should never happen")
}
}()
var response Comment
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}
return &response, nil
}
// CommentCreateRequest represents the request body for CommentClient.Create.
type CommentCreateRequest struct {
Parent Parent `json:"parent,omitempty"`
DiscussionID DiscussionID `json:"discussion_id,omitempty"`
RichText []RichText `json:"rich_text"`
}
// Retrieves a list of un-resolved Comment objects from a page or block.
//
// See https://developers.notion.com/reference/retrieve-a-comment
func (cc *CommentClient) Get(ctx context.Context, id BlockID, pagination *Pagination) (*CommentQueryResponse, error) {
queryParams := map[string]string{}
if pagination != nil {
queryParams = pagination.ToQuery()
}
queryParams["block_id"] = id.String()
res, err := cc.apiClient.request(ctx, http.MethodGet, "comments", queryParams, nil)
if err != nil {
return nil, err
}
defer func() {
if errClose := res.Body.Close(); errClose != nil {
log.Println("failed to close body, should never happen")
}
}()
var response CommentQueryResponse
err = json.NewDecoder(res.Body).Decode(&response)
if err != nil {
return nil, err
}
return &response, nil
}
type DiscussionID string
func (dID DiscussionID) String() string {
return string(dID)
}
type Comment struct {
Object ObjectType `json:"object"`
ID ObjectID `json:"id"`
DiscussionID DiscussionID `json:"discussion_id"`
CreatedTime time.Time `json:"created_time"`
LastEditedTime time.Time `json:"last_edited_time"`
CreatedBy User `json:"created_by,omitempty"`
RichText []RichText `json:"rich_text"`
Parent Parent `json:"parent"`
}
type CommentQueryResponse struct {
Object ObjectType `json:"object"`
Results []Comment `json:"results"`
HasMore bool `json:"has_more"`
NextCursor Cursor `json:"next_cursor"`
}