Repository navigation
Expand file tree
/
Copy pathnodeinfo.go
More file actions
228 lines (203 loc) · 5.5 KB
/
Copy pathnodeinfo.go
File metadata and controls
228 lines (203 loc) · 5.5 KB
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
package webfinger
import (
"net/http"
"path"
"regexp"
"git.sr.ht/~mariusor/storage-all"
vocab "github.com/go-ap/activitypub"
"github.com/go-ap/auth"
"github.com/go-ap/errors"
"github.com/go-ap/filters"
"github.com/writeas/go-nodeinfo"
)
type NodeInfoResolver struct {
users int
comments int
posts int
}
var (
ValidActorTypes = vocab.ActivityVocabularyTypes{
vocab.PersonType,
vocab.ServiceType,
vocab.GroupType,
vocab.ApplicationType,
vocab.OrganizationType,
}
ValidContentTypes = vocab.ActivityVocabularyTypes{
vocab.ArticleType,
vocab.NoteType,
vocab.LinkType,
vocab.PageType,
vocab.DocumentType,
vocab.VideoType,
vocab.AudioType,
}
actorsFilter = filters.Object(filters.HasType(ValidActorTypes...))
postsFilter = filters.Object(filters.NilInReplyTo, filters.HasType(ValidContentTypes...))
allFilter = filters.Object(filters.HasType(ValidContentTypes...))
)
func NodeInfoResolverNew(r storage.ReadStore, app vocab.Actor) NodeInfoResolver {
n := NodeInfoResolver{}
if r == nil {
return n
}
inboxOf := vocab.Inbox.Of(app)
if vocab.IsNil(inboxOf) {
return n
}
ff := filters.Checks{
filters.HasType(vocab.CreateType),
filters.Object(filters.IDLike(string(app.ID))),
}
col, err := r.Load(inboxOf.GetLink(), ff...)
if err != nil {
return n
}
var allItems vocab.ItemCollection
_ = vocab.OnCollectionIntf(col, func(col vocab.CollectionInterface) error {
allItems = col.Collection()
return nil
})
_ = vocab.OnCollectionIntf(filters.Checks{actorsFilter}.Run(allItems), func(col vocab.CollectionInterface) error {
n.users = len(col.Collection())
return nil
})
_ = vocab.OnCollectionIntf(filters.Checks{postsFilter}.Run(allItems), func(col vocab.CollectionInterface) error {
n.posts = len(col.Collection())
return nil
})
_ = vocab.OnCollectionIntf(filters.Checks{allFilter}.Run(allItems), func(col vocab.CollectionInterface) error {
n.comments = len(col.Collection())
return nil
})
n.comments -= n.posts
return n
}
func (n NodeInfoResolver) IsOpenRegistration() (bool, error) {
// TODO(marius)
return true, nil
}
func (n NodeInfoResolver) Usage() (nodeinfo.Usage, error) {
u := nodeinfo.Usage{
Users: nodeinfo.UsageUsers{
Total: n.users,
},
LocalComments: n.comments,
LocalPosts: n.posts,
}
return u, nil
}
const (
softwareName = "FedBOX"
sourceURL = "https://git.sr.ht/~mariusor/fedbox"
)
var Version = "HEAD"
func NodeInfoConfig(app vocab.Actor, ni WebInfo) nodeinfo.Config {
var baseURL string
if !vocab.IsNil(app.URL) {
baseURL = string(app.URL.GetLink())
}
var attributedToURL string
if !vocab.IsNil(app.AttributedTo) {
attributedToURL = string(app.AttributedTo.GetLink())
}
return nodeinfo.Config{
BaseURL: baseURL,
InfoURL: "/nodeinfo",
Metadata: nodeinfo.Metadata{
NodeName: string(regexp.MustCompile(`<[/\w]+>`).ReplaceAll([]byte(ni.Title), []byte{})),
NodeDescription: ni.Summary,
Private: false,
Software: nodeinfo.SoftwareMeta{
GitHub: sourceURL,
HomePage: baseURL,
Follow: attributedToURL,
},
},
Protocols: []nodeinfo.NodeProtocol{
nodeinfo.ProtocolActivityPub,
},
Services: nodeinfo.Services{
Inbound: []nodeinfo.NodeService{},
Outbound: []nodeinfo.NodeService{},
},
Software: nodeinfo.SoftwareInfo{
Name: path.Base(softwareName),
Version: Version,
},
}
}
type WebInfo struct {
Title string `json:"title"`
Email string `json:"email"`
Summary string `json:"summary"`
Description string `json:"description"`
Thumbnail string `json:"thumbnail,omitempty"`
Languages []string `json:"languages"`
URI string `json:"uri"`
Urls []string `json:"urls,omitempty"`
Version string `json:"version"`
}
func IconOf(it vocab.Item) string {
var iconURL string
if vocab.IsObject(it) {
_ = vocab.OnObject(it, func(ob *vocab.Object) error {
if ob.Icon != nil {
iconURL = string(ob.Icon.GetLink())
}
return nil
})
}
return iconURL
}
func (h *handler) setupNodeInfo(r *http.Request) (*nodeinfo.Service, error) {
storage, err := h.findMatchingStorage(baseURL(r)...)
if err != nil {
return nil, err
}
var app vocab.Actor
maybeActor := storage.Root
if !vocab.IsNil(maybeActor) {
app = maybeActor
}
if app.ID == "" || auth.AnonymousActor.Equals(app) {
return nil, errors.NotFoundf("root actor not found")
}
name := vocab.NameOf(app)
if name == "" {
name = vocab.PreferredNameOf(app)
}
cfg := NodeInfoConfig(app, WebInfo{
Title: name,
Email: "",
Summary: vocab.SummaryOf(app),
Description: vocab.ContentOf(app),
Thumbnail: IconOf(app),
URI: string(app.ID),
Urls: nil,
Version: Version,
})
return nodeinfo.NewService(cfg, NodeInfoResolverNew(aggRepo(storage), app)), nil
}
const NodeInfoDiscoverPath = "/.well-known/nodeinfo"
// NodeInfoDiscover handles "/.well-known/nodeinfo"
func (h handler) NodeInfoDiscover(w http.ResponseWriter, r *http.Request) {
ni, err := h.setupNodeInfo(r)
if err != nil {
handleErr(h.l)(r, err).ServeHTTP(w, r)
return
}
ni.NodeInfoDiscover(w, r)
h.l.Debugf("%s %s%s %d %s", r.Method, r.Host, r.RequestURI, http.StatusOK, http.StatusText(http.StatusOK))
}
const NodeInfoPath = "/nodeinfo"
// NodeInfo handles "/nodeinfo"
func (h handler) NodeInfo(w http.ResponseWriter, r *http.Request) {
ni, err := h.setupNodeInfo(r)
if err != nil {
handleErr(h.l)(r, err).ServeHTTP(w, r)
return
}
ni.NodeInfo(w, r)
h.l.Debugf("%s %s%s %d %s", r.Method, r.Host, r.RequestURI, http.StatusOK, http.StatusText(http.StatusOK))
}