aboutsummaryrefslogtreecommitdiff
path: root/resources/outbox.go
blob: ed87dd80a344db8184449e0e0f88d00b99f720c6 (plain)
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
package resources

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

	"github.com/dgraph-io/badger/v3"
	"github.com/go-fed/activity/streams"
)

func RenderOutboxPage(name, domain string, db *badger.DB) (map[string]interface{}, error) {
	id, err := url.Parse(path.Join("https://", domain, "actors", name, "outbox") + "?page=true")
	if err != nil {
		return nil, fmt.Errorf("could not partse url: %w", err)
	}

	partOf, err := url.Parse(path.Join("https://", domain, "actors", name, "outbox"))
	if err != nil {
		return nil, fmt.Errorf("could not partse url: %w", err)
	}

	oc := streams.NewActivityStreamsOrderedCollectionPage()

	idProp := streams.NewJSONLDIdProperty()
	idProp.Set(id)
	oc.SetJSONLDId(idProp)

	partOfProp := streams.NewActivityStreamsPartOfProperty()
	partOfProp.SetIRI(partOf)
	oc.SetActivityStreamsPartOf(partOfProp)

	itemsProp := streams.NewActivityStreamsOrderedItemsProperty()

	err = db.View(func(txn *badger.Txn) error {
		it := txn.NewIterator(badger.DefaultIteratorOptions)
		defer it.Close()
		prefix := []byte("outbox:sample") // TODO
		for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
			item := it.Item()
			err := item.Value(func(v []byte) error {
				crea := streams.NewActivityStreamsCreate()
				obj := streams.NewActivityStreamsObjectProperty()
				crea.SetActivityStreamsObject(obj)

				note := streams.NewActivityStreamsNote()
				contentProp := streams.NewActivityStreamsContentProperty()
				contentProp.AppendXMLSchemaString(string(v))
				note.SetActivityStreamsContent(contentProp)
				obj.AppendActivityStreamsNote(note)

				itemsProp.AppendActivityStreamsCreate(crea)
				return nil
			})
			if err != nil {
				return err
			}
		}
		return nil
	})
	if err != nil {
		return nil, err
	}

	oc.SetActivityStreamsOrderedItems(itemsProp)
	return streams.Serialize(oc)
}

func RenderOutbox(name, domain string, db *badger.DB) (map[string]interface{}, error) {
	id, err := url.Parse(path.Join("https://", domain, "actors", name, "outbox") + "?page=true")

	if err != nil {
		return nil, fmt.Errorf("could not partse url: %w", err)
	}

	first, err := url.Parse(path.Join("https://", domain, "actors", name, "outbox") + "?page=true")
	if err != nil {
		return nil, fmt.Errorf("could not partse url: %w", err)
	}

	oc := streams.NewActivityStreamsOrderedCollection()

	idProp := streams.NewJSONLDIdProperty()
	idProp.Set(id)
	oc.SetJSONLDId(idProp)

	var i int
	err = db.View(func(txn *badger.Txn) error {
		it := txn.NewIterator(badger.DefaultIteratorOptions)
		defer it.Close()
		prefix := []byte("outbox:sample") // TODO
		for it.Seek(prefix); it.ValidForPrefix(prefix); it.Next() {
			i++
		}
		return nil
	})
	if err != nil {
		return nil, err
	}

	itemsProp := streams.NewActivityStreamsTotalItemsProperty()
	itemsProp.Set(i)
	oc.SetActivityStreamsTotalItems(itemsProp)

	ocProp := streams.NewActivityStreamsFirstProperty()
	ocProp.SetIRI(first)
	oc.SetActivityStreamsFirst(ocProp)

	return streams.Serialize(oc)
}