aboutsummaryrefslogtreecommitdiff
path: root/registry/registry.go
blob: e7f66fcca038a0559c4d02f32d9d2a6a281de952 (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
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
package registry

import (
	"bytes"
	"encoding/gob"
	"net/http"
	"net/url"
	"strings"

	"git.capotej.com/capotej/communique/config"
	"git.capotej.com/capotej/communique/models"
	"git.capotej.com/capotej/communique/urls"
	"git.capotej.com/capotej/communique/views"
)

type Handler struct {
	handlerCfg config.Handler
}

// TODO rename to controller and controller.go
type Registry struct {
	cfg        config.Config
	persister  *models.Persister
	handlerMap map[string]Handler
}

func NewRegistry(cfg config.Config, persister *models.Persister) (*Registry, error) {
	reg := Registry{cfg: cfg, persister: persister}
	reg.handlerMap = make(map[string]Handler)
	for _, v := range cfg.Handlers {
		reg.handlerMap[v.Name] = Handler{handlerCfg: v}
		err := generateKeypairIfNeeded(v, persister)
		if err != nil {
			return nil, err
		}
	}
	return &reg, nil
}

func generateKeypairIfNeeded(v config.Handler, p *models.Persister) error {
	kp, err := models.CreateKeypair(v)
	if err != nil {
		return err
	}
	err = p.Store(kp)
	if err != nil {
		return err
	}
	return nil
}

func (r *Registry) Actor(name string) (map[string]interface{}, error) {
	handler := r.findByName(name)
	if handler == nil {
		return nil, nil
	}
	return views.RenderActor(handler.handlerCfg.Name, r.cfg.Domain)
}

func (r *Registry) OutboxCollection(name string) (map[string]interface{}, error) {
	handler := r.findByName(name)
	if handler == nil {
		return nil, nil
	}
	aso := models.NewOutboxItem(handler.handlerCfg)
	page, err := r.persister.Collect(aso)
	if err != nil {
		return nil, err
	}
	var outboxItems []models.OutboxItem
	for _, v := range page { //TODO pagination
		buf := bytes.NewBuffer(v)
		dec := gob.NewDecoder(buf)
		var outboxItem models.OutboxItem
		err = dec.Decode(&outboxItem)
		if err != nil {
			return nil, err
		}
		outboxItems = append(outboxItems, outboxItem)
	}
	return views.RenderOutboxCollection(handler.handlerCfg.Name, r.cfg.Domain, outboxItems)
}

func (r *Registry) Following(name string) (map[string]interface{}, error) {
	handler := r.findByName(name)
	if handler == nil {
		return nil, nil
	}
	profile, err := urls.UrlProfile(name, r.cfg.Domain)
	if err != nil {
		return nil, err
	}
	following, err := urls.UrlFollowing(name, r.cfg.Domain)
	if err != nil {
		return nil, err
	}
	result := make(map[string]interface{})
	result["@context"] = "https://www.w3.org/ns/activitystreams"
	result["attributedTo"] = profile.String()
	result["id"] = following.String()
	result["totalItems"] = 0
	result["orderedItems"] = []bool{}
	result["type"] = "OrderedCollection"
	return result, nil
}

func (r *Registry) Followers(name string) (map[string]interface{}, error) {
	handler := r.findByName(name)
	if handler == nil {
		return nil, nil
	}
	profile, err := urls.UrlProfile(name, r.cfg.Domain)
	if err != nil {
		return nil, err
	}
	followers, err := urls.UrlFollowers(name, r.cfg.Domain)
	if err != nil {
		return nil, err
	}
	result := make(map[string]interface{})
	result["@context"] = "https://www.w3.org/ns/activitystreams"
	result["attributedTo"] = profile.String()
	result["id"] = followers.String()
	result["totalItems"] = 0
	result["orderedItems"] = []bool{}
	result["type"] = "OrderedCollection"
	return result, nil
}

func (r *Registry) Inbox(name string, req *http.Request) error {
	handler := r.findByName(name)
	if handler == nil {
		return nil
	}
	// verifier, err := httpsig.NewVerifier(req)
	// if err != nil {
	// 	return err
	// }
	return nil
}

func (r *Registry) ActivityOrNote(activityOrNote, name, id string) (map[string]interface{}, error) {
	handler := r.findByName(name)
	if handler == nil {
		return nil, nil
	}
	lookup := models.NewOutboxItem(handler.handlerCfg)
	lookup.Id = []byte(id)
	result, err := r.persister.Find(lookup)
	if err != nil {
		return nil, err
	}
	if result == nil {
		return nil, nil
	}
	buf := bytes.NewBuffer(result)
	dec := gob.NewDecoder(buf)
	var outboxItem models.OutboxItem
	err = dec.Decode(&outboxItem)
	if err != nil {
		return nil, err
	}
	if activityOrNote == "activity" {
		return views.RenderActivity(handler.handlerCfg.Name, r.cfg.Domain, outboxItem)
	}
	return views.RenderNote(handler.handlerCfg.Name, r.cfg.Domain, outboxItem)
}

// This has to handle various lookup formats:
// ?resource=acct:actor@domain
// ?resource=actor@domain
// ?resource=actor
// ?resource=acct:actor
func (r *Registry) Webfinger(fqn string) (*views.WebfingerResource, error) {
	// Strip away acct: prefix, if found
	fqn = strings.TrimPrefix(fqn, "acct:")

	// Strip away @domain suffix, if found
	domainUrl, err := url.Parse(r.cfg.Domain)
	if err != nil {
		return nil, err
	}
	hostname := "@" + domainUrl.Hostname()
	fqn = strings.TrimSuffix(fqn, hostname)

	// We should just have $actor left
	handler := r.findByName(fqn)
	if handler == nil {
		return nil, nil
	}
	return views.RenderWebfinger(handler.handlerCfg.Name, r.cfg.Domain, hostname)
}

func (r *Registry) findByName(name string) *Handler {
	handler, ok := r.handlerMap[name]
	if !ok {
		return nil
	}
	return &handler
}