-
Notifications
You must be signed in to change notification settings - Fork 0
/
postmanager.go
233 lines (194 loc) · 5.85 KB
/
postmanager.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
package internal
import (
"bytes"
"encoding/gob"
"errors"
"fmt"
"github.com/bluele/gcache"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/html"
"github.com/gomarkdown/markdown/parser"
"github.com/labstack/gommon/log"
"github.com/microcosm-cc/bluemonday"
"hash/fnv"
"html/template"
"os"
"time"
"github.com/google/uuid"
"github.com/jtanza/post-pigeon/internal/model"
)
type PostManager struct {
db DB
cache gcache.Cache
namespace string
markdownExtensions parser.Extensions
}
func NewPostManager(db DB, cache gcache.Cache) PostManager {
namespace := os.Getenv("POST_PIGEON_NS")
if len(namespace) == 0 {
log.Fatal("unset namespace for app")
}
extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock | parser.Footnotes
return PostManager{db, cache, namespace, extensions}
}
func (pm PostManager) CreatePost(request model.PostRequest) (string, error) {
if err := ValidateSignature(request.PublicKey, request.Signature, request.Body); err != nil {
return "", errors.New("could not validate signature")
}
m, err := pm.formatRequestData(request)
if err != nil {
return "", err
}
renderedHTML, err := toHTML("post", m)
if err != nil {
return "", err
}
postUUID, err := GenerateDeterministicUUID(request.PublicKey, request.Title, pm.namespace)
if err != nil {
return "", err
}
if err = pm.db.PersistPost(postUUID, request, renderedHTML, ParseExpiration(request.Expiration)); err != nil {
return "", err
}
return postUUID, err
}
func (pm PostManager) IsDuplicate(request model.PostRequest) (bool, error) {
postUUID, err := GenerateDeterministicUUID(request.PublicKey, request.Title, pm.namespace)
if err != nil {
return false, err
}
post, err := pm.db.GetPost(postUUID)
if err != nil {
return false, err
}
return post != nil, nil
}
func (pm PostManager) HasPosts(fingerprint string) (bool, error) {
posts, err := pm.db.GetUserPosts(fingerprint)
if err != nil {
return false, err
}
return len(posts) > 0, nil
}
// RemovePost will use the stored public key and post message to delete a post from the provided request
// iff the signatures can be verified using the stored key/message.
func (pm PostManager) RemovePost(request model.PostDeleteRequest) error {
post, err := pm.db.GetPost(request.UUID)
if err != nil {
return err
}
if post == nil {
// dont leak proof of a non-existent post
return errors.New("could not verify signature")
}
content, err := pm.db.GetPostContent(post.UUID)
if err != nil {
return err
}
// https://crypto.stackexchange.com/q/111536/116199
if err = ValidateSignature(post.Key, request.Signature, content.Message); err != nil {
return errors.New("could not validate signature")
}
pm.cache.Remove(post.UUID)
return pm.db.DeletePost(request)
}
func (pm PostManager) FetchPostContent(postUUID string) (*model.PostContent, error) {
if pm.cache.Has(postUUID) {
post, err := pm.cache.Get(postUUID)
if err != nil {
log.Error(err)
} else {
log.Infof("serving post %s from cache. hit rate: %f", postUUID, pm.cache.HitRate())
return post.(*model.PostContent), nil
}
}
post, err := pm.db.GetPostContent(postUUID)
if err != nil {
return nil, err
}
if err = pm.cache.Set(postUUID, post); err != nil {
log.Error(err)
}
return post, nil
}
func (pm PostManager) GetAllUserPosts(fingerprint string) (string, error) {
posts, err := pm.db.GetUserPosts(fingerprint)
if err != nil {
return "", err
}
data := make([]map[string]interface{}, 0)
for _, p := range posts {
m := map[string]interface{}{
"Fingerprint": fingerprint,
"UUID": p.UUID,
"Title": p.Title,
"Date": p.CreatedAt.Format(time.DateOnly),
}
data = append(data, m)
}
return toHTML("posts", data)
}
// GenerateDeterministicUUID creates a deterministic (version 5) uuid from the provided key and title
func GenerateDeterministicUUID(key, title, namespace string) (string, error) {
if len(key) == 0 || len(title) == 0 {
return "", errors.New("invalid title or key")
}
buf := &bytes.Buffer{}
if err := gob.NewEncoder(buf).Encode([]string{key, title}); err != nil {
return "", err
}
h := fnv.New64()
if _, err := h.Write(buf.Bytes()); err != nil {
return "", err
}
id, err := uuid.FromBytes([]byte(namespace)[:16])
if err != nil {
return "", err
}
return uuid.NewSHA1(id, h.Sum(nil)).String(), nil
}
// ParseExpiration converts common expiration times used on our frontend into actual time periods
// our post reaper will leverage to expire posts.
func ParseExpiration(expirationRequest string) *time.Time {
expiration := time.Now().UTC()
switch expirationRequest {
case "1 hour":
expiration = expiration.Add(time.Hour)
case "1 day":
expiration = expiration.AddDate(0, 0, 1)
case "1 month":
expiration = expiration.AddDate(0, 1, 0)
case "1 year":
expiration = expiration.AddDate(1, 0, 0)
default:
return nil
}
return &expiration
}
func (pm PostManager) formatRequestData(request model.PostRequest) (map[string]any, error) {
fingerprint, err := Fingerprint(request.PublicKey)
if err != nil {
return nil, err
}
md := parser.NewWithExtensions(pm.markdownExtensions).Parse([]byte(request.Body))
renderer := html.NewRenderer(html.RendererOptions{Flags: html.CommonFlags | html.HrefTargetBlank})
sanitizedBody := bluemonday.UGCPolicy().SanitizeBytes(markdown.Render(md, renderer))
m := map[string]interface{}{
"Title": request.Title,
"Body": template.HTML(sanitizedBody),
"Fingerprint": fingerprint,
"CreationDate": time.Now().Format(time.DateOnly),
}
return m, nil
}
func toHTML(templateName string, data any) (string, error) {
t, err := template.New(templateName).ParseFiles(fmt.Sprintf("templates/%s", templateName))
if err != nil {
return "", err
}
var buf bytes.Buffer
if err = t.Execute(&buf, data); err != nil {
return "", err
}
return buf.String(), nil
}