-
Notifications
You must be signed in to change notification settings - Fork 13
/
items.go
209 lines (178 loc) · 4.65 KB
/
items.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package brutalinks
import (
"bytes"
"net/http"
"net/url"
"time"
vocab "github.com/go-ap/activitypub"
"github.com/go-ap/errors"
"github.com/go-chi/chi/v5"
)
type ItemMetadata struct {
To AccountCollection `json:"to,omitempty"`
CC AccountCollection `json:"to,omitempty"`
Tags TagCollection `json:"tags,omitempty"`
Mentions TagCollection `json:"mentions,omitempty"`
ID string `json:"id,omitempty"`
URL string `json:"url,omitempty"`
RepliesURI string `json:"replies,omitempty"`
LikesURI string `json:"likes,omitempty"`
SharesURI string `json:"shares,omitempty"`
AuthorURI string `json:"author,omitempty"`
Icon ImageMetadata `json:"icon,omitempty"`
}
var ValidContentTypes = vocab.ActivityVocabularyTypes{
vocab.ArticleType,
vocab.NoteType,
vocab.LinkType,
vocab.PageType,
vocab.DocumentType,
vocab.VideoType,
vocab.AudioType,
}
var ValidContentManagementTypes = vocab.ActivityVocabularyTypes{
vocab.UpdateType,
vocab.CreateType,
vocab.DeleteType,
}
var ContentManagementActivitiesFilter = ActivityTypesFilter(ValidContentManagementTypes...)
type Identifiable interface {
Id() int64
}
func (i *Item) IsValid() bool {
return i != nil && i.Hash.IsValid()
}
// AP returns the underlying actvitypub item
func (i *Item) AP() vocab.Item {
return i.Pub
}
// Content returns the content of the Item
func (i Item) Content() map[string][]byte {
return map[string][]byte{i.MimeType: []byte(i.Data)}
}
// Tags returns the tags associated with the current Item
func (i Item) Tags() TagCollection {
return i.Metadata.Tags
}
// Mentions returns the mentions associated with the current Item
func (i Item) Mentions() TagCollection {
return i.Metadata.Mentions
}
func (i *Item) Deleted() bool {
return i != nil && (i.Flags&FlagsDeleted) == FlagsDeleted
}
// UnDelete remove the deleted flag from an item
func (i *Item) UnDelete() {
i.Flags ^= FlagsDeleted
}
// Delete add the deleted flag on an item
func (i *Item) Delete() {
i.Flags |= FlagsDeleted
}
func (i *Item) Private() bool {
return i != nil && (i.Flags&FlagsPrivate) == FlagsPrivate
}
func (i *Item) Public() bool {
return i != nil && (i.Flags&FlagsPrivate) != FlagsPrivate
}
func (i *Item) MakePrivate() {
i.Flags |= FlagsPrivate
}
func (i *Item) MakePublic() {
i.Flags ^= FlagsPrivate
}
func (i Item) IsLink() bool {
return isDocument(i.MimeType)
}
func (i Item) IsSelf() bool {
return !isDocument(i.MimeType)
}
func (i ItemCollection) First() (*Item, error) {
for _, it := range i {
return &it, nil
}
return nil, errors.Errorf("empty %T", i)
}
func (i ItemCollection) Split(pieceCount int) []ItemCollection {
l := len(i)
if l <= pieceCount {
return []ItemCollection{i}
}
ret := make([]ItemCollection, 0)
for it := 0; it <= l/pieceCount; it++ {
st := it * pieceCount
if st > l {
break
}
end := (it + 1) * pieceCount
if end > l {
end = l
}
ret = append(ret, i[st:end])
}
return ret
}
const (
MaxContentItems = 35
)
func detectMimeType(data string) string {
u, err := url.ParseRequestURI(data)
if err == nil && u != nil && !bytes.ContainsRune([]byte(data), '\n') {
return MimeTypeURL
}
return "text/plain"
}
func updateItemFromRequest(r *http.Request, author Account, i *Item) error {
if r.Method != http.MethodPost {
return errors.Errorf("invalid http method type")
}
var receivers AccountCollection
var err error
if i.Metadata == nil {
i.Metadata = new(ItemMetadata)
}
if hash := HashFromString(r.PostFormValue("hash")); hash.IsValid() {
i.Hash = hash
}
if receivers, err = accountsFromRequestHandle(r); err == nil && chi.URLParam(r, "hash") == "" {
i.MakePrivate()
for _, rec := range receivers {
if !rec.IsValid() {
continue
}
i.Metadata.To = append(i.Metadata.To, rec)
}
}
if tit := r.PostFormValue("title"); len(tit) > 0 {
i.Title = tit
}
if dat := r.PostFormValue("data"); len(dat) > 0 {
i.Data = dat
}
i.SubmittedBy = &author
i.MimeType = detectMimeType(i.Data)
i.Metadata.Tags, i.Metadata.Mentions = loadTags(i.Data)
if !i.IsLink() {
i.MimeType = r.PostFormValue("mime-type")
}
if len(i.Data) > 0 {
now := time.Now().UTC()
i.SubmittedAt = now
i.UpdatedAt = now
}
if parent := HashFromString(r.PostFormValue("parent")); parent.IsValid() {
if i.Parent == nil || i.Parent.ID() != parent {
i.Parent = &Item{Hash: parent}
}
}
if op := HashFromString(r.PostFormValue("op")); op.IsValid() {
if i.OP != nil || i.OP.ID() != op {
i.OP = &Item{Hash: op}
}
}
return nil
}
func ContentFromRequest(r *http.Request, author Account) (Item, error) {
i := Item{}
return i, updateItemFromRequest(r, author, &i)
}