Permalink
Browse files

Modify "friend" and "unfriend" API methods to only require "container…

…" parameter when the action is upon a user, and to utilize the c.site (or 'r' parameter) when the action is upon a subreddit.
  • Loading branch information...
1 parent 07d8675 commit e2fd083cb4ac74c4521e8bd426ccb2836cc24321 @bboe committed with spladug Dec 24, 2011
Showing with 23 additions and 13 deletions.
  1. +23 −13 r2/r2/controllers/api.py
View
@@ -472,7 +472,7 @@ def POST_leavecontributor(self, container):
VModhash(),
nuser = VExistingUname('name'),
iuser = VByName('id'),
- container = VByName('container'),
+ container = nop('container'),
type = VOneOf('type', ('friend', 'enemy', 'moderator',
'contributor', 'banned')))
@api_doc(api_section.users)
@@ -481,18 +481,22 @@ def POST_unfriend(self, nuser, iuser, container, type):
Handles removal of a friend (a user-user relation) or removal
of a user's privileges from a subreddit (a user-subreddit
relation). The user can either be passed in by name (nuser)
- or by fullname (iuser). 'container' will either be the
- current user or the subreddit.
-
+ or by fullname (iuser). If type is friend or enemy, 'container'
+ will be the current user, otherwise the subreddit must be set.
"""
+ sr_types = ('moderator', 'contributor', 'banned')
+ if type in sr_types:
+ container = c.site
+ else:
+ container = VByName('container').run(container)
+ if not container:
+ return
+
# The user who made the request must be an admin or a moderator
# for the privilege change to succeed.
-
victim = iuser or nuser
-
if (not c.user_is_admin
- and (type in ('moderator','contributor','banned')
- and not container.is_moderator(c.user))):
+ and (type in sr_types and not container.is_moderator(c.user))):
abort(403, 'forbidden')
if (type == 'moderator' and not
(c.user_is_admin or container.can_demod(c.user, victim))):
@@ -505,7 +509,7 @@ def POST_unfriend(self, nuser, iuser, container, type):
new = fn(victim)
# Log this action
- if new and type in ('moderator','contributor','banned'):
+ if new and type in sr_types:
action = dict(banned='unbanuser', moderator='removemoderator',
contributor='removecontributor').get(type, None)
ModAction.create(container, c.user, action, target=victim)
@@ -520,7 +524,7 @@ def POST_unfriend(self, nuser, iuser, container, type):
VModhash(),
ip = ValidIP(),
friend = VExistingUname('name'),
- container = VByName('container'),
+ container = nop('container'),
type = VOneOf('type', ('friend', 'moderator',
'contributor', 'banned')),
note = VLength('note', 300))
@@ -531,13 +535,19 @@ def POST_friend(self, form, jquery, ip, friend,
Complement to POST_unfriend: handles friending as well as
privilege changes on subreddits.
"""
+ sr_types = ('moderator', 'contributor', 'banned')
+ if type in sr_types:
+ container = c.site
+ else:
+ container = VByName('container').run(container)
+ if not container:
+ return
fn = getattr(container, 'add_' + type)
# The user who made the request must be an admin or a moderator
# for the privilege change to succeed.
if (not c.user_is_admin
- and (type in ('moderator','contributor', 'banned')
- and not container.is_moderator(c.user))):
+ and (type in sr_types and not container.is_moderator(c.user))):
abort(403,'forbidden')
# if we are (strictly) friending, the container
@@ -551,7 +561,7 @@ def POST_friend(self, form, jquery, ip, friend,
new = fn(friend)
# Log this action
- if new and type in ('moderator','contributor','banned'):
+ if new and type in sr_types:
action = dict(banned='banuser', moderator='addmoderator',
contributor='addcontributor').get(type, None)
ModAction.create(container, c.user, action, target=friend)

0 comments on commit e2fd083

Please sign in to comment.