/
blob.go
186 lines (162 loc) · 4.67 KB
/
blob.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package types
import (
"net/http"
"strings"
"time"
)
type AttachmentType string
const (
Attachment = "attachment"
EncryptedAttachment = "encrypted_attachment"
EncryptedBody = "encrypted_body"
)
type BlobHistoryItem struct {
AsOf time.Time `json:"history_time"`
BlobValue *Blob `json:"blob_value"`
}
type BlobBinaryAttachment struct {
Description string `json:"description"`
Data []byte `json:"data"`
Type AttachmentType `json:"type"`
}
type Blob struct {
Id string `json:"id"`
Title string `json:"title"`
Type string `json:"type"`
Data string `json:"data"`
RawData []BlobBinaryAttachment `json:"raw_data"`
Importance int `json:"importance"`
Tags []string `json:"tags"`
Deleted bool `json:"deleted"`
ChildIds []string `json:"child_ids,omitempty"`
Children []*Blob `json:"children,omitempty"`
OwnerId string `json:"owner_id,omitempty"`
ParentId string `json:"parent_id,omitempty"`
ExpireTime *time.Time `json:"expire_time,omitempty"`
VersionHistory []*BlobHistoryItem `json:"version_history,omitempty"`
}
func (blob *Blob) Size() uint64 {
s := uint64(len(blob.Data))
for _, x := range blob.RawData {
s += uint64(len(x.Data))
}
return s
}
func (blob *Blob) IsEncryptedAndEmpty() bool {
if blob.Data == "" {
for _, x := range blob.RawData {
if x.Type == EncryptedBody {
return true
}
}
}
return false
}
func (blob *Blob) EncryptedBody() *BlobBinaryAttachment {
for _, x := range blob.RawData {
if x.Type == EncryptedBody {
return &x
}
}
return nil
}
func (blob *Blob) GetAttachment(desc string) ([]byte, bool) {
for _, x := range blob.RawData {
if x.Description == desc && x.Type == EncryptedAttachment {
return x.Data, true
} else if x.Description == desc && x.Type == Attachment {
return x.Data, false
}
}
return nil, false
}
type BlobSkeleton struct {
Id string `json:"id"`
Title string `json:"title"`
Type string `json:"type"`
Importance int `json:"importance"`
Tags []string `json:"tags"`
Deleted bool `json:"deleted"`
Children []*BlobSkeleton `json:"children,omitempty"`
OwnerId string `json:"owner_id,omitempty"`
ParentId string `json:"parent_id,omitempty"`
}
type PostResponse struct {
Posts []*Post `json:"posts"`
Blobs []*Blob `json:"blobs,omitempty"`
Body string `json:"body,omitempty"`
EncryptedBody []byte `json:"encrypted_body,omitempty"`
}
func (b *PostResponse) Render(w http.ResponseWriter, r *http.Request) error {
return nil
}
type BasicAuthCredentials struct {
UserEmail string `json:"email"`
UserPassword string `json:"password"`
DisplayName string `json:"display_name,omitempty"`
}
type AuthTokens struct {
AccessToken string `json:"access"`
RefreshToken string `json:"refresh"`
}
func cloneAttachments(attachments []BlobBinaryAttachment) []BlobBinaryAttachment {
var cloned []BlobBinaryAttachment
for _, x := range attachments {
cloned = append(cloned, x)
}
return cloned
}
func cloneBlobArray(ss []*Blob) []*Blob {
var out []*Blob
for _, x := range ss {
out = append(out, x)
}
return out
}
func cloneStrArray(ss []string) []string {
var out []string
for _, x := range ss {
out = append(out, x)
}
return out
}
func (b *Blob) Clone() *Blob {
return &Blob{
Id: b.Id,
Title: b.Title,
Type: b.Type,
Data: b.Data,
RawData: cloneAttachments(b.RawData),
Importance: b.Importance,
Tags: b.Tags,
Deleted: b.Deleted,
ChildIds: cloneStrArray(b.ChildIds),
Children: cloneBlobArray(b.Children),
OwnerId: b.OwnerId,
ParentId: b.ParentId,
VersionHistory: b.VersionHistory,
ExpireTime: b.ExpireTime,
}
}
func _tagsMatch(tags []string, searchString string) bool {
for _, t := range tags {
if t == searchString {
return true
}
}
return false
}
func (b *Blob) Matches(searchString string) bool {
return strings.Contains(b.Title, searchString) || strings.Contains(b.Data, searchString) || b.Type == searchString || _tagsMatch(b.Tags, searchString)
}
type BlobResponse struct {
Blobs []*Blob `json:"blobs"`
}
type BlobList struct {
RootBlobs []*BlobSkeleton `json:"root_blobs"`
}
type UserResponse struct {
Id string `json:"user_id"`
Email string `json:"user_email"`
DisplayName string `json:"display_name"`
}