-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoc.go
More file actions
152 lines (131 loc) · 3.75 KB
/
Copy pathdoc.go
File metadata and controls
152 lines (131 loc) · 3.75 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package docs
import (
"encoding/json"
"fmt"
"net/http"
"github.com/go-lark/docs/doctypes"
)
func newDoc(token string, client *Client) *Doc {
return &Doc{
baseClient: client,
tokenIns: tokenIns{
token: token,
},
}
}
// Doc represent a doc file
type Doc struct {
Err error
tokenIns
baseClient *Client
_comment *comment
}
// GetMeta for doc meta info
func (d *Doc) GetMeta() (*DocMeta, error) {
u := fmt.Sprintf("%s/open-apis/doc/v2/meta/%s", d.baseClient.domain, d.token)
req, _ := http.NewRequest(http.MethodGet, u, nil)
res := &DocMeta{}
_, err := d.baseClient.CommonReq(req, res)
return res, err
}
func (d *Doc) GetContent() ([]byte, *DocContent, int, error) {
u := fmt.Sprintf("%s/open-apis/doc/v2/%s/content", d.baseClient.domain, d.token)
req, _ := http.NewRequest(http.MethodGet, u, nil)
resp := &respGetContent{}
r, e := d.baseClient.CommonReq(req, resp)
if e != nil {
return nil, nil, 0, e
}
content := &DocContent{}
err := json.Unmarshal([]byte(resp.Content), content)
return r, content, resp.Revision, err
}
func (d *Doc) AddWholeComment(content string) (*RespComment, error) {
return d.getComment().AddWholeComment(FileTypeDoc, content)
}
// Share to other user or group.
func (d *Doc) Share(perm Perm, notify bool, members ...*Member) *Doc {
if d.Err != nil {
return d
}
err := d.baseClient.permission().Add(d.token, FileTypeDoc, perm, notify, members...)
d.Err = err
return d
}
func (d *Doc) ChangeOwner(newOwner *Member, removeOldOwner, notify bool) *Doc {
if d.Err != nil {
return d
}
_, _, err := d.baseClient.permission().TransferOwner(d.token, FileTypeDoc, newOwner, removeOldOwner, notify)
if err != nil {
d.Err = err
}
return d
}
func (d *Doc) SetAccessPermission(per string) *Doc {
return d.setPublic(&PublicSet{
LinkShareEntity: &per,
})
}
func (d *Doc) Statistics() (*FileStatistics, error) {
return newFile(d.baseClient).statistics(d.token, FileTypeDoc)
}
func (d *Doc) setPublic(args *PublicSet) *Doc {
if d.Err != nil {
return d
}
err := d.baseClient.permission().PublicSet(args)
d.Err = err
return d
}
type DocMeta struct {
CreateDate string `json:"create_date"`
CreateTime int `json:"create_time"`
CreateUID string `json:"create_uid"`
CreateUserName string `json:"create_user_name"`
DeleteFlag DocDeleteFlag `json:"delete_flag"`
EditTime int64 `json:"edit_time,string"`
EditUserName string `json:"edit_user_name"`
IsExternal bool `json:"is_external"`
IsPined bool `json:"is_pined"`
IsStared bool `json:"is_stared"`
ObjType string `json:"obj_type"`
OwnerID string `json:"owner_id"`
OwnerUserName string `json:"owner_user_name"`
ServerTime int `json:"server_time"`
TenantID string `json:"tenant_id"`
Title string `json:"title"`
Type int `json:"type"`
URL string `json:"url"`
}
type DocDeleteFlag int
var (
DocDeleteFlagNormal DocDeleteFlag = 0
DocDeleteFlagTrashed DocDeleteFlag = 1
DocDeleteFlagDeleted DocDeleteFlag = 2
)
func (d DocDeleteFlag) MarshalJSON() ([]byte, error) {
return []byte{byte(int(d) - 1 + int('1'))}, nil
}
func (d *DocDeleteFlag) UnmarshalJSON(data []byte) error {
*d = DocDeleteFlag(data[0] + 1 - '1')
return nil
}
type DocContent struct {
Title *doctypes.Title `json:"title"`
Body *doctypes.Body `json:"body"`
}
type RespCreateDoc struct {
ObjToken string `json:"objToken"`
URL string `json:"url"`
}
type respGetContent struct {
Content string `json:"content"`
Revision int `json:"revision"`
}
func (d *Doc) getComment() *comment {
if d._comment == nil {
d._comment = newComment(d.token, d.baseClient)
}
return d._comment
}