-
Notifications
You must be signed in to change notification settings - Fork 13
/
content.go
369 lines (326 loc) · 7.06 KB
/
content.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package brutalinks
import (
"html/template"
"sort"
"strings"
"time"
"unicode"
vocab "github.com/go-ap/activitypub"
mark "gitlab.com/golang-commonmark/markdown"
)
type FlagBits uint8
const (
FlagsDeleted = FlagBits(1 << iota)
FlagsOperator
FlagsModerator
FlagsApplication
FlagsGroup
FlagsService
FlagsPrivate
FlagsNone = FlagBits(0)
)
const (
MimeTypeURL = "application/url"
MimeTypeHTML = "text/html"
MimeTypeMarkdown = "text/markdown"
MimeTypeText = "text/plain"
MimeTypeSVG = "image/svg+xml"
MimeTypeCss = "text/css"
)
func (f *FlagBits) FromInt64() error {
return nil
}
func (f FlagBits) MarshalJSON() ([]byte, error) {
pieces := make([]string, 0)
if f|FlagsDeleted == f {
pieces = append(pieces, "Deleted")
}
if f|FlagsOperator == f {
pieces = append(pieces, "Operator")
}
if f|FlagsModerator == f {
pieces = append(pieces, "Moderator")
}
if f|FlagsApplication == f {
pieces = append(pieces, "Application")
}
if f|FlagsGroup == f {
pieces = append(pieces, "Group")
}
if f|FlagsService == f {
pieces = append(pieces, "Service")
}
if f|FlagsPrivate == f {
pieces = append(pieces, "Private")
}
if len(pieces) == 0 {
return []byte("None"), nil
}
return []byte(`"` + strings.Join(pieces, "|") + `"`), nil
}
type ItemCollection []Item
var MdPolicy = mark.New(
mark.HTML(true),
mark.Tables(true),
mark.Linkify(false),
mark.Breaks(false),
mark.Typographer(false),
mark.XHTMLOutput(false),
)
// Markdown outputs the markdown render of a string
func Markdown(data string) template.HTML {
return template.HTML(MdPolicy.RenderToString([]byte(data)))
}
// HasMetadata
func (i *Item) HasMetadata() bool {
return i != nil && i.Metadata != nil
}
// IsFederated
func (i Item) IsFederated() bool {
return !i.IsLocal()
}
// IsLocal
func (i Item) IsLocal() bool {
if !i.HasMetadata() {
return true
}
if len(i.Metadata.ID) > 0 {
return HostIsLocal(i.Metadata.ID)
}
if len(i.Metadata.URL) > 0 {
return HostIsLocal(i.Metadata.URL)
}
return true
}
const Edit = "edit"
const Delete = "rm"
const Report = "bad"
const Yay = "yay"
const Nay = "nay"
type RenderType int
const (
UnknownType RenderType = -1
CommentType RenderType = iota
FollowType
AppreciationType
ActorType
ModerationType
)
type Renderable interface {
ID() Hash
AP() vocab.Item
IsValid() bool
Type() RenderType
Date() time.Time
Children() *RenderableList
}
type HasContent interface {
Content() map[string][]byte
Tags() TagCollection
Mentions() TagCollection
}
// Item
type Item struct {
Hash Hash `json:"hash"`
Title string `json:"-"`
MimeType string `json:"-"`
Data string `json:"-"`
Votes VoteCollection `json:"-"`
SubmittedAt time.Time `json:"-"`
SubmittedBy *Account `json:"by,omitempty"`
UpdatedAt time.Time `json:"-"`
UpdatedBy *Account `json:"-"`
Flags FlagBits `json:"-"`
Metadata *ItemMetadata `json:"-"`
Pub vocab.Item `json:"-"`
Parent Renderable `json:"-"`
OP Renderable `json:"-"`
Level uint8 `json:"-"`
children RenderableList `json:"-"`
}
func (i *Item) ID() Hash {
if i == nil {
return AnonymousHash
}
return i.Hash
}
func (i *Item) Children() *RenderableList {
return &i.children
}
func (i *Item) Type() RenderType {
return CommentType
}
func (i Item) Date() time.Time {
return i.SubmittedAt
}
func (i Item) Score() int {
return i.Votes.Score()
}
// IsTop returns true if current item is a top level submission
func (i *Item) IsTop() bool {
if i == nil || i.Pub == nil {
return false
}
isTop := false
vocab.OnObject(i.Pub, func(o *vocab.Object) error {
if o.InReplyTo == nil {
isTop = true
}
return nil
})
return isTop
}
func (i ItemCollection) Contains(cc Item) bool {
for _, com := range i {
if com.Hash == cc.Hash {
return true
}
}
return false
}
func (i ItemCollection) getItemsHashes() Hashes {
var items = make(Hashes, len(i))
for k, com := range i {
items[k] = com.Hash
}
return items
}
func inRange(n string, nn map[string]string) bool {
for k := range nn {
if k == n {
return true
}
}
return false
}
func replaceBetweenPos(d, r []byte, st, end int) []byte {
if st < 0 || end > len(d) {
return d
}
if end < len(d) {
r = append(r, d[end:]...)
}
return append(d[:st], r...)
}
func isWordDelimiter(b byte) bool {
return unicode.Is(unicode.Number, rune(b)) ||
unicode.Is(unicode.Letter, rune(b)) ||
unicode.Is(unicode.Punct, rune(b))
}
func addLevels(allComments RenderableList) RenderableList {
if len(allComments) == 0 {
return nil
}
leveled := make(Hashes, 0)
var setLevel func(RenderableList, uint8)
setLevel = func(com RenderableList, pL uint8) {
for _, cur := range com {
if cur == nil || leveled.Contains(cur.ID()) {
continue
}
if cur.Children() == nil {
continue
}
switch c := cur.(type) {
case *Item:
c.Level = pL
setLevel(c.children, pL+1)
case *Account:
c.Level = pL
setLevel(c.children, pL+1)
}
leveled = append(leveled, cur.ID())
}
}
setLevel(allComments, 0)
return allComments
}
type ItemPtrCollection []*Item
func (h ItemPtrCollection) Contains(it Item) bool {
for _, hh := range h {
if hh.Hash == it.Hash {
return true
}
}
return false
}
func (h ItemPtrCollection) Sorted() ItemPtrCollection {
sort.SliceStable(h, func(i, j int) bool {
ii := h[i]
ij := h[j]
return ii.Votes.Score() > ij.Votes.Score() || (ii.Votes.Score() == ij.Votes.Score() && ii.SubmittedAt.After(ij.SubmittedAt))
})
return h
}
func parentByPub(t ItemPtrCollection, cur *Item) *Item {
var inReplyTo vocab.ItemCollection
vocab.OnObject(cur.Pub, func(ob *vocab.Object) error {
if ob.InReplyTo != nil {
vocab.OnCollectionIntf(ob.InReplyTo, func(col vocab.CollectionInterface) error {
inReplyTo = col.Collection()
return nil
})
}
return nil
})
if len(inReplyTo) == 0 {
return nil
}
for _, n := range t {
for _, pp := range inReplyTo {
if n.Pub == nil {
continue
}
if pp.GetLink().Equals(n.Pub.GetLink(), false) {
return n
}
}
}
return nil
}
func parentByHash(t RenderableList, cur Renderable) Renderable {
for _, n := range t {
switch c := cur.(type) {
case *Item:
if c.Parent != nil && c.Parent.IsValid() {
if c.Parent.ID() == n.ID() {
return n
}
}
case *Account:
if c.CreatedBy.IsValid() {
if c.CreatedBy.ID() == n.ID() {
return n
}
}
}
}
return nil
}
func reparentRenderables(allComments RenderableList) RenderableList {
if len(allComments) == 0 {
return allComments
}
var parFn = parentByHash
retComments := make(RenderableList, 0)
for _, cur := range allComments {
if par := parFn(allComments, cur); par != nil {
if par.Children().Contains(cur) {
continue
}
par.Children().Append(cur)
switch c := cur.(type) {
case *Item:
c.Parent, _ = par.(*Item)
case *Account:
c.Parent, _ = par.(*Account)
}
} else {
if cur == nil || retComments.Contains(cur) {
continue
}
retComments = append(retComments, cur)
}
}
return addLevels(retComments)
}