def recompute_unread(min_date = None): from r2.models import Inbox, Account, Comment, Message from r2.lib.db import queries def load_accounts(inbox_rel): accounts = set() q = inbox_rel._query(eager_load = False, data = False, sort = desc("_date")) if min_date: q._filter(inbox_rel.c._date > min_date) for i in fetch_things2(q): accounts.add(i._thing1_id) return accounts accounts_m = load_accounts(Inbox.rel(Account, Message)) for i, a in enumerate(accounts_m): a = Account._byID(a) print "%s / %s : %s" % (i, len(accounts_m), a) queries.get_unread_messages(a).update() queries.get_unread_comments(a).update() queries.get_unread_selfreply(a).update() accounts = load_accounts(Inbox.rel(Account, Comment)) - accounts_m for i, a in enumerate(accounts): a = Account._byID(a) print "%s / %s : %s" % (i, len(accounts), a) queries.get_unread_comments(a).update() queries.get_unread_selfreply(a).update()
def recompute_unread(min_date=None): from r2.models import Inbox, Account, Comment, Message from r2.lib.db import queries def load_accounts(inbox_rel): accounts = set() q = inbox_rel._query(eager_load=False, data=False, sort=desc("_date")) if min_date: q._filter(inbox_rel.c._date > min_date) for i in fetch_things2(q): accounts.add(i._thing1_id) return accounts accounts_m = load_accounts(Inbox.rel(Account, Message)) for i, a in enumerate(accounts_m): a = Account._byID(a) print "%s / %s : %s" % (i, len(accounts_m), a) queries.get_unread_messages(a).update() queries.get_unread_comments(a).update() queries.get_unread_selfreply(a).update() accounts = load_accounts(Inbox.rel(Account, Comment)) - accounts_m for i, a in enumerate(accounts): a = Account._byID(a) print "%s / %s : %s" % (i, len(accounts), a) queries.get_unread_comments(a).update() queries.get_unread_selfreply(a).update()
def monitor_mentions(comment): if not isinstance(comment, Comment): return if comment._spam or comment._deleted: return sender = comment.author_slow if getattr(sender, "butler_ignore", False): # this is an account that generates false notifications, e.g. # LinkFixer return subreddit = comment.subreddit_slow usernames = list( extract_user_mentions(comment.body, num=g.butler_max_mentions + 1)) inbox_class = Inbox.rel(Account, Comment) # If more than our allowed number of mentions were passed, don't highlight # any of them. if len(usernames) > g.butler_max_mentions: return # Subreddit.can_view stupidly requires this. c.user_is_loggedin = True for username in usernames: try: account = Account._by_name(username) except NotFound: continue # most people are aware of when they mention themselves. if account == sender: continue # bail out if that user has the feature turned off if not account.pref_monitor_mentions: continue # don't notify users of things they can't see if not subreddit.can_view(account): continue # don't notify users when a person they've blocked mentions them if account.is_enemy(sender): continue # ensure this comment isn't already in the user's inbox already rels = inbox_class._fast_query( account, comment, ("inbox", "selfreply", "mention"), ) if filter(None, rels.values()): continue notify_mention(account, comment)
def monitor_mentions(comment): if not isinstance(comment, Comment): return if comment._spam or comment._deleted: return sender = comment.author_slow if getattr(sender, "butler_ignore", False): # this is an account that generates false notifications, e.g. # LinkFixer return subreddit = comment.subreddit_slow usernames = list(extract_user_mentions(comment.body, num=g.butler_max_mentions + 1)) inbox_class = Inbox.rel(Account, Comment) # If more than our allowed number of mentions were passed, don't highlight # any of them. if len(usernames) > g.butler_max_mentions: return # Subreddit.can_view stupidly requires this. c.user_is_loggedin = True for username in usernames: try: account = Account._by_name(username) except NotFound: continue # most people are aware of when they mention themselves. if account == sender: continue # bail out if that user has the feature turned off if not account.pref_monitor_mentions: continue # don't notify users of things they can't see if not subreddit.can_view(account): continue # don't notify users when a person they've blocked mentions them if account.is_enemy(sender): continue # ensure this comment isn't already in the user's inbox already rels = inbox_class._fast_query( account, comment, ("inbox", "selfreply", "mention"), ) if filter(None, rels.values()): continue notify_mention(account, comment)
return rel_query(SaveHide, user, 'hide') @cached_userrel_query def get_saved(user): return rel_query(SaveHide, user, 'save') @migrating_cached_srrel_query def get_subreddit_messages(sr): return rel_query(ModeratorInbox, sr, 'inbox') @migrating_cached_srrel_query def get_unread_subreddit_messages(sr): return rel_query(ModeratorInbox, sr, 'inbox', filters = [ModeratorInbox.c.new == True]) inbox_message_rel = Inbox.rel(Account, Message) @migrating_cached_userrel_query def get_inbox_messages(user): return rel_query(inbox_message_rel, user, 'inbox') @migrating_cached_userrel_query def get_unread_messages(user): return rel_query(inbox_message_rel, user, 'inbox', filters = [inbox_message_rel.c.new == True]) inbox_comment_rel = Inbox.rel(Account, Comment) @migrating_cached_userrel_query def get_inbox_comments(user): return rel_query(inbox_comment_rel, user, 'inbox') @migrating_cached_userrel_query
def get_saved(user): return user_rel_query(SaveHide, user, 'save') def get_subreddit_messages(sr): return user_rel_query(ModeratorInbox, sr, 'inbox') def get_unread_subreddit_messages(sr): return user_rel_query(ModeratorInbox, sr, 'inbox', filters=[ModeratorInbox.c.new == True]) inbox_message_rel = Inbox.rel(Account, Message) def get_inbox_messages(user): return user_rel_query(inbox_message_rel, user, 'inbox') def get_unread_messages(user): return user_rel_query(inbox_message_rel, user, 'inbox', filters=[inbox_message_rel.c.new == True]) inbox_comment_rel = Inbox.rel(Account, Comment)