From d3f8fdbda2ef4df9be69b4177921bda7c6c4d3f6 Mon Sep 17 00:00:00 2001 From: Keith Mitchell Date: Thu, 13 Oct 2011 10:32:15 -0700 Subject: [PATCH] Send ban message from SR, not mod --- r2/r2/controllers/api.py | 10 ++++++++-- r2/r2/lib/jsontemplates.py | 2 ++ r2/r2/models/link.py | 28 ++++++++++++++++++---------- r2/r2/templates/message.compact | 11 ++++++++--- r2/r2/templates/message.html | 9 +++++++-- r2/r2/templates/message.xml | 9 +++++++-- 6 files changed, 50 insertions(+), 19 deletions(-) diff --git a/r2/r2/controllers/api.py b/r2/r2/controllers/api.py index a3e6a7eea..e6a78701f 100644 --- a/r2/r2/controllers/api.py +++ b/r2/r2/controllers/api.py @@ -565,8 +565,14 @@ class ApiController(RedditController): title = container.title) msg = msg % d subj = subj % d - item, inbox_rel = Message._new(c.user, friend, - subj, msg, ip) + if type == 'banned': + from_sr = True + sr = container + else: + from_sr = False + sr = None + item, inbox_rel = Message._new(c.user, friend, subj, msg, + ip, from_sr=from_sr, sr=sr) queries.new_message(item, inbox_rel) diff --git a/r2/r2/lib/jsontemplates.py b/r2/r2/lib/jsontemplates.py index ff099ce67..d9ad55d32 100644 --- a/r2/r2/lib/jsontemplates.py +++ b/r2/r2/lib/jsontemplates.py @@ -415,6 +415,8 @@ class MessageJsonTemplate(ThingJsonTemplate): return None elif attr == "body_html": return safemarkdown(thing.body) + elif attr == "author" and getattr(thing, "hide_author", False): + return None return ThingJsonTemplate.thing_attr(self, thing, attr) def rendered_data(self, wrapped): diff --git a/r2/r2/models/link.py b/r2/r2/models/link.py index 1dd86ed08..1d7dda11e 100644 --- a/r2/r2/models/link.py +++ b/r2/r2/models/link.py @@ -930,16 +930,16 @@ class Message(Thing, Printable): cache_ignore = set(["to", "subreddit"]).union(Printable.cache_ignore) @classmethod - def _new(cls, author, to, subject, body, ip, parent = None, sr = None): - m = Message(subject = subject, - body = body, - author_id = author._id, - new = True, - ip = ip) + def _new(cls, author, to, subject, body, ip, parent=None, sr=None, + from_sr=False): + m = Message(subject=subject, body=body, author_id=author._id, new=True, + ip=ip, from_sr=from_sr) m._spam = author._spam sr_id = None # check to see if the recipient is a subreddit and swap args accordingly if to and isinstance(to, Subreddit): + if from_sr: + raise CreationError("Cannot send from SR to SR") to_subreddit = True to, sr = None, to else: @@ -957,7 +957,9 @@ class Message(Thing, Printable): sr_id = parent.sr_id if not to and not sr_id: - raise CreationError, "Message created with neither to nor sr_id" + raise CreationError("Message created with neither to nor sr_id") + if from_sr and not sr_id: + raise CreationError("Message sent from_sr without setting sr") m.to_id = to._id if to else None if sr_id is not None: @@ -965,7 +967,6 @@ class Message(Thing, Printable): m._commit() - inbox_rel = None if sr_id and not sr: sr = Subreddit._byID(sr_id) @@ -974,7 +975,7 @@ class Message(Thing, Printable): # if there is a subreddit id, and it's either a reply or # an initial message to an SR, add to the moderator inbox # (i.e., don't do it for automated messages from the SR) - if parent or to_subreddit: + if parent or to_subreddit and not from_sr: inbox_rel.append(ModeratorInbox._add(sr, m, 'inbox')) if author.name in g.admins: m.distinguished = 'admin' @@ -1074,7 +1075,7 @@ class Message(Thing, Printable): item.recipient = (item.author_id != c.user._id) else: item.recipient = (item.to_id == c.user._id) - + # new-ness is stored on the relation if item.author_id == c.user._id: item.new = False @@ -1112,6 +1113,13 @@ class Message(Thing, Printable): item.message_style = "post-reply" elif item.sr_id is not None: item.subreddit = m_subreddits[item.sr_id] + + item.hide_author = False + if getattr(item, "from_sr", False): + if not (item.subreddit.is_moderator(c.user) or + c.user_is_admin): + item.author = item.subreddit + item.hide_author = True item.is_collapsed = None if not item.new: diff --git a/r2/r2/templates/message.compact b/r2/r2/templates/message.compact index 1b101fcac..ecd8f5d8e 100644 --- a/r2/r2/templates/message.compact +++ b/r2/r2/templates/message.compact @@ -55,7 +55,9 @@
<% taglinetext = '' - if thing.to_id == c.user._id or thing.to_id is None: + if getattr(thing, "hide_author", False): + taglinetext = _("subreddit message %(author)s sent %(when)s ago") + elif thing.to_id == c.user._id or thing.to_id is None: taglinetext = _("from %(author)s sent %(when)s ago") elif thing.author_id == c.user._id: taglinetext = _("to %(dest)s sent %(when)s ago") @@ -65,7 +67,10 @@ taglinetext = taglinetext.replace(' ', ' ') author = WrappedUser(thing.author, thing.attribs, thing).render() if thing.sr_id: - updated_author = _("%(author)s via %(subreddit)s").replace(' ', ' ') + if getattr(thing, "hide_author", False): + updated_author = _("via %(subreddit)s") + else: + updated_author = _("%(author)s via %(subreddit)s").replace(' ', ' ') subreddit = '%s' % (thing.subreddit.path, thing.subreddit.name) author = updated_author % dict(author = author, subreddit = subreddit) @@ -80,7 +85,7 @@ ${unsafe(safemarkdown(thing.body))}