forked from dstotijn/go-notion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment.go
80 lines (69 loc) · 2.09 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
package notion
import (
"encoding/json"
"errors"
"time"
)
// Comment represents a comment on a Notion page or block.
// See: https://developers.notion.com/reference/comment-object
type Comment struct {
ID string `json:"id"`
Parent Parent `json:"parent"`
DiscussionID string `json:"discussion_id"`
RichText []RichText `json:"rich_text"`
CreatedTime time.Time `json:"created_time"`
LastEditedTime time.Time `json:"last_edited_time"`
CreatedBy BaseUser `json:"created_by"`
}
// CreateCommentParams are the params used for creating a comment.
type CreateCommentParams struct {
// Either ParentPageID or DiscussionID must be non-empty. Also cannot be set
// both at the same time.
ParentPageID string
DiscussionID string
RichText []RichText
}
func (p CreateCommentParams) Validate() error {
if p.ParentPageID == "" && p.DiscussionID == "" {
return errors.New("either parent page ID or discussion ID is required")
}
if p.ParentPageID != "" && p.DiscussionID != "" {
return errors.New("parent page ID and discussion ID cannot both be non-empty")
}
if len(p.RichText) == 0 {
return errors.New("rich text is required")
}
return nil
}
func (p CreateCommentParams) MarshalJSON() ([]byte, error) {
type CreateCommentParamsDTO struct {
Parent *Parent `json:"parent,omitempty"`
DiscussionID string `json:"discussion_id,omitempty"`
RichText []RichText `json:"rich_text"`
}
dto := CreateCommentParamsDTO{
RichText: p.RichText,
}
if p.ParentPageID != "" {
dto.Parent = &Parent{
Type: ParentTypePage,
PageID: p.ParentPageID,
}
} else {
dto.DiscussionID = p.DiscussionID
}
return json.Marshal(dto)
}
// FindCommentsByBlockIDQuery is used when listing comments.
type FindCommentsByBlockIDQuery struct {
BlockID string
StartCursor string
PageSize int
}
// FindCommentsResponse contains results (comments) and pagination data returned
// from a list request.
type FindCommentsResponse struct {
Results []Comment `json:"results"`
HasMore bool `json:"has_more"`
NextCursor *string `json:"next_cursor"`
}