forked from tanitall/memo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
new.go
85 lines (83 loc) · 2.56 KB
/
new.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
package posts
import (
"fmt"
"github.com/jchavannes/jgo/jerr"
"github.com/jchavannes/jgo/web"
"github.com/memocash/memo/app/auth"
"github.com/memocash/memo/app/db"
"github.com/memocash/memo/app/html-parser"
"github.com/memocash/memo/app/metric"
"github.com/memocash/memo/app/profile"
"github.com/memocash/memo/app/res"
"net/http"
"strings"
)
var newRoute = web.Route{
Pattern: res.UrlPostsNew,
Handler: func(r *web.Response) {
preHandler(r)
offset := r.Request.GetUrlParameterInt("offset")
searchString := html_parser.EscapeWithEmojis(r.Request.GetUrlParameter("s"))
var userPkHash []byte
var userId uint
if auth.IsLoggedIn(r.Session.CookieId) {
user, err := auth.GetSessionUser(r.Session.CookieId)
if err != nil {
r.Error(jerr.Get("error getting session user", err), http.StatusInternalServerError)
return
}
key, err := db.GetKeyForUser(user.Id)
if err != nil {
r.Error(jerr.Get("error getting key for user", err), http.StatusInternalServerError)
return
}
userPkHash = key.PkHash
userId = user.Id
}
posts, err := profile.GetRecentPosts(userPkHash, uint(offset), searchString)
if err != nil {
r.Error(jerr.Get("error getting recent posts", err), http.StatusInternalServerError)
return
}
err = profile.AttachParentToPosts(posts)
if err != nil {
r.Error(jerr.Get("error attaching parent to posts", err), http.StatusInternalServerError)
return
}
err = profile.AttachLikesToPosts(posts)
if err != nil {
r.Error(jerr.Get("error attaching likes to posts", err), http.StatusInternalServerError)
return
}
err = profile.AttachPollsToPosts(posts)
if err != nil {
r.Error(jerr.Get("error attaching polls to posts", err), http.StatusInternalServerError)
return
}
if len(userPkHash) > 0 {
err = profile.AttachReputationToPosts(posts)
if err != nil {
r.Error(jerr.Get("error attaching reputation to posts", err), http.StatusInternalServerError)
return
}
}
err = profile.SetShowMediaForPosts(posts, userId)
if err != nil {
r.Error(jerr.Get("error setting show media for posts", err), http.StatusInternalServerError)
return
}
res.SetPageAndOffset(r, offset)
r.Helper["Posts"] = posts
r.Helper["Title"] = "Memo - New Posts"
r.Helper["SearchString"] = searchString
if searchString != "" {
r.Helper["OffsetLink"] = fmt.Sprintf("%s?s=%s", strings.TrimLeft(res.UrlPostsNew, "/"), searchString)
go func () {
metric.AddMemoPostSearch(searchString, res.UrlPostsNew)
}()
} else {
r.Helper["OffsetLink"] = fmt.Sprintf("%s?", res.UrlPostsNew)
}
r.Render()
},
}