Permalink
Browse files

More easily support messaging reddits.

  • Loading branch information...
1 parent 1c49c5c commit 208832517f26406bed20e8506e224219e44b518e @bboe bboe committed Apr 2, 2012
Showing with 30 additions and 15 deletions.
  1. +12 −2 reddit/__init__.py
  2. +9 −10 reddit/objects.py
  3. +9 −3 reddit/reddit_test.py
View
@@ -484,10 +484,20 @@ def _mark_as_read(self, thing_ids, unread=False):
@reddit.decorators.RequireCaptcha
@reddit.decorators.require_login
def compose_message(self, recipient, subject, message, captcha=None):
- """Send a message to another redditor."""
+ """Send a message to another redditor or a subreddit.
+
+ When sending a message to a subreddit the recipient paramater must
+ either be a subreddit object or the subreddit name needs to be prefixed
+ with either '/r/' or '#'.
+ """
+ if isinstance(recipient, reddit.objects.Subreddit):
+ to = '/r/%s' % recipient.display_name
+ else:
+ to = six.text_type(recipient)
+
params = {'text': message,
'subject': subject,
- 'to': six.text_type(recipient),
+ 'to': to,
'user': self.user.name}
if captcha:
params.update(captcha)
View
@@ -167,8 +167,7 @@ def edit(self, text):
class Inboxable(RedditContentObject):
- """Interface for RedditContentObjects that appear in the Inbox."""
- @require_login
+ """Interface for Reddit content objects that appear in the Inbox."""
def reply(self, text):
"""Reply to the comment with the specified text."""
# pylint: disable-msg=E1101,W0212
@@ -181,17 +180,21 @@ def reply(self, text):
self.reddit_session.config['sent']])
return response
- @require_login
def mark_as_read(self):
""" Marks the comment as read."""
return self.reddit_session.user.mark_as_read(self)
- @require_login
def mark_as_unread(self):
""" Marks the comment as unread."""
return self.reddit_session.user.mark_as_read(self, unread=True)
+class Messageable(RedditContentObject):
+ """Interface for Reddit content objects that can be messaged."""
+ def compose_message(self, subject, message):
+ return self.reddit_session.compose_message(self, subject, message)
+
+
class Reportable(RedditContentObject):
"""Interface for Reddit content objects that can be reported."""
@require_login
@@ -351,7 +354,7 @@ def comments(self, update=True):
return self._comments
-class Redditor(RedditContentObject):
+class Redditor(Messageable):
"""A class for Redditor methods."""
get_overview = _get_section('')
get_comments = _get_section('comments')
@@ -371,10 +374,6 @@ def __unicode__(self):
return self.name
@require_login
- def compose_message(self, subject, message):
- return self.reddit_session.compose_message(self, subject, message)
-
- @require_login
def friend(self):
"""Friend the user."""
return _modify_relationship('friend')(self.reddit_session.user, self)
@@ -610,7 +609,7 @@ def add_comment(self, text):
return response
-class Subreddit(RedditContentObject):
+class Subreddit(Messageable):
"""A class for Subreddits."""
ban = _modify_relationship('banned', is_sub=True)
View
@@ -423,6 +423,15 @@ def test_compose(self):
else:
self.fail('Could not find the message we just sent to ourself.')
+ def test_modmail_compose(self):
+ subject = 'Unique message: %s' % uuid.uuid4()
+ self.r.get_subreddit(self.sr).compose_message(subject, 'Content')
+ for msg in self.r.user.get_modmail():
+ if msg.subject == subject:
+ break
+ else:
+ self.fail('Could not find the message we just sent to outself.')
+
def test_mark_as_read(self):
oth = Reddit(USER_AGENT)
oth.login('PyApiTestUser3', '1111')
@@ -459,9 +468,6 @@ def test_mark_multiple_as_read(self):
for msg in messages:
self.assertTrue(msg not in unread)
- def test_modmail(self):
- self.assertTrue(len(list(self.r.user.get_modmail())) > 0)
-
def test_reply_to_message_and_verify(self):
text = 'Unique message reply: %s' % uuid.uuid4()
found = None

0 comments on commit 2088325

Please sign in to comment.