-
Notifications
You must be signed in to change notification settings - Fork 11
/
post.go
102 lines (88 loc) · 2.14 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
package readas
import (
"github.com/gorilla/mux"
"github.com/microcosm-cc/bluemonday"
"html/template"
"net/http"
"strconv"
"time"
)
type Post struct {
ID int64
OwnerID int64
ActivityID string
Type string
Published time.Time
URL string
Name string
Content string
actorID string
IsInFeed bool
Owner *User
}
func (p *Post) SanitaryContent() template.HTML {
// Strip out bad HTML
policy := getSanitizationPolicy()
policy.RequireNoFollowOnLinks(true)
return template.HTML(policy.Sanitize(p.Content))
}
func (p *Post) DisplayTitle() string {
t := "A post"
if p.Name != "" {
t = p.Name
}
return t + " by " + p.Owner.Name
}
func (p *Post) Summary() string {
// TODO: return truncated summary of post.
return ""
}
func (p *Post) PublishedDate() string {
return p.Published.Format("2006-01-02")
}
func (p *Post) Published8601() string {
return p.Published.Format("2006-01-02T15:04:05Z")
}
func getSanitizationPolicy() *bluemonday.Policy {
policy := bluemonday.UGCPolicy()
policy.AllowAttrs("src", "style").OnElements("iframe", "video")
policy.AllowAttrs("frameborder", "width", "height").Matching(bluemonday.Integer).OnElements("iframe")
policy.AllowAttrs("allowfullscreen").OnElements("iframe")
policy.AllowAttrs("controls", "loop", "muted", "autoplay").OnElements("video")
policy.AllowAttrs("target").OnElements("a")
policy.AllowAttrs("style", "class", "id").Globally()
policy.AllowURLSchemes("http", "https", "mailto", "xmpp")
return policy
}
func handleViewPost(app *app, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
idStr := vars["id"]
id, err := strconv.Atoi(idStr)
if err != nil {
return err
}
cu := getUserSession(app, r)
var u *LocalUser
if cu != nil {
u, err = app.getLocalUser(cu.PreferredUsername)
if err != nil {
return err
}
}
p := struct {
User *LocalUser
Version string
InstanceName string
Post *Post
}{
User: u,
Version: softwareVersion,
InstanceName: app.cfg.Name,
Post: &Post{},
}
p.Post, err = app.getPost(int64(id))
if err != nil {
return err
}
return renderTemplate(w, "post", p)
}