def subsciteit_slow(self): from subsciteit import Subsciteit """return's a comments's subsciteit. in most case the subsciteit is already on the wrapped link (as .subsciteit), and that should be used when possible. if sr_id does not exist, then use the parent link's""" self._safe_load() if hasattr(self, 'sr_id'): sr_id = self.sr_id else: l = Link._byID(self.link_id, True) sr_id = l.sr_id return Subsciteit._byID(sr_id, True, return_dict = False)
def can_view_slow(self): if c.user_is_loggedin: # simple case from before: if (c.user_is_admin or c.user._id in (self.author_id, self.to_id)): return True elif self.sr_id: sr = Subsciteit._byID(self.sr_id) is_moderator = sr.is_moderator(c.user) # moderators can view messages on subsciteits they moderate if is_moderator: return True elif self.first_message: first = Message._byID(self.first_message, True) return (first.author_id == c.user._id)
def subsciteits(self): from subsciteit import Subsciteit return Subsciteit.user_subsciteits(self)
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) m._spam = author._spam sr_id = None # check to see if the recipient is a subsciteit and swap args accordingly if to and isinstance(to, Subsciteit): to_subsciteit = True to, sr = None, to else: to_subsciteit = False if sr: sr_id = sr._id if parent: m.parent_id = parent._id if parent.first_message: m.first_message = parent.first_message else: m.first_message = parent._id if parent.sr_id: sr_id = parent.sr_id if not to and not sr_id: raise CreationError, "Message created with neither to nor sr_id" m.to_id = to._id if to else None if sr_id is not None: m.sr_id = sr_id m._commit() inbox_rel = None if sr_id and not sr: sr = Subsciteit._byID(sr_id) inbox_rel = [] if sr_id: # if there is a subsciteit 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_subsciteit: inbox_rel.append(ModeratorInbox._add(sr, m, 'inbox')) if author.name in g.admins: m.distinguished = 'admin' m._commit() elif sr.is_moderator(author): m.distinguished = 'yes' m._commit() # if there is a "to" we may have to create an inbox relation as well # also, only global admins can be message spammed. if to and (not m._spam or to.name in g.admins): # if the current "to" is not a sr moderator, # they need to be notified if not sr_id or not sr.is_moderator(to): # Record the inbox relation, but don't give the user # an orangered, if they PM themselves. # Don't notify on PMs from blocked users, either orangered = (to.name != author.name and author._id not in to.enemies) inbox_rel.append(Inbox._add(to, m, 'inbox', orangered=orangered)) # find the message originator elif sr_id and m.first_message: first = Message._byID(m.first_message, True) orig = Account._byID(first.author_id, True) # if the originator is not a moderator... if not sr.is_moderator(orig) and orig._id != author._id: inbox_rel.append(Inbox._add(orig, m, 'inbox')) return (m, inbox_rel)
def add_props(cls, user, wrapped): from r2.lib.template_helpers import add_attr, get_domain from r2.lib import promote from r2.lib.wrapped import CachedVariable from r2.lib.pages import WrappedUser #fetch parent links links = Link._byID(set(l.link_id for l in wrapped), data = True, return_dict = True, stale=True) # fetch authors authors = Account._byID(set(l.author_id for l in links.values()), data=True, return_dict=True, stale=True) #get srs for comments that don't have them (old comments) for cm in wrapped: if not hasattr(cm, 'sr_id'): cm.sr_id = links[cm.link_id].sr_id subsciteits = Subsciteit._byID(set(cm.sr_id for cm in wrapped), data=True, return_dict=False, stale=True) cids = dict((w._id, w) for w in wrapped) parent_ids = set(cm.parent_id for cm in wrapped if getattr(cm, 'parent_id', None) and cm.parent_id not in cids) parents = {} if parent_ids: parents = Comment._byID(parent_ids, data=True, stale=True) can_reply_srs = set(s._id for s in subsciteits if s.can_comment(user)) \ if c.user_is_loggedin else set() can_reply_srs.add(promote.get_promote_srid()) min_score = user.pref_min_comment_score profilepage = c.profilepage user_is_admin = c.user_is_admin user_is_loggedin = c.user_is_loggedin focal_comment = c.focal_comment cname = c.cname site = c.site for item in wrapped: # for caching: item.profilepage = c.profilepage item.link = links.get(item.link_id) if (item.link._score <= 1 or item.score < 3 or item.link._spam or item._spam or item.author._spam): item.nofollow = True else: item.nofollow = False if not hasattr(item, 'subsciteit'): item.subsciteit = item.subsciteit_slow if item.author_id == item.link.author_id and not item.link._deleted: add_attr(item.attribs, 'S', link = item.link.make_permalink(item.subsciteit,criticism=item.criticism)) if not hasattr(item, 'target'): item.target = None if item.parent_id: if item.parent_id in cids: item.parent_permalink = '#' + utils.to36(item.parent_id) else: parent = parents[item.parent_id] item.parent_permalink = parent.make_permalink(item.link, item.subsciteit,criticism=item.criticism) else: item.parent_permalink = None item.can_reply = False if c.can_reply or (item.sr_id in can_reply_srs): age = datetime.now(g.tz) - item._date if age.days < g.REPLY_AGE_LIMIT: item.can_reply = True # not deleted on profile pages, # deleted if spam and not author or admin item.deleted = (not profilepage and (item._deleted or (item._spam and item.author != user and not item.show_spam))) extra_css = '' if item.deleted: extra_css += "grayed" if not user_is_admin: item.author = DeletedUser() item.body = '[deleted]' if focal_comment == item._id36: extra_css += " border" if profilepage: item.link_author = WrappedUser(authors[item.link.author_id]) item.subsciteit_path = item.subsciteit.path if cname: item.subsciteit_path = ("http://" + get_domain(cname = (site == item.subsciteit), subsciteit = False)) if site != item.subsciteit: item.subsciteit_path += item.subsciteit.path item.full_comment_path = item.link.make_permalink(item.subsciteit,criticism=item.criticism) # don't collapse for admins, on profile pages, or if deleted item.collapsed = False if ((item.score < min_score) and not (profilepage or item.deleted or user_is_admin)): item.collapsed = True item.collapsed_reason = _("comment score below threshold") if user_is_loggedin and item.author_id in c.user.enemies: if "grayed" not in extra_css: extra_css += " grayed" item.collapsed = True item.collapsed_reason = _("blocked user") item.editted = getattr(item, "editted", False) item.render_css_class = "comment %s" % CachedVariable("time_period") #will get updated in builder item.num_children = 0 item.score_fmt = Score.points item.permalink = item.make_permalink(item.link, item.subsciteit,criticism=item.criticism) item.is_author = (user == item.author) #If rootid is None that means we're the parent... item.is_rootauthor = item.criticism and user_is_loggedin and (user._id == item.rootauthor) and item.rootid item.is_focal = (focal_comment == item._id36) item_age = c.start_time - item._date if item_age.days > g.VOTE_AGE_LIMIT: item.votable = False else: item.votable = True #will seem less horrible when add_props is in pages.py from r2.lib.pages import UserText item.usertext = UserText(item, item.body, editable = item.is_author, nofollow = item.nofollow, target = item.target, extra_css = extra_css) # Run this last Printable.add_props(user, wrapped)
def subsciteit_slow(self): from subsciteit import Subsciteit """return's a link's subsciteit. in most case the subsciteit is already on the wrapped link (as .subsciteit), and that should be used when possible. """ return Subsciteit._byID(self.sr_id, True, return_dict = False)
def subsciteit_slow(self): from subsciteit import Subsciteit if self.sr_id: return Subsciteit._byID(self.sr_id)
def add_props(cls, user, wrapped): from r2.lib.db import queries #TODO global-ish functions that shouldn't be here? #reset msgtime after this request msgtime = c.have_messages # make sure there is a sr_id set: for w in wrapped: if not hasattr(w, "sr_id"): w.sr_id = None # load the to fields if one exists to_ids = set(w.to_id for w in wrapped if w.to_id is not None) tos = Account._byID(to_ids, True) if to_ids else {} # load the subsciteit field if one exists: sr_ids = set(w.sr_id for w in wrapped if w.sr_id is not None) m_subsciteits = Subsciteit._byID(sr_ids, data = True, return_dict = True) # load the links and their subsciteits (if comment-as-message) links = Link._byID(set(l.link_id for l in wrapped if l.was_comment), data = True, return_dict = True) # subsciteits of the links (for comment-as-message) l_subsciteits = Subsciteit._byID(set(l.sr_id for l in links.values()), data = True, return_dict = True) parents = Comment._byID(set(l.parent_id for l in wrapped if l.parent_id and l.was_comment), data = True, return_dict = True) # load the unread list to determine message newness unread = set(queries.get_unread_inbox(user)) msg_srs = set(m_subsciteits[x.sr_id] for x in wrapped if x.sr_id is not None and isinstance(x.lookups[0], Message)) # load the unread mod list for the same reason mod_unread = set(queries.merge_results( *[queries.get_unread_subsciteit_messages(sr) for sr in msg_srs])) for item in wrapped: item.to = tos.get(item.to_id) if item.sr_id: 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 elif item._fullname in unread: item.new = True # wipe new messages if preferences say so, and this isn't a feed # and it is in the user's personal inbox if (item.new and c.user.pref_mark_messages_read and c.extension not in ("rss", "xml", "api", "json")): queries.set_unread(item.lookups[0], c.user, False) else: item.new = (item._fullname in mod_unread) item.score_fmt = Score.none item.message_style = "" # comment as message: if item.was_comment: link = links[item.link_id] sr = l_subsciteits[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) if item.parent_id: item.subject = _('comment reply') item.message_style = "comment-reply" parent = parents[item.parent_id] item.parent = parent._fullname item.parent_permalink = parent.make_permalink(link, sr) else: item.subject = _('post reply') item.message_style = "post-reply" elif item.sr_id is not None: item.subsciteit = m_subsciteits[item.sr_id] item.is_collapsed = None if not item.new: if item.recipient: item.is_collapsed = item.to_collapse if item.author_id == c.user._id: item.is_collapsed = item.author_collapse if c.user.pref_collapse_read_messages: item.is_collapsed = (item.is_collapsed is not False) if item.author_id in c.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]') # Run this last Printable.add_props(user, wrapped)