forked from tanitall/memo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
follow.go
109 lines (103 loc) · 3.54 KB
/
follow.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
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/bitcoin/wallet"
"github.com/memocash/memo/app/db"
"github.com/memocash/memo/app/profile"
"github.com/memocash/memo/app/res"
"net/http"
"strings"
)
var followersRoute = web.Route{
Pattern: res.UrlProfileFollowers + "/" + urlAddress.UrlPart(),
Handler: func(r *web.Response) {
offset := r.Request.GetUrlParameterInt("offset")
addressString := r.Request.GetUrlNamedQueryVariable(urlAddress.Id)
address := wallet.GetAddressFromString(addressString)
pkHash := address.GetScriptAddress()
var userPkHash []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
}
userPkHash = key.PkHash
}
pf, err := profile.GetProfile(pkHash, userPkHash)
if err != nil {
r.Error(jerr.Get("error getting profile for hash", err), http.StatusInternalServerError)
return
}
r.Helper["Profile"] = pf
followers, err := profile.GetFollowers(userPkHash, pkHash, offset)
if err != nil {
r.Error(jerr.Get("error setting followers for hash", err), http.StatusInternalServerError)
return
}
if len(userPkHash) > 0 {
err = profile.AttachReputationToFollowers(followers)
if err != nil {
r.Error(jerr.Get("error attaching reputation to followers", err), http.StatusInternalServerError)
return
}
}
r.Helper["Followers"] = followers
r.Helper["OffsetLink"] = fmt.Sprintf("%s/%s", strings.TrimLeft(res.UrlProfileFollowers, "/"), address.GetEncoded())
res.SetPageAndOffset(r, offset)
r.RenderTemplate(res.UrlProfileFollowers)
},
}
var followingRoute = web.Route{
Pattern: res.UrlProfileFollowing + "/" + urlAddress.UrlPart(),
Handler: func(r *web.Response) {
offset := r.Request.GetUrlParameterInt("offset")
addressString := r.Request.GetUrlNamedQueryVariable(urlAddress.Id)
address := wallet.GetAddressFromString(addressString)
pkHash := address.GetScriptAddress()
var userPkHash []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
}
userPkHash = key.PkHash
}
pf, err := profile.GetProfile(pkHash, userPkHash)
if err != nil {
r.Error(jerr.Get("error getting profile for hash", err), http.StatusInternalServerError)
return
}
r.Helper["Profile"] = pf
following, err := profile.GetFollowing(userPkHash, pkHash, offset)
if err != nil {
r.Error(jerr.Get("error setting following for hash", err), http.StatusInternalServerError)
return
}
if len(userPkHash) > 0 {
err = profile.AttachReputationToFollowers(following)
if err != nil {
r.Error(jerr.Get("error attaching reputation to following", err), http.StatusInternalServerError)
return
}
}
r.Helper["Following"] = following
r.Helper["OffsetLink"] = fmt.Sprintf("%s/%s", strings.TrimLeft(res.UrlProfileFollowing, "/"), address.GetEncoded())
res.SetPageAndOffset(r, offset)
r.RenderTemplate(res.UrlProfileFollowing)
},
}