forked from tanitall/memo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
all.go
73 lines (70 loc) · 2.04 KB
/
all.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
package profile
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/profile"
"github.com/memocash/memo/app/res"
"net/http"
"strings"
)
var allRoute = web.Route{
Pattern: res.UrlProfiles,
Handler: func(r *web.Response) {
profilesByDate(r, true)
r.RenderTemplate(res.TmplProfiles)
},
}
func profilesByDate(r *web.Response, oldestToNewest bool) {
r.Helper["Nav"] = "profiles"
r.Helper["Title"] = "Memo - Profiles"
offset := r.Request.GetUrlParameterInt("offset")
searchString := html_parser.EscapeWithEmojis(r.Request.GetUrlParameter("s"))
var selfPkHash []byte
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
}
selfPkHash = key.PkHash
}
var statOrderType db.UserStatOrderType
if oldestToNewest {
statOrderType = db.UserStatOrderCreated
} else {
statOrderType = db.UserStatOrderNewest
}
profiles, err := profile.GetProfiles(selfPkHash, searchString, offset, statOrderType)
if err != nil {
r.Error(jerr.Get("error getting profiles", err), http.StatusInternalServerError)
return
}
err = profile.AttachReputationToProfiles(profiles)
if err != nil {
r.Error(jerr.Get("error attaching reputation to profiles", err), http.StatusInternalServerError)
return
}
res.SetPageAndOffset(r, offset)
r.Helper["SearchString"] = searchString
var url string
if oldestToNewest {
url = res.UrlProfiles
} else {
url = res.UrlProfilesNew
}
if searchString != "" {
r.Helper["OffsetLink"] = fmt.Sprintf("%s?s=%s", strings.TrimLeft(url, "/"), searchString)
} else {
r.Helper["OffsetLink"] = fmt.Sprintf("%s?", url)
}
r.Helper["Profiles"] = profiles
}