-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
262 lines (186 loc) · 6.3 KB
/
account.go
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package www
import (
"encoding/json"
"fmt"
"html/template"
"net/http"
"strings"
"time"
"github.com/sfomuseum/go-activitypub"
"github.com/sfomuseum/go-activitypub/ap"
"github.com/sfomuseum/go-activitypub/uris"
)
type AccountHandlerOptions struct {
AccountsDatabase activitypub.AccountsDatabase
AliasesDatabase activitypub.AliasesDatabase
PropertiesDatabase activitypub.PropertiesDatabase
URIs *uris.URIs
Templates *template.Template
RedirectOnAlias bool
}
type AccountTemplateVars struct {
Account *activitypub.Account
AccountURL string
// To do: URLs (properties)
IconURL string
PropertiesMap map[string]*activitypub.Property
URLProperties map[string]string
}
func AccountHandler(opts *AccountHandlerOptions) (http.Handler, error) {
account_t := opts.Templates.Lookup("account")
if account_t == nil {
return nil, fmt.Errorf("Failed to retrieve 'account' template")
}
fn := func(rsp http.ResponseWriter, req *http.Request) {
ctx := req.Context()
logger := LoggerWithRequest(req, nil)
t1 := time.Now()
defer func() {
logger.Info("Time to serve request", "ms", time.Since(t1).Milliseconds())
}()
switch req.Method {
case http.MethodGet, http.MethodPost:
// pass
default:
logger.Error("Method not allowed")
http.Error(rsp, "Method not allowed", http.StatusMethodNotAllowed)
return
}
account_name, host, err := activitypub.ParseAddressFromRequest(req)
if err != nil {
logger.Error("Failed to parse address from request", "error", err)
http.Error(rsp, "Bad request", http.StatusBadRequest)
return
}
logger = logger.With("account name", account_name)
if host != "" && host != opts.URIs.Hostname {
logger.Error("Resouce has bunk hostname", "host", host)
http.Error(rsp, "Not found", http.StatusNotFound)
return
}
acct, err := opts.AccountsDatabase.GetAccountWithName(ctx, account_name)
if err != nil && err != activitypub.ErrNotFound {
logger.Error("Failed to retrieve account", "error", err)
if err == activitypub.ErrNotFound {
http.Error(rsp, "Not found", http.StatusNotFound)
return
}
http.Error(rsp, "Internal server error", http.StatusInternalServerError)
return
}
// START OF lookup account by alias
if err != nil {
logger.Debug("Lookup resource by alias")
alias, err := opts.AliasesDatabase.GetAliasWithName(ctx, account_name)
if err != nil {
logger.Error("Failed to retrieve account for resource alias", "alias", account_name, "error", err)
if err == activitypub.ErrNotFound {
http.Error(rsp, "Not found", http.StatusNotFound)
return
}
http.Error(rsp, "Internal server error", http.StatusInternalServerError)
return
}
acct, err = opts.AccountsDatabase.GetAccountWithId(ctx, alias.AccountId)
if err != nil {
logger.Error("Failed to retrieve account with ID for resource alias", "alias", account_name, "account id", alias.AccountId, "error", err)
if err == activitypub.ErrNotFound {
http.Error(rsp, "Not found", http.StatusNotFound)
return
}
http.Error(rsp, "Internal server error", http.StatusInternalServerError)
return
}
// False by default - currently lacking hooks in the rest of the code
// for setting this manually (cli flag, etc)
if opts.RedirectOnAlias {
acct_u := acct.AccountURL(ctx, opts.URIs)
logger.Debug("Redirect to account page", "page", acct_u.String())
http.Redirect(rsp, req, acct_u.String(), http.StatusSeeOther)
return
}
}
logger = logger.With("account id", acct.Id)
props_map, err := activitypub.PropertiesMapForAccount(ctx, opts.PropertiesDatabase, acct)
if err != nil {
logger.Error("Failed to derive properties map for account", "error", err)
http.Error(rsp, "Internal server error", http.StatusInternalServerError)
return
}
// Check content-type here and HTML or JSON it up...
if IsActivityStreamRequest(req, "Accept") {
profile, err := acct.ProfileResource(ctx, opts.URIs)
if err != nil {
logger.Error("Failed to derive profile response for resource", "error", err)
http.Error(rsp, "Not acceptable", http.StatusNotAcceptable)
return
}
var attachments []*ap.Attachment
if profile.Attachments != nil {
attachments = profile.Attachments
}
for k, prop := range props_map {
if !strings.HasPrefix(k, "url:") {
continue
}
parts := strings.Split(k, ":")
label := parts[1]
href := prop.Value
url := href
// This is what Mastodon does so for the time being we'll do it too
url = strings.Replace(url, "https://", `<span class="invisible">https://</span>`, 1)
url = strings.Replace(url, "www.", `<span class="invisible">www.</span>`, 1)
link := fmt.Sprintf(`<a href="%s" target="_blank" rel="nofollow noopener noreferrer me" translate="no">%s</a>`, href, url)
a := &ap.Attachment{
Type: "PropertyValue",
Name: label,
Value: link,
}
attachments = append(attachments, a)
}
if len(attachments) > 0 {
profile.Attachments = attachments
}
// To do: append properties map (above)
rsp.Header().Set("Content-type", "application/activity+json")
enc := json.NewEncoder(rsp)
err = enc.Encode(profile)
if err != nil {
logger.Error("Failed to encode profile response for resource", "error", err)
http.Error(rsp, "Internal server error", http.StatusInternalServerError)
return
}
return
}
acct.PrivateKeyURI = "constant://?val="
account_url := acct.AccountURL(ctx, opts.URIs)
icon_path := uris.AssignResource(opts.URIs.Icon, acct.Name)
icon_url := uris.NewURL(opts.URIs, icon_path)
url_props := make(map[string]string)
for k, prop := range props_map {
if !strings.HasPrefix(k, "url:") {
continue
}
parts := strings.Split(k, ":")
label := parts[1]
href := prop.Value
url_props[label] = href
}
vars := AccountTemplateVars{
Account: acct,
IconURL: icon_url.String(),
AccountURL: account_url.String(),
PropertiesMap: props_map,
URLProperties: url_props,
}
rsp.Header().Set("Content-type", "text/html")
err = account_t.Execute(rsp, vars)
if err != nil {
logger.Error("Failed to render template", "error", err)
http.Error(rsp, "Internal server error", http.StatusInternalServerError)
return
}
return
}
return http.HandlerFunc(fn), nil
}