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

import (
	"fmt"

	"git.capotej.com/capotej/communique/config"
	"git.capotej.com/capotej/communique/resources"
	"github.com/dgraph-io/badger/v3"
)

type Handler struct {
	handlerCfg config.Handler
}

type Registry struct {
	cfg        config.Config
	db         *badger.DB
	handlerMap map[string]Handler
}

func NewRegistry(cfg config.Config, db *badger.DB) *Registry {
	reg := Registry{cfg: cfg, db: db}
	reg.handlerMap = make(map[string]Handler)
	for _, v := range cfg.Handlers {
		reg.handlerMap[fqn(v.Name, cfg.Domain)] = Handler{handlerCfg: v}
	}
	return &reg
}

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

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

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

func (r *Registry) WebfingerResource(fqn string) (*resources.WebfingerResource, error) {
	handler := r.findByFQN(fqn)
	if handler == nil {
		handler = r.findByFQN("acct:" + fqn)
	}
	if handler == nil {
		return nil, nil
	}
	return resources.RenderWebfingerResource(handler.handlerCfg.Name, r.cfg.Domain)
}

func fqn(name, domain string) string {
	return fmt.Sprintf("acct:%s@%s", name, domain)
}

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

func (r *Registry) findByName(name string) *Handler {
	return r.findByFQN(fqn(name, r.cfg.Domain))
}