forked from tanitall/memo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unfollow.go
116 lines (107 loc) · 3.42 KB
/
unfollow.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
110
111
112
113
114
115
116
package memo
import (
"bytes"
"github.com/jchavannes/jgo/jerr"
"github.com/jchavannes/jgo/web"
"github.com/memocash/memo/app/auth"
"github.com/memocash/memo/app/bitcoin/transaction"
"github.com/memocash/memo/app/bitcoin/transaction/build"
"github.com/memocash/memo/app/bitcoin/wallet"
"github.com/memocash/memo/app/db"
"github.com/memocash/memo/app/mutex"
"github.com/memocash/memo/app/profile"
"github.com/memocash/memo/app/res"
"net/http"
)
var unfollowRoute = web.Route{
Pattern: res.UrlMemoUnfollow + "/" + urlAddress.UrlPart(),
NeedsLogin: true,
Handler: func(r *web.Response) {
addressString := r.Request.GetUrlNamedQueryVariable(urlAddress.Id)
address := wallet.GetAddressFromString(addressString)
pkHash := address.GetScriptAddress()
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
}
if bytes.Equal(key.PkHash, pkHash) {
r.SetRedirect(res.GetUrlWithBaseUrl(res.UrlIndex, r))
return
}
hasSpendableTxOut, err := db.HasSpendable(key.PkHash)
if err != nil {
r.Error(jerr.Get("error getting spendable tx out", err), http.StatusInternalServerError)
return
}
if ! hasSpendableTxOut {
r.SetRedirect(res.UrlNeedFunds)
return
}
pf, err := profile.GetProfile(pkHash, key.PkHash)
if err != nil {
r.Error(jerr.Get("error getting profile for hash", err), http.StatusInternalServerError)
return
}
canFollow, err := profile.CanFollow(pkHash, key.PkHash)
if err != nil {
r.Error(jerr.Get("error getting can follow", err), http.StatusInternalServerError)
return
}
if canFollow {
r.Error(jerr.New("unable to unfollow user"), http.StatusUnprocessableEntity)
return
}
r.Helper["Profile"] = pf
r.RenderTemplate(res.UrlMemoUnfollow)
},
}
var unfollowSubmitRoute = web.Route{
Pattern: res.UrlMemoUnfollowSubmit,
NeedsLogin: true,
CsrfProtect: true,
Handler: func(r *web.Response) {
addressString := r.Request.GetFormValue("address")
followAddress := wallet.GetAddressFromString(addressString)
if followAddress.GetEncoded() != addressString {
r.Error(jerr.New("error parsing address"), http.StatusUnprocessableEntity)
return
}
password := r.Request.GetFormValue("password")
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
}
privateKey, err := key.GetPrivateKey(password)
if err != nil {
r.Error(jerr.Get("error getting private key", err), http.StatusUnauthorized)
return
}
pkHash := privateKey.GetPublicKey().GetAddress().GetScriptAddress()
mutex.Lock(pkHash)
tx, err := build.UnfollowUser(followAddress.GetScriptAddress(), privateKey)
if err != nil {
var statusCode = http.StatusInternalServerError
if build.IsNotEnoughValueError(err) {
statusCode = http.StatusPaymentRequired
}
mutex.Unlock(pkHash)
r.Error(jerr.Get("error building unfollow tx", err), statusCode)
return
}
transaction.GetTxInfo(tx).Print()
transaction.QueueTx(tx)
r.Write(tx.MsgTx.TxHash().String())
},
}