22
22
23
23
from r2 .controllers .reddit_base import (
24
24
cross_domain ,
25
+ generate_modhash ,
25
26
is_trusted_origin ,
26
27
MinimalController ,
27
28
pagecache_policy ,
51
52
query_string ,
52
53
randstr ,
53
54
sanitize_url ,
54
- timeago ,
55
55
timefromnow ,
56
56
timeuntil ,
57
57
tup ,
110
110
from r2 .controllers .ipn import generate_blob , update_blob
111
111
from r2 .lib .lock import TimeoutExpired
112
112
from r2 .lib .csrf import csrf_exempt
113
+ from r2 .lib .voting import cast_vote
113
114
114
115
from r2 .models import wiki
115
116
from r2 .models .recommend import AccountSRFeedback , FEEDBACK_ACTIONS
117
+ from r2 .models .vote import Vote
116
118
from r2 .lib .merge import ConflictException
117
119
118
120
import csv
124
126
import urllib
125
127
import urllib2
126
128
127
- def reject_vote (thing ):
128
- voteword = request .params .get ('dir' )
129
-
130
- if voteword == '1' :
131
- voteword = 'upvote'
132
- elif voteword == '0' :
133
- voteword = '0-vote'
134
- elif voteword == '-1' :
135
- voteword = 'downvote'
136
-
137
- log_text ("rejected vote" , "Rejected %s from %s (%s) on %s %s via %s" %
138
- (voteword , c .user .name , request .ip , thing .__class__ .__name__ ,
139
- thing ._id36 , request .referer ), "info" )
140
-
141
129
142
130
class ApiminimalController (MinimalController ):
143
131
"""
@@ -547,7 +535,6 @@ def POST_submit(self, form, jquery, url, selftext, kind, title,
547
535
sendreplies = sendreplies ,
548
536
)
549
537
550
-
551
538
if not is_self :
552
539
ban = is_banned_domain (url )
553
540
if ban :
@@ -556,9 +543,6 @@ def POST_submit(self, form, jquery, url, selftext, kind, title,
556
543
hooks .get_hook ('banned_domain.submit' ).call (item = l , url = url ,
557
544
ban = ban )
558
545
559
- queries .queue_vote (c .user , l , dir = True , ip = request .ip ,
560
- cheater = c .cheater )
561
-
562
546
if sr .should_ratelimit (c .user , 'link' ):
563
547
VRatelimit .ratelimit (rate_user = True , rate_ip = True ,
564
548
prefix = "rate_submit_" )
@@ -607,7 +591,7 @@ def _login(self, responder, user, rem = None):
607
591
self .login (user , rem = rem )
608
592
609
593
if request .params .get ("hoist" ) != "cookie" :
610
- responder ._send_data (modhash = user . modhash ())
594
+ responder ._send_data (modhash = generate_modhash ())
611
595
responder ._send_data (cookie = user .make_cookie ())
612
596
responder ._send_data (need_https = feature .is_enabled ("force_https" ))
613
597
@@ -1882,8 +1866,7 @@ def POST_editusertext(self, form, jquery, item, text):
1882
1866
if item ._deleted :
1883
1867
return abort (403 , "forbidden" )
1884
1868
1885
- if (item ._date < timeago ('3 minutes' )
1886
- or (item ._ups + item ._downs > 2 )):
1869
+ if item ._age > timedelta (minutes = 3 ) or item .num_votes > 2 :
1887
1870
item .editted = c .start_time
1888
1871
1889
1872
item .ignore_reports = False
@@ -2035,8 +2018,6 @@ def POST_comment(self, commentform, jquery, parent, comment):
2035
2018
else :
2036
2019
item , inbox_rel = Comment ._new (c .user , link , parent_comment ,
2037
2020
comment , request .ip )
2038
- queries .queue_vote (c .user , item , dir = True , ip = request .ip ,
2039
- cheater = c .cheater )
2040
2021
2041
2022
if is_message :
2042
2023
queries .new_message (item , inbox_rel )
@@ -2161,12 +2142,12 @@ def POST_share(self, shareform, jquery, share_to, message, link):
2161
2142
@require_oauth2_scope ("vote" )
2162
2143
@noresponse (VUser (),
2163
2144
VModhash (),
2164
- vote_info = VVotehash ( 'vh' ) ,
2165
- dir = VInt ( 'dir' , min = - 1 , max = 1 , docs = {"dir" :
2166
- "vote direction. one of (1, 0, -1)" } ),
2145
+ direction = VInt ( "dir" , min = - 1 , max = 1 ,
2146
+ docs = {"dir" : "vote direction. one of (1, 0, -1)" }
2147
+ ),
2167
2148
thing = VByName ('id' ))
2168
2149
@api_doc (api_section .links_and_comments )
2169
- def POST_vote (self , dir , thing , vote_info ):
2150
+ def POST_vote (self , direction , thing ):
2170
2151
"""Cast a vote on a thing.
2171
2152
2172
2153
`id` should be the fullname of the Link or Comment to vote on.
@@ -2182,34 +2163,31 @@ def POST_vote(self, dir, thing, vote_info):
2182
2163
2183
2164
"""
2184
2165
2185
- user = c .user
2186
- store = True
2187
-
2188
2166
if not thing or thing ._deleted :
2189
2167
return self .abort404 ()
2190
2168
2191
- hooks .get_hook ("vote.validate" ).call (thing = thing )
2169
+ if not thing .is_votable :
2170
+ abort (400 , "That type of thing can't be voted on." )
2192
2171
2193
- if not isinstance (thing , (Link , Comment )):
2194
- return self .abort404 ()
2172
+ hooks .get_hook ("vote.validate" ).call (thing = thing )
2195
2173
2196
2174
if isinstance (thing , Link ) and promote .is_promo (thing ):
2197
2175
if not promote .is_promoted (thing ):
2198
2176
return abort (400 , "Bad Request" )
2199
2177
2200
- if vote_info == 'rejected' :
2201
- reject_vote ( thing )
2202
- store = False
2203
-
2204
- if thing . _age > thing . subreddit_slow . archive_age :
2205
- store = False
2206
-
2207
- dir = ( True if dir > 0
2208
- else False if dir < 0
2209
- else None )
2210
-
2211
- queries . queue_vote ( user , thing , dir , request . ip , vote_info = vote_info ,
2212
- store = store , cheater = c . cheater )
2178
+ if thing . archived :
2179
+ return abort ( 400 ,
2180
+ "This thing is archived and may no longer be voted on" )
2181
+
2182
+ # convert vote direction to enum value
2183
+ if direction == 1 :
2184
+ direction = Vote . DIRECTIONS . up
2185
+ elif direction == - 1 :
2186
+ direction = Vote . DIRECTIONS . down
2187
+ elif direction == 0 :
2188
+ direction = Vote . DIRECTIONS . unvote
2189
+
2190
+ cast_vote ( c . user , thing , direction )
2213
2191
2214
2192
@require_oauth2_scope ("modconfig" )
2215
2193
@validatedForm (VSrModerator (perms = 'config' ),
0 commit comments