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

import (
	"fmt"
	"path"

	"git.capotej.com/capotej/communique/config"
	"git.capotej.com/capotej/communique/resources"
)

type Handler struct {
	handlerCfg config.Handler
}

type Registry struct {
	cfg        config.Config
	handlerMap map[string]Handler
}

func NewRegistry(cfg config.Config) *Registry {
	reg := Registry{cfg: cfg}
	reg.handlerMap = make(map[string]Handler)
	for _, v := range cfg.Handlers {
		// TODO clean up
		fqdn := fmt.Sprintf("acct:%s@%s", v.Name, cfg.Domain)
		reg.handlerMap[fqdn] = Handler{handlerCfg: v}
	}
	return &reg
}

// TODO should probably be getprofilebyname
func (r *Registry) LookupByName(name string) *resources.Profile {
	fqdn := fmt.Sprintf("acct:%s@%s", name, r.cfg.Domain)
	if r.LookupResource(fqdn) == nil {
		return nil
	}
	return resources.RenderProfile(name, r.cfg.Domain)
}

// TODO extract an existsbyname existsbyfqdn from this
func (r *Registry) LookupResource(fqdn string) *resources.WebfingerResource {
	handler, ok := r.handlerMap[fqdn]
	if !ok {
		return nil
	}
	rs := resources.WebfingerResource{
		// TODO clean up
		Subject: fmt.Sprintf("acct:%s@%s", handler.handlerCfg.Name, r.cfg.Domain),
		Aliases: []string{},
		Links: []resources.Link{{
			Rel:  "self",
			Href: path.Join("https://", r.cfg.Domain, "actors", handler.handlerCfg.Name),
			Type: "application/activity+json",
		}},
	}
	return &rs

}