~mariusor/motley

motley/fedbox.go -rw-r--r-- 14.9 KiB
5be6bfcdMarius Orcsik Padding the pager contents 4 months ago
                                                                                
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
package motley

import (
	"context"
	"fmt"
	"net/url"
	"path"
	"path/filepath"

	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
	pub "github.com/go-ap/activitypub"
	"github.com/go-ap/errors"
	fbox "github.com/go-ap/fedbox/activitypub"
	"github.com/go-ap/processing"
	tree "github.com/mariusor/bubbles-tree"
	"github.com/mariusor/qstring"
	"github.com/openshift/osin"
	"github.com/sirupsen/logrus"
	"golang.org/x/sync/errgroup"
)

const (
	//HasChanges   = "⧆"
	//Locked       = "⚿"
	Collapsed    = "⊞"
	Expanded     = "⊟"
	Unexpandable = "⬚"
	//Unexpandable = "⊠"
	Attention = "⊡"
)

const (
	NodeSyncing = tree.NodeLastChild << (iota + 1)
	NodeSynced
	NodeError
)

type loggerFn func(string, ...interface{})

var logFn = func(string, ...interface{}) {}

type fedbox struct {
	tree  map[pub.IRI]pub.Item
	iri   pub.IRI
	s     processing.Store
	o     osin.Storage
	logFn loggerFn
}

func FedBOX(base pub.IRI, r processing.Store, o osin.Storage, l *logrus.Logger) *fedbox {
	logFn = l.Infof
	return &fedbox{tree: make(map[pub.IRI]pub.Item), iri: base, s: r, o: o, logFn: l.Infof}
}

func (f *fedbox) getService() pub.Item {
	col, err := f.s.Load(f.iri)
	if err != nil {
		return nil
	}
	var service pub.Item
	pub.OnActor(col, func(o *pub.Actor) error {
		service = o
		return nil
	})
	return service
}

func initNodes(f *fedbox) tree.Nodes {
	n := node(
		f.getService(),
		withState(tree.NodeLastChild|tree.NodeSelected),
	)

	return tree.Nodes{n}
}

type n struct {
	pub.Item
	n string
	p *n
	c []*n
	s tree.NodeState
}

func (n *n) Parent() tree.Node {
	if n.p == nil {
		return nil
	}
	return n.p
}
func (n *n) Init() tea.Cmd {
	return nil
}

func nodeIsError(n *n) bool {
	return n.s.Is(NodeError)
}

func iriIsCollection(iri pub.IRI) bool {
	if _, typ := pub.Split(iri); len(typ) > 0 {
		return true
	}
	if _, typ := fbox.FedBOXCollections.Split(iri); len(typ) > 0 {
		return true
	}
	return false
}

func nodeIsCollapsible(n *n) bool {
	it := n.Item
	if len(n.c) > 0 {
		return true
	}
	if iriIsCollection(it.GetLink()) {
		n.s |= tree.NodeCollapsed | tree.NodeCollapsible
	}
	return n.s.Is(tree.NodeCollapsible)
}

func (n *n) View() string {
	if n == nil || n.s.Is(tree.NodeHidden) {
		return ""
	}
	hints := n.s
	annotation := ""
	st := lipgloss.NewStyle()
	if nodeIsError(n) {
		st = faintRedFg
		annotation = Attention
	}

	if n.Item != nil && nodeIsCollapsible(n) {
		annotation = Expanded
		if hints.Is(tree.NodeCollapsed) {
			annotation = Collapsed
		}
		if len(n.c) == 0 {
			annotation = Unexpandable
			st = st.Faint(true)
		}
		if n.s.Is(NodeSyncing) {
			annotation = Attention
		}
	}

	return fmt.Sprintf("%-1s %s", annotation, st.Render(n.n))
}

func (n *n) Children() tree.Nodes {
	nodes := make(tree.Nodes, len(n.c))
	for i, nn := range n.c {
		nodes[i] = nn
	}
	return nodes
}

func (n *n) State() tree.NodeState {
	return n.s
}

func (n *n) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
	switch m := msg.(type) {
	case tree.NodeState:
		n.s = m
	}
	return n, nil
}

func (n *n) setChildren(c ...*n) {
	for i, nnn := range c {
		if i == len(c)-1 {
			nnn.s |= tree.NodeLastChild
		}
		nnn.p = n
		n.c = append(n.c, nnn)
	}
}

func withName(name string) func(*n) {
	return func(nn *n) {
		nn.n = name
	}
}

func withParent(p *n) func(*n) {
	return func(nn *n) {
		//nn.f = p.f
		nn.p = p
	}
}

func withState(st tree.NodeState) func(*n) {
	return func(nn *n) {
		nn.s = st
	}
}

func withChildren(c ...*n) func(*n) {
	return func(nn *n) {
		nn.setChildren(c...)
	}
}

func getNameFromItem(it pub.Item) string {
	name := filepath.Base(it.GetLink().String())
	var err error
	typ := it.GetType()
	switch {
	case pub.ActorTypes.Contains(typ):
		err = pub.OnActor(it, func(act *pub.Actor) error {
			if len(act.PreferredUsername) > 0 {
				name = fmt.Sprintf("%s", act.PreferredUsername.First())
			} else if len(act.Name) > 0 {
				name = fmt.Sprintf("%s", act.Name.First())
			}
			return nil
		})
	case pub.ActivityTypes.Contains(typ), pub.IntransitiveActivityTypes.Contains(typ):
		err = pub.OnIntransitiveActivity(it, func(act *pub.IntransitiveActivity) error {
			name = fmt.Sprintf("%s", typ)
			return nil
		})
	case pub.ObjectTypes.Contains(typ):
		err = pub.OnObject(it, func(ob *pub.Object) error {
			if len(ob.Name) > 0 {
				name = fmt.Sprintf("[%s]%s", typ, ob.Name.First())
			} else {
				name = fmt.Sprintf("%s", typ)
			}
			return nil
		})
	case typ == "":
		err = pub.OnObject(it, func(ob *pub.Object) error {
			if len(ob.Name) > 0 {
				name = fmt.Sprintf("%s", ob.Name.First())
			}
			return nil
		})
	}
	if err != nil && len(name) == 0 {
		return err.Error()
	}
	return name
}

func node(it pub.Item, fns ...func(*n)) *n {
	n := &n{Item: it}

	if it == nil {
		n.s = NodeError
		n.n = "Invalid ActivityPub object"
		n.c = n.c[:0]
		return n
	}

	n.n = getNameFromItem(it)
	n.c = getItemElements(n)

	for _, fn := range fns {
		fn(n)
	}
	if len(n.c) > 0 || pub.IsItemCollection(it) || pub.ValidCollectionIRI(it.GetLink()) {
		n.s |= tree.NodeCollapsible
	}
	return n
}

func getObjectElements(ob pub.Object, parent *n) []*n {
	result := make([]*n, 0)
	if ob.Likes != nil {
		result = append(result, node(ob.Likes, withParent(parent), withState(tree.NodeCollapsed)))
	}
	if ob.Shares != nil {
		result = append(result, node(ob.Shares, withParent(parent), withState(tree.NodeCollapsed)))
	}
	if ob.Replies != nil {
		result = append(result, node(ob.Replies, withParent(parent), withState(tree.NodeCollapsed)))
	}
	return result
}

func getActorElements(act pub.Actor, parent *n) []*n {
	result := make([]*n, 0)
	pub.OnObject(&act, func(o *pub.Object) error {
		result = append(result, getObjectElements(*o, parent)...)
		return nil
	})
	if act.Inbox != nil {
		result = append(result, node(act.Inbox, withParent(parent), withState(tree.NodeCollapsed)))
	}
	if act.Outbox != nil {
		result = append(result, node(act.Outbox, withParent(parent), withState(tree.NodeCollapsed)))
	}
	if act.Liked != nil {
		result = append(result, node(act.Liked, withParent(parent), withState(tree.NodeCollapsed)))
	}
	if act.Followers != nil {
		result = append(result, node(act.Followers, withParent(parent), withState(tree.NodeCollapsed)))
	}
	if act.Following != nil {
		result = append(result, node(act.Following, withParent(parent), withState(tree.NodeCollapsed)))
	}
	if act.Streams != nil {
		result = append(result, node(act.Streams, withName("streams"), withParent(parent), withState(tree.NodeCollapsed)))
	}
	return result
}

func getItemElements(parent *n) []*n {
	result := make([]*n, 0)
	it := parent.Item

	if pub.IsItemCollection(it) {
		pub.OnItemCollection(it, func(c *pub.ItemCollection) error {
			for _, ob := range c.Collection() {
				result = append(result, node(ob, withParent(parent)))
			}
			return nil
		})
	}
	if pub.ActorTypes.Contains(it.GetType()) {
		pub.OnActor(it, func(act *pub.Actor) error {
			result = append(result, getActorElements(*act, parent)...)
			return nil
		})
	}
	if pub.ActivityTypes.Contains(it.GetType()) || pub.ObjectTypes.Contains(it.GetType()) {
		pub.OnObject(it, func(act *pub.Object) error {
			result = append(result, getObjectElements(*act, parent)...)
			return nil
		})
	}
	return result
}

func (m *model) loadDepsForNode(ctx context.Context, node *n) tea.Cmd {
	g, gtx := errgroup.WithContext(ctx)
	node.s |= NodeSyncing
	g.Go(func() error {
		if err := dereferenceItemProperties(gtx, m.f, node.Item); err != nil {
			m.logFn("error while loading attributes %s", err)
			node.s |= NodeError
			return err
		}
		return nil
	})

	g.Go(func() error {
		if node.s.Is(tree.NodeCollapsible) && len(node.c) == 0 {
			if err := m.loadChildrenForNode(ctx, node); err != nil {
				node.s |= NodeError
				m.logFn("error while loading children %s", err)
				return err
			}
		}
		return nil
	})
	go func() {
		g.Wait()
		node.s ^= NodeSyncing
	}()
	return m.status.spinner.Tick
}

func (m *model) loadChildrenForNode(ctx context.Context, nn *n) error {
	iri := nn.Item.GetLink()
	accum := func(children *[]*n) func(ctx context.Context, col pub.CollectionInterface) error {
		return func(ctx context.Context, col pub.CollectionInterface) error {
			for _, it := range col.Collection() {
				*children = append(*children, node(it, withState(tree.NodeCollapsed)))
			}
			return nil
		}
	}

	children := make([]*n, 0)
	if err := accumFn(accum(&children)).LoadFromSearch(ctx, m.f, iri); err != nil {
		return err
	}
	nn.setChildren(children...)
	return nil
}

func dereferenceIRIs(ctx context.Context, f *fedbox, iris pub.ItemCollection) pub.ItemCollection {
	if len(iris) == 0 {
		return nil
	}
	items := make(pub.ItemCollection, 0, len(iris))
	for _, it := range iris {
		if deref := dereferenceIRI(ctx, f, it); pub.IsItemCollection(deref) {
			pub.OnItemCollection(deref, func(col *pub.ItemCollection) error {
				items = append(items, pub.ItemCollectionDeduplication(col)...)
				return nil
			})
		} else {
			items = append(items, deref)
		}
	}
	return items
}

func dereferenceIRI(ctx context.Context, f *fedbox, it pub.Item) pub.Item {
	if pub.IsNil(it) {
		return nil
	}
	if !pub.IsIRI(it) {
		return it
	}
	if pub.PublicNS.Equals(it.GetLink(), false) {
		return it
	}
	loadFn := func(ctx context.Context, col pub.CollectionInterface) error {
		it = col
		return nil
	}
	accumFn(loadFn).LoadFromSearch(ctx, f, it.GetLink())

	return it
}

func dereferenceIntransitiveActivityProperties(ctx context.Context, f *fedbox) func(act *pub.IntransitiveActivity) error {
	if f == nil {
		return func(act *pub.IntransitiveActivity) error { return fmt.Errorf("invalid fedbox storage") }
	}
	return func(act *pub.IntransitiveActivity) error {
		g, gtx := errgroup.WithContext(ctx)
		g.Go(func() error {
			pub.OnObject(act, dereferenceObjectProperties(gtx, f))
			act.Actor = dereferenceIRI(gtx, f, act.Actor)
			act.Target = dereferenceIRI(gtx, f, act.Target)
			act.Instrument = dereferenceIRI(gtx, f, act.Instrument)
			act.Result = dereferenceIRI(gtx, f, act.Result)
			return nil
		})
		return g.Wait()
	}
}

func dereferenceActivityProperties(ctx context.Context, f *fedbox) func(act *pub.Activity) error {
	if f == nil {
		return func(act *pub.Activity) error { return fmt.Errorf("invalid fedbox storage") }
	}
	return func(act *pub.Activity) error {
		g, gtx := errgroup.WithContext(ctx)
		g.Go(func() error {
			pub.OnIntransitiveActivity(act, dereferenceIntransitiveActivityProperties(ctx, f))
			act.Actor = dereferenceIRI(gtx, f, act.Actor)
			return nil
		})
		return g.Wait()
	}
}

func dereferenceObjectProperties(ctx context.Context, f *fedbox) func(ob *pub.Object) error {
	if f == nil {
		return func(ob *pub.Object) error { return fmt.Errorf("invalid fedbox storage") }
	}
	return func(ob *pub.Object) error {
		g, gtx := errgroup.WithContext(ctx)
		g.Go(func() error {
			ob.AttributedTo = dereferenceIRI(gtx, f, ob.AttributedTo)
			ob.InReplyTo = dereferenceIRI(gtx, f, ob.InReplyTo)
			ob.Tag = dereferenceIRIs(gtx, f, ob.Tag)
			ob.To = dereferenceIRIs(ctx, f, ob.To)
			ob.CC = dereferenceIRIs(ctx, f, ob.CC)
			ob.Bto = dereferenceIRIs(ctx, f, ob.Bto)
			ob.BCC = dereferenceIRIs(ctx, f, ob.BCC)
			ob.Audience = dereferenceIRIs(ctx, f, ob.Audience)
			return nil
		})
		return g.Wait()
	}
}

type StopLoad struct{}

func (s StopLoad) Error() string {
	return "stop load"
}

func dereferenceItemProperties(ctx context.Context, f *fedbox, it pub.Item) error {
	if pub.IsObject(it) {
		typ := it.GetType()
		switch {
		case pub.ObjectTypes.Contains(typ), pub.ActorTypes.Contains(typ), typ == "":
			return pub.OnObject(it, dereferenceObjectProperties(ctx, f))
		case pub.IntransitiveActivityTypes.Contains(typ):
			return pub.OnIntransitiveActivity(it, dereferenceIntransitiveActivityProperties(ctx, f))
		case pub.ActivityTypes.Contains(typ):
			return pub.OnActivity(it, dereferenceActivityProperties(ctx, f))
		}
	}

	if pub.IsItemCollection(it) {
		return pub.OnItemCollection(it, func(col *pub.ItemCollection) error {
			it = dereferenceIRIs(ctx, f, *col)
			return nil
		})
	}
	return nil
}

func getCollectionPrevNext(col pub.Item) (prev, next string) {
	qFn := func(i pub.Item) url.Values {
		if i == nil {
			return url.Values{}
		}
		if u, err := i.GetLink().URL(); err == nil {
			return u.Query()
		}
		return url.Values{}
	}
	beforeFn := func(i pub.Item) string {
		return qFn(i).Get("before")
	}
	afterFn := func(i pub.Item) string {
		return qFn(i).Get("after")
	}
	nextFromLastFn := func(i pub.Item) string {
		if u, err := i.GetLink().URL(); err == nil {
			_, next = path.Split(u.Path)
			return next
		}
		return ""
	}
	switch col.GetType() {
	case pub.OrderedCollectionPageType:
		if c, ok := col.(*pub.OrderedCollectionPage); ok {
			prev = beforeFn(c.Prev)
			if int(c.TotalItems) > len(c.OrderedItems) {
				next = afterFn(c.Next)
			}
		}
	case pub.OrderedCollectionType:
		if c, ok := col.(*pub.OrderedCollection); ok {
			if len(c.OrderedItems) > 0 && int(c.TotalItems) > len(c.OrderedItems) {
				next = nextFromLastFn(c.OrderedItems[len(c.OrderedItems)-1])
			}
		}
	case pub.CollectionPageType:
		if c, ok := col.(*pub.CollectionPage); ok {
			prev = beforeFn(c.Prev)
			if int(c.TotalItems) > len(c.Items) {
				next = afterFn(c.Next)
			}
		}
	case pub.CollectionType:
		if c, ok := col.(*pub.Collection); ok {
			if len(c.Items) > 0 && int(c.TotalItems) > len(c.Items) {
				next = nextFromLastFn(c.Items[len(c.Items)-1])
			}
		}
	}
	// NOTE(marius): we check if current Collection id contains a cursor, and if `next` points to the same URL
	//   we don't take it into consideration.
	if next != "" {
		f := struct {
			Next string `qstring:"after"`
		}{}
		if err := qstring.Unmarshal(qFn(col.GetLink()), &f); err == nil && next == f.Next {
			next = ""
		}
	}
	return prev, next
}

const MaxItems = 100

type accumFn func(context.Context, pub.CollectionInterface) error

func (f *fedbox) searchFn(ctx context.Context, g *errgroup.Group, loadIRI pub.IRI, accumFn accumFn) func() error {
	return func() error {
		col, err := f.s.Load(loadIRI)
		if err != nil {
			return errors.Annotatef(err, "failed to load search: %s", loadIRI)
		}

		if pub.IsItemCollection(col) {
			maxItems := 0
			err = pub.OnCollectionIntf(col, func(c pub.CollectionInterface) error {
				maxItems = int(c.Count())
				return accumFn(ctx, c)
			})
			if err != nil {
				return err
			}

			var next string

			if maxItems-MaxItems < 5 {
				if _, next = getCollectionPrevNext(col); len(next) > 0 {
					g.Go(f.searchFn(ctx, g, loadIRI, accumFn))
				}
				return nil
			} else {
				return StopLoad{}
			}
		}
		return accumFn(ctx, &pub.ItemCollection{col})
	}
}

func emptyAccum(ctx context.Context, c pub.CollectionInterface) error {
	return nil
}

func (a accumFn) LoadFromSearch(ctx context.Context, f *fedbox, iris ...pub.IRI) error {
	var cancelFn func()

	ctx, cancelFn = context.WithCancel(ctx)
	g, gtx := errgroup.WithContext(ctx)

	for _, iri := range iris {
		g.Go(f.searchFn(gtx, g, iri, a))
	}
	if err := g.Wait(); err != nil {
		if errors.Is(err, StopLoad{}) {
			f.logFn("stopped loading search")
			cancelFn()
		} else {
			f.logFn("%s", err)
		}
	}
	return nil
}

func pubUrl(it pub.Item) string {
	name := ""
	pub.OnObject(it, func(o *pub.Object) error {
		u, _ := o.URL.GetLink().URL()
		name = u.Hostname()
		return nil
	})
	return name
}