4
4
"github.com/robfig/revel"
5
5
"github.com/PacketFire/goqdb/app/models"
6
6
"strings"
7
- "encoding/hex"
8
- "crypto/md5"
9
- "io"
10
7
"time"
11
8
)
12
9
@@ -22,16 +19,6 @@ const (
22
19
ERR_ID_NOT_FOUND = "id not found"
23
20
)
24
21
25
- func setUserId (c * revel.Controller ) revel.Result {
26
- h := md5 .New ()
27
-
28
- io .WriteString (h , c .Request .RemoteAddr )
29
- io .WriteString (h , c .Request .Header .Get ("User-Agent" ))
30
-
31
- c .Params .Set ("userid" , hex .EncodeToString (h .Sum (nil )))
32
- return nil
33
- }
34
-
35
22
func (c * Base ) getEntries (arg models.Args ) []models.Quote {
36
23
var entries []models.Quote
37
24
@@ -138,9 +125,7 @@ func (c *Base) getTotal (arg models.Args) int64 {
138
125
139
126
func (c * Base ) insertQuote (quote * models.Quote ) {
140
127
141
- var userId string
142
-
143
- c .Params .Bind (& userId , "userid" )
128
+ userId := c .Session .Id ()
144
129
145
130
if ! c .canPost (userId ) {
146
131
c .Flash .Error (ERR_POST_THRESHOLD_REACHED )
@@ -223,15 +208,15 @@ func (c *Base) vote (voteId int, voteType string) {
223
208
return
224
209
}
225
210
226
- var userId string
227
- c .Params .Bind (& userId , "userid" )
228
-
229
- if ! c .canVote (voteId , userId ) {
230
- c .Flash .Error (ERR_MULTIPLE_VOTE )
231
- return
232
- }
211
+ userId := c .Session .Id ()
233
212
234
213
if voteType != models .VOTE_DELETE {
214
+
215
+ if ! c .canVote (voteId , userId ) {
216
+ c .Flash .Error (ERR_MULTIPLE_VOTE )
217
+ return
218
+ }
219
+
235
220
query := `UPDATE QuoteEntry SET Rating = Rating`
236
221
switch voteType {
237
222
case models .VOTE_UP :
@@ -250,6 +235,21 @@ func (c *Base) vote (voteId int, voteType string) {
250
235
c .Flash .Error (ERR_ID_NOT_FOUND )
251
236
return
252
237
}
238
+ } else {
239
+ t := time .Now ().Truncate (24 * time .Hour ).Unix ()
240
+ res , err := c .Txn .Exec (
241
+ `DELETE FROM QuoteEntry ` +
242
+ `WHERE QuoteId = ? AND UserId = ? AND Created >= ?` ,
243
+ voteId , userId , t )
244
+
245
+ if err != nil {
246
+ panic (err )
247
+ }
248
+ if n , err := res .RowsAffected (); n >= 1 {
249
+ return
250
+ } else if err != nil {
251
+ panic (err )
252
+ }
253
253
}
254
254
255
255
err := c .Txn .Insert (& models.VoteEntry {
0 commit comments