/
post.go
171 lines (123 loc) · 3.87 KB
/
post.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
package www
import (
"fmt"
"html/template"
"net/http"
"regexp"
"strconv"
"strings"
"time"
"github.com/sfomuseum/go-activitypub"
"github.com/sfomuseum/go-activitypub/uris"
)
type PostHandlerOptions struct {
AccountsDatabase activitypub.AccountsDatabase
PostsDatabase activitypub.PostsDatabase
URIs *uris.URIs
Templates *template.Template
}
type PostHandlerVars struct {
Post *activitypub.Post
PostBody template.HTML
Account *activitypub.Account
AccountURL string
PostURL string
IconURL string
}
func PostHandler(opts *PostHandlerOptions) (http.Handler, error) {
post_t := opts.Templates.Lookup("post")
if post_t == nil {
return nil, fmt.Errorf("Failed to lookup 'post' template")
}
post_pat := opts.URIs.Post
post_pat = strings.Replace(post_pat, "{resource}", "(?:[^\\/]+)", 1)
post_pat = strings.Replace(post_pat, "{id}", "(\\d+)", 1)
re_post, err := regexp.Compile(post_pat)
if err != nil {
return nil, fmt.Errorf("Failed to create post regular expression, %w", err)
}
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())
}()
if req.Method != http.MethodGet {
logger.Error("Method not allowed")
http.Error(rsp, "Method not allowed", http.StatusMethodNotAllowed)
return
}
// Basic sanity checking of post ID
if !re_post.MatchString(req.URL.Path) {
http.Error(rsp, "Not found", http.StatusNotFound)
return
}
m := re_post.FindStringSubmatch(req.URL.Path)
str_id := m[1]
post_id, err := strconv.ParseInt(str_id, 10, 64)
if err != nil {
http.Error(rsp, "Bad request", http.StatusBadRequest)
return
}
logger = logger.With("post id", post_id)
// Get account
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 {
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
}
logger = logger.With("account id", acct.Id)
// Get post
post, err := opts.PostsDatabase.GetPostWithId(ctx, post_id)
if err != nil {
logger.Error("Failed to retrieve post", "error", err)
http.Error(rsp, "Internal server error", http.StatusInternalServerError)
return
}
if post.AccountId != acct.Id {
logger.Error("Post is owned by different account", "post account id", post.AccountId)
http.Error(rsp, "Not found", http.StatusNotFound)
return
}
acct.PrivateKeyURI = "constant://?val="
account_url := acct.AccountURL(ctx, opts.URIs)
post_url := acct.PostURL(ctx, opts.URIs, post)
icon_path := uris.AssignResource(opts.URIs.Icon, acct.Name)
icon_url := uris.NewURL(opts.URIs, icon_path)
// Render template
vars := PostHandlerVars{
Account: acct,
Post: post,
PostBody: template.HTML(post.Body),
IconURL: icon_url.String(),
AccountURL: account_url.String(),
PostURL: post_url.String(),
}
rsp.Header().Set("Content-Type", "text/html")
err = post_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 http.HandlerFunc(fn), nil
}