forked from tanitall/memo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reply.go
133 lines (126 loc) · 4.08 KB
/
reply.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package memo
import (
"github.com/jchavannes/btcd/chaincfg/chainhash"
"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/db"
"github.com/memocash/memo/app/mutex"
"github.com/memocash/memo/app/profile"
"github.com/memocash/memo/app/res"
"net/http"
)
var replyRoute = web.Route{
Pattern: res.UrlMemoReply + "/" + urlTxHash.UrlPart(),
NeedsLogin: true,
Handler: func(r *web.Response) {
txHashString := r.Request.GetUrlNamedQueryVariable(urlTxHash.Id)
txHash, err := chainhash.NewHashFromStr(txHashString)
if err != nil {
r.Error(jerr.Get("error getting transaction hash", err), http.StatusInternalServerError)
return
}
var pkHash []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
}
pkHash = key.PkHash
}
post, err := profile.GetPostByTxHashWithReplies(txHash.CloneBytes(), pkHash, 0)
if err != nil {
r.Error(jerr.Get("error getting post", err), http.StatusInternalServerError)
return
}
err = profile.AttachParentToPosts([]*profile.Post{post})
if err != nil {
r.Error(jerr.Get("error attaching parent to post", err), http.StatusInternalServerError)
return
}
err = profile.AttachLikesToPosts([]*profile.Post{post})
if err != nil {
r.Error(jerr.Get("error attaching likes to posts", err), http.StatusInternalServerError)
return
}
err = profile.AttachPollsToPosts([]*profile.Post{post})
if err != nil {
r.Error(jerr.Get("error attaching likes to posts", err), http.StatusInternalServerError)
return
}
r.Helper["Post"] = post
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
}
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
}
r.RenderTemplate(res.UrlMemoReply)
},
}
var replySubmitRoute = web.Route{
Pattern: res.UrlMemoReplySubmit,
NeedsLogin: true,
CsrfProtect: true,
Handler: func(r *web.Response) {
txHashString := r.Request.GetFormValue("txHash")
txHash, err := chainhash.NewHashFromStr(txHashString)
if err != nil {
r.Error(jerr.Get("error getting transaction hash", err), http.StatusInternalServerError)
return
}
message := r.Request.GetFormValue("message")
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.MemoReply(txHash.CloneBytes(), message, privateKey)
if err != nil {
var statusCode = http.StatusInternalServerError
if build.IsNotEnoughValueError(err) {
statusCode = http.StatusPaymentRequired
}
mutex.Unlock(pkHash)
r.Error(jerr.Get("error building reply tx", err), statusCode)
return
}
transaction.GetTxInfo(tx).Print()
transaction.QueueTx(tx)
r.Write(tx.MsgTx.TxHash().String())
},
}