Message.add_props: detangle logic.

This commit is contained in:
Brian Simpson
2014-12-04 20:17:20 -05:00
parent 7f84f427fc
commit d2fa4b5364
6 changed files with 177 additions and 146 deletions

View File

@@ -206,7 +206,7 @@ class MessageButtons(PrintableButtons):
permalink = permalink,
was_comment = was_comment,
unread = thing.new,
recipient = thing.recipient,
user_is_recipient = thing.user_is_recipient,
can_reply = can_reply,
parent_id = getattr(thing, "parent_id", None),
show_report = True,

View File

@@ -1313,8 +1313,8 @@ class MoreMessages(Printable):
return self.parent.author
@property
def recipient(self):
return self.parent.recipient
def user_is_recipient(self):
return self.parent.user_is_recipient
@property
def sr_id(self):
@@ -1584,13 +1584,111 @@ class Message(Thing, Printable):
to_set_unread = []
for item in wrapped:
item.to = accounts.get(item.to_id)
if item.sr_id:
item.recipient = (item.author_id != user._id)
else:
item.recipient = (item.to_id == user._id)
user_is_recipient = item.to_id == user._id
user_is_sender = (item.author_id == user._id and
not getattr(item, "display_author", None))
sent_by_sr = item.sr_id and getattr(item, 'from_sr', None)
sent_to_sr = item.sr_id and not item.to_id
if item.author_id == user._id:
item.to = accounts[item.to_id] if item.to_id else None
item.is_mention = False
item.is_collapsed = None
item.score_fmt = Score.none
item.hide_author = False
if item.was_comment:
item.user_is_recipient = user_is_recipient
link = links[item.link_id]
sr = srs[link.sr_id]
item.to_collapse = False
item.author_collapse = False
item.link_title = link.title
item.permalink = item.lookups[0].make_permalink(link, sr=sr)
item.link_permalink = link.make_permalink(sr)
item.full_comment_count = link.num_comments
parent = parents[item.parent_id] if item.parent_id else None
if parent:
item.parent = parent._fullname
item.parent_permalink = parent.make_permalink(link, sr)
if parent and parent.author_id == user._id:
item.subject = _('comment reply')
elif not parent and link.author_id == user._id:
item.subject = _('post reply')
else:
item.subject = _('username mention')
item.is_mention = True
item.taglinetext = _(
"from %(author)s via %(subreddit)s sent %(when)s")
elif item.sr_id:
item.user_is_recipient = not user_is_sender
item.subreddit = srs[item.sr_id]
user_is_moderator = item.sr_id in user_mod_sr_ids
if sent_by_sr:
if item.sr_id in blocked_srids:
item.subject = _('[message from blocked subreddit]')
item.sr_blocked = True
item.is_collapsed = True
if not user_is_moderator and not c.user_is_admin:
item.author = item.subreddit
item.hide_author = True
item.taglinetext = _(
"subreddit message via %(subreddit)s sent %(when)s")
elif user_is_sender:
item.taglinetext = _(
"to %(dest)s sent %(when)s")
else:
item.taglinetext = _(
"to %(dest)s from %(author)s via %(subreddit)s sent"
" %(when)s")
else:
if item._id in mod_message_authors:
# let moderators see the original author when a regular
# user responds to a modmail message from subreddit.
# item.to_id is not set, but we found the original
# sender by inspecting the parent message
item.to = mod_message_authors[item._id]
if user_is_recipient:
item.taglinetext = _(
"from %(author)s via %(subreddit)s sent %(when)s")
elif user_is_sender and sent_to_sr:
item.taglinetext = _("to %(subreddit)s sent %(when)s")
elif user_is_sender:
item.taglinetext = _(
"to %(dest)s via %(subreddit)s sent %(when)s")
elif sent_to_sr:
item.taglinetext = _(
"to %(subreddit)s from %(author)s sent %(when)s")
else:
item.taglinetext = _(
"to %(dest)s from %(author)s via %(subreddit)s sent"
" %(when)s")
else:
item.user_is_recipient = user_is_recipient
if item.display_author:
item.author = accounts[item.display_author]
if item.display_to:
item.to = accounts[item.display_to]
if item.to_id == user._id:
item.body = (strings.anonymous_gilder_warning +
_force_unicode(item.body))
if user_is_recipient:
item.taglinetext = _("from %(author)s sent %(when)s")
elif user_is_sender:
item.taglinetext = _("to %(dest)s sent %(when)s")
else:
item.taglinetext = _(
"to %(dest)s from %(author)s sent %(when)s")
if user_is_sender:
item.new = False
elif item._fullname in unread:
item.new = True
@@ -1600,102 +1698,21 @@ class Message(Thing, Printable):
else:
item.new = item._fullname in mod_unread
item.score_fmt = Score.none
# comment as message:
item.is_mention = False
if item.was_comment:
link = links[item.link_id]
sr = srs[link.sr_id]
item.to_collapse = False
item.author_collapse = False
item.link_title = link.title
item.permalink = item.lookups[0].make_permalink(link, sr=sr)
item.link_permalink = link.make_permalink(sr)
item.full_comment_count = link.num_comments
if item.parent_id:
parent = parents[item.parent_id]
item.parent = parent._fullname
item.parent_permalink = parent.make_permalink(link, sr)
if parent.author_id == user._id:
item.subject = _('comment reply')
else:
item.subject = _('username mention')
item.is_mention = True
else:
if link.author_id == user._id:
item.subject = _('post reply')
else:
item.subject = _('username mention')
item.is_mention = True
elif item.sr_id is not None:
item.subreddit = srs[item.sr_id]
else:
if item.display_author:
item.author = accounts[item.display_author]
if item.display_to:
item.to = accounts[item.display_to]
if item.to_id == user._id:
item.body = (strings.anonymous_gilder_warning +
_force_unicode(item.body))
item.hide_author = False
item.is_collapsed = None
if getattr(item, "from_sr", False):
if not (item.subreddit._id in user_mod_sr_ids or
c.user_is_admin):
item.author = item.subreddit
item.hide_author = True
if item.subreddit._id in blocked_srids:
item.subject = _('[message from blocked subreddit]')
item.sr_blocked = True
item.is_collapsed = True
if not item.new:
if item.recipient:
if item.user_is_recipient:
item.is_collapsed = item.to_collapse
if item.author_id == user._id:
item.is_collapsed = item.author_collapse
if user.pref_collapse_read_messages:
item.is_collapsed = (item.is_collapsed is not False)
if item.author_id in user.enemies and not item.was_comment:
item.is_collapsed = True
if not c.user_is_admin:
item.subject = _('[message from blocked user]')
item.body = _('[unblock user to see this message]')
taglinetext = ''
if item.hide_author:
taglinetext = _("subreddit message %(author)s sent %(when)s")
elif (item.author_id == user._id and
not getattr(item, "display_author", None)):
taglinetext = _("to %(dest)s sent %(when)s")
elif (item._id in mod_message_authors and
(item.subreddit._id in user_mod_sr_ids or c.user_is_admin)):
item.to = mod_message_authors[item._id]
taglinetext = _("to %(dest)s from %(author)s sent %(when)s")
elif item.to_id == user._id or item.to_id is None:
taglinetext = _("from %(author)s sent %(when)s")
else:
taglinetext = _("to %(dest)s from %(author)s sent %(when)s")
item.taglinetext = taglinetext
if item.to:
if item.to._deleted:
item.dest = "[deleted]"
else:
item.dest = item.to.name
else:
item.dest = ""
if item.sr_id:
if item.hide_author:
item.updated_author = _("via %(subreddit)s")
else:
item.updated_author = _("%(author)s via %(subreddit)s")
else:
item.updated_author = ''
if item.sr_id and item.dest and item.to:
if item.sr_id and item.to:
item.to_is_moderator = item.to._id in mods_by_srid[item.sr_id]
if to_set_unread:
@@ -1707,7 +1724,6 @@ class Message(Thing, Printable):
# Inbox.set_unread can only handle one type of thing at a time
queries.set_unread(things, user, unread=False)
# Run this last
Printable.add_props(user, wrapped)
@property

View File

@@ -23,6 +23,7 @@
<%!
from r2.lib.pages import WrappedUser
from r2.lib.filters import safemarkdown, websafe, mako_websafe
from r2.models import Account
%>
<%namespace file="printable.html" import="arrow, score"/>
<%namespace file="utils.html" import="plain_link" />
@@ -55,19 +56,24 @@
<div class="entry">
<%
author = WrappedUser(thing.author, thing.attribs, thing).render()
if thing.sr_id:
subreddit = '&nbsp;<a href="%s.compact">%s</a>&nbsp;' % \
(thing.subreddit.path, thing.subreddit.path)
if not thing.dest:
thing.dest = subreddit
author = websafe(thing.updated_author) % dict(author=author, subreddit=subreddit)
taglinetext = mako_websafe(thing.taglinetext).replace(' ', '&nbsp;')
taglinetext = taglinetext % dict(when=thing.timesince,
author=u"<b>%s</b>" % author,
dest=u"<b>%s</b>" % thing.dest)
substitutions = {}
if thing.sr_id:
substitutions['subreddit'] = u'<b>&nbsp;<a href="%(path)s.compact">%(path)s</a>&nbsp;</b>' % dict(path=thing.subreddit.path)
substitutions['author'] = u"<b>%s</b>" % WrappedUser(thing.author, thing.attribs, thing).render()
if isinstance(thing.to, Account):
substitutions['dest'] = u"<b>%s</b>" % WrappedUser(thing.to, [], thing).render()
elif thing.sr_id:
substitutions['dest'] = substitutions['subreddit']
substitutions['when'] = thing.timesince
taglinetext = mako_websafe(thing.taglinetext).replace(' ', '&nbsp;')
taglinetext %= substitutions
%>
<div class="tagline">
${unsafe(taglinetext)}
</div>

View File

@@ -25,7 +25,9 @@
from r2.lib.pages.things import MessageButtons
from r2.lib.pages import WrappedUser
from r2.lib.template_helpers import static, add_attr
from r2.models import Account
%>
<%inherit file="comment_skeleton.html"/>
<%namespace file="utils.html" import="thing_timestamp" />
@@ -39,7 +41,7 @@
</%def>
<%def name="thing_css_class(what)" buffered="True">
${parent.thing_css_class(what)} ${"new" if thing.new else ""} ${"was-comment" if thing.was_comment else ""} ${"recipient" if thing.recipient else ""} ${"message-reply" if getattr(thing, "is_child", False) else ""} ${"message-parent" if getattr(thing, "is_parent", False) else ""} ${"focal" if getattr(thing, "focal", False) else ""} ${"gold" if getattr(thing, "distinguished", "") == "gold" else ""} ${"gold-auto" if getattr(thing, "distinguished", "") == "gold-auto" else ""}
${parent.thing_css_class(what)} ${"new" if thing.new else ""} ${"was-comment" if thing.was_comment else ""} ${"recipient" if thing.user_is_recipient else ""} ${"message-reply" if getattr(thing, "is_child", False) else ""} ${"message-parent" if getattr(thing, "is_parent", False) else ""} ${"focal" if getattr(thing, "focal", False) else ""} ${"gold" if getattr(thing, "distinguished", "") == "gold" else ""} ${"gold-auto" if getattr(thing, "distinguished", "") == "gold-auto" else ""}
</%def>
<%def name="tagline()">
@@ -48,35 +50,38 @@ ${parent.thing_css_class(what)} ${"new" if thing.new else ""} ${"was-comment" if
</a>
<%
author = WrappedUser(thing.author, thing.attribs, thing).render()
if thing.sr_id:
subreddit = '<a href="%s">%s</a>' % (thing.subreddit.path,
thing.subreddit.path)
if not thing.dest:
thing.dest = subreddit
elif thing.to:
to_attribs = []
if thing.to.name in g.admins:
add_attr(to_attribs, 'A')
elif thing.to_is_moderator:
link = '/r/%s/about/moderators' % thing.subreddit.name
label = _('moderator of /r/%(reddit)s, speaking officially')
label %= {'reddit': thing.subreddit.name}
add_attr(to_attribs, 'M', label=label, link=link)
thing.dest = WrappedUser(thing.to, to_attribs, thing).render()
author = websafe(thing.updated_author).replace(' ', '&#32;') % dict(
author=author,
subreddit=subreddit)
substitutions = {}
taglinetext = mako_websafe(thing.taglinetext).replace(' ', '&#32;') % dict(
when=capture(thing_timestamp, thing, thing.timesince, include_tense=True),
author=u"<b>%s</b>" % author,
dest=u"<b>%s</b>" % thing.dest)
if thing.sr_id:
substitutions['subreddit'] = u'<b><a href="%(path)s">%(path)s</a></b>' % dict(path=thing.subreddit.path)
substitutions['author'] = u"<b>%s</b>" % WrappedUser(thing.author, thing.attribs, thing).render()
if isinstance(thing.to, Account):
to_attribs = []
if thing.sr_id:
if thing.to.name in g.admins:
# distinguish admins in admin modmail
add_attr(to_attribs, 'A')
elif thing.to_is_moderator:
link = '/r/%s/about/moderators' % thing.subreddit.name
label = _('moderator of /r/%(reddit)s, speaking officially')
label %= {'reddit': thing.subreddit.name}
add_attr(to_attribs, 'M', label=label, link=link)
substitutions['dest'] = u"<b>%s</b>" % WrappedUser(thing.to, to_attribs, thing).render()
elif thing.sr_id:
substitutions['dest'] = substitutions['subreddit']
substitutions['when'] = capture(thing_timestamp, thing, thing.timesince, include_tense=True)
taglinetext = mako_websafe(thing.taglinetext).replace(' ', '&#32;')
taglinetext %= substitutions
%>
<span class="head">
${unsafe(taglinetext)}
</span>
%if c.user_is_admin:
${self.admintagline()}
%endif
@@ -94,7 +99,7 @@ ${parent.thing_css_class(what)} ${"new" if thing.new else ""} ${"was-comment" if
%else:
<span class="correspondent rounded">
<%
corr = thing.author if thing.recipient else thing.to
corr = thing.author if thing.user_is_recipient else thing.to
%>
${WrappedUser(corr)}
</span>

View File

@@ -23,7 +23,7 @@
<%!
from r2.lib.filters import safemarkdown, websafe, mako_websafe
from r2.lib.template_helpers import add_sr
from r2.models.account import DeletedUser
from r2.models.account import DeletedUser, Account
%>
<%
@@ -34,20 +34,24 @@
<link>${permalink}</link>
<title>
<%
author = thing.author
if author._deleted:
author = DeletedUser()
author_name = author.name
if thing.sr_id and not (getattr(thing, "is_child", False) or
getattr(thing, "is_parent", False)):
subreddit = thing.subreddit.name
author_name = websafe(thing.updated_author) % dict(author=author_name,
subreddit=subreddit)
taglinetext = mako_websafe(thing.taglinetext)
substitutions = {}
if thing.sr_id:
substitutions['subreddit'] = thing.subreddit.name
substitutions['author'] = (DeletedUser() if thing.author._deleted else thing.author).name
if isinstance(thing.to, Account):
substitutions['dest'] = (DeletedUser() if thing.to._deleted else thing.to).name
elif thing.sr_id:
substitutions['dest'] = substitutions['subreddit']
substitutions['when'] = thing.timesince
taglinetext = mako_websafe(thing.taglinetext)
taglinetext %= substitutions
%>
${thing.subject} : ${unsafe(taglinetext % dict(when=thing.timesince,
author=u"%s" % author_name,
dest=u"%s" % thing.dest))}
${thing.subject} : ${unsafe(taglinetext)}
</title>
<pubDate>${thing._date.strftime('%a, %d %b %Y %H:%M:%S %z')}</pubDate>
<dc:date>${thing._date.isoformat()}-0700</dc:date>

View File

@@ -411,7 +411,7 @@
${self.bylink_button(_("permalink"), thing.permalink, sr_path=False)}
</li>
%endif
%if thing.recipient:
%if thing.user_is_recipient:
%if thing.can_block:
${self.banbuttons()}
%if (not thing.was_comment or thing.thing.is_mention) and thing.thing.author_id != c.user._id and thing.thing.author_id not in c.user.enemies: