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
|
package main
import (
"encoding/json"
"html/template"
"net/http"
"slices"
"strings"
"time"
)
type Post struct {
ID string
Created time.Time
Modified time.Time
Content string
ContentHTML template.HTML
Link string
Actor *Actor
ReplyTo string
Sensitive bool
Likes int
Shares int
Replies int
LocalID string
}
func ComparePost(a *Post, b *Post) int {
return a.Created.Compare(b.Created)
}
func FromNote(id string, user *User, actor string, note *NoteMeta) *Post {
post := &Post{}
post.Created, _ = time.Parse("2006-01-02T15:04:05Z", note.Published)
post.ContentHTML = template.HTML(user.config.UGCPolicy.Sanitize(note.Content))
if actor, err := user.fetchActorInfo(actor, true); err != nil {
post.Actor = &Actor{
ID: note.actor,
}
} else {
post.Actor = actor
}
post.ID = id
post.LocalID = note.LocalID()
post.Link = note.ID
post.Sensitive = note.Sensitive
// also mark as sensitive if the To field isn't public...
if !slices.Contains(note.To, "https://www.w3.org/ns/activitystreams#Public") {
post.Sensitive = true
}
// used for xml encoding...
post.Content = note.Content
post.Content = strings.ReplaceAll(post.Content, "&", "&")
post.Content = strings.ReplaceAll(post.Content, "<", "<")
post.Content = strings.ReplaceAll(post.Content, ">", ">")
post.Likes = CacheCount(user, "likes", note.LocalID())
post.Shares = CacheCount(user, "shares", note.LocalID())
return post
}
func (user *User) processPostJson(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
id = strings.TrimSuffix(id, ".json")
if data, err := FromCache(user, "notes", "", user.ID+"/posts/"+id); err == nil {
note := &NoteMeta{}
json.Unmarshal(data, note)
note.Replies = CollectionMeta{ID: user.ID + "/posts/" + id + "/replies", Type: "Collection", TotalItems: CacheCount(user, "replies", id)}
note.Likes = CollectionMeta{ID: user.ID + "/posts/" + id + "/likes", Type: "Collection", TotalItems: CacheCount(user, "likes", id)}
note.Shares = CollectionMeta{ID: user.ID + "/posts/" + id + "/replies", Type: "Collection", TotalItems: CacheCount(user, "shares", id)}
item := NoteMetaWithContext{}
item.Context = []ContextElement{
{Simple: StringOrArray{"https://www.w3.org/ns/activitystreams"}},
{Complex: map[string]string{"ostatus": "http://ostatus.org#", "atomUri": "ostatus:atomUri", "inReplyToAtomUri": "ostatus:inReplyToAtomUri", "conversation": "ostatus:conversation", "sensitive": "as:sensitive", "toot": "http://joinmastodon.org/ns#", "votersCount": "toot:votersCount"}},
}
item.NoteMeta = *note
w.Header().Add("Content-Type", "application/activity+json")
data, _ := json.Marshal(item)
w.Write(data)
} else {
http.NotFound(w, r)
}
}
func (user *User) processFollowers(w http.ResponseWriter, r *http.Request) {
collection := CollectionMetaWithContext{}
collection.Context = "https://www.w3.org/ns/activitystreams"
collection.ID = user.ID + "/followers"
collection.Type = "Collection"
collection.TotalItems = CacheCount(user, "followers", "")
w.Header().Add("Content-Type", "application/activity+json")
data, _ := json.Marshal(collection)
w.Write(data)
}
func (user *User) processCollection(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
id = strings.TrimSuffix(id, ".json")
if data, err := FromCache(user, "notes", "", user.ID+"/posts/"+id); err == nil {
note := &NoteMeta{}
json.Unmarshal(data, note)
collectionName := r.PathValue("collection")
collection := CollectionMetaWithContext{}
collection.Context = "https://www.w3.org/ns/activitystreams"
collection.ID = user.ID + "/posts/" + id + "/" + collectionName
collection.Type = "Collection"
collection.TotalItems = CacheCount(user, collectionName, id)
w.Header().Add("Content-Type", "application/activity+json")
data, _ := json.Marshal(collection)
w.Write(data)
} else {
http.NotFound(w, r)
}
}
func (user *User) processPost(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if !strings.HasSuffix(id, ".json") && strings.Contains(r.Header.Get("accept"), "text/html") {
if data, err := FromCache(user, "notes", "", user.ID+"/posts/"+id); err == nil {
note := &NoteMeta{}
json.Unmarshal(data, note)
fpost := FromNote(CacheId(user, "notes", "", user.ID+"/posts/"+id), user, user.ID, note)
posts := []*Post{}
isAdmin, _ := user.config.IsAdmin(r)
WalkCache(user, "replies", id, func(id string, b []byte) {
postmeta := &PostMeta{}
if err := json.Unmarshal(b, postmeta); err == nil {
post := FromNote(id, user, postmeta.Actor, &postmeta.Object)
if isAdmin {
post.Sensitive = false
}
posts = append(posts, post)
}
})
slices.SortFunc(posts, ComparePost)
page := user.config.DefaultPage(r)
page.User = user
page.Posts = posts
page.IsAdmin = isAdmin
page.FeaturedPost = fpost
user.config.Templates.ExecuteTemplate(w, "mentions.tpl.html", page)
return
} else {
http.NotFound(w, r)
return
}
}
user.processPostJson(w, r)
}
|