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)
def notify_mention(user, thing): try: inbox_rel = Inbox._add(user, thing, "mention") except CreationError: # this mention was already inserted, ignore it g.log.error("duplicate mention for (%s, %s)", user, thing) return with query_cache.CachedQueryMutator() as m: m.insert(queries.get_inbox_comment_mentions(user), [inbox_rel]) queries.set_unread(thing, user, unread=True, mutator=m)
def set_unread(message, to, unread): if isinstance(to, Subreddit): for i in ModeratorInbox.set_unread(message, unread): kw = dict(insert_items=i) if unread else dict(delete_items=i) add_queries([get_unread_subreddit_messages(i._thing1)], **kw) else: for i in Inbox.set_unread(message, unread, to=to): kw = dict(insert_items=i) if unread else dict(delete_items=i) if i._name == 'selfreply': add_queries([get_unread_selfreply(i._thing1)], **kw) elif isinstance(message, Comment): add_queries([get_unread_comments(i._thing1)], **kw) else: add_queries([get_unread_messages(i._thing1)], **kw)
def set_unread(message, to, unread): if isinstance(to, Subreddit): for i in ModeratorInbox.set_unread(message, unread): kw = dict(insert_items=i) if unread else dict(delete_items=i) add_queries([get_unread_subreddit_messages(i._thing1)], **kw) else: for i in Inbox.set_unread(message, unread, to=to): kw = dict(insert_items=i) if unread else dict(delete_items=i) if i._name == "selfreply": add_queries([get_unread_selfreply(i._thing1)], **kw) elif isinstance(message, Comment): add_queries([get_unread_comments(i._thing1)], **kw) else: add_queries([get_unread_messages(i._thing1)], **kw)
def set_unread(messages, to, unread): # Maintain backwards compatability messages = tup(messages) if isinstance(to, Subreddit): for i in ModeratorInbox.set_unread(messages, unread): kw = dict(insert_items=i) if unread else dict(delete_items=i) add_queries([get_unread_subreddit_messages(i._thing1)], **kw) else: # All messages should be of the same type for i in Inbox.set_unread(messages, unread, to=to): kw = dict(insert_items=i) if unread else dict(delete_items=i) if isinstance(messages[0], Comment) and not unread: add_queries([get_unread_comments(i._thing1)], **kw) add_queries([get_unread_selfreply(i._thing1)], **kw) elif i._name == 'selfreply': add_queries([get_unread_selfreply(i._thing1)], **kw) elif isinstance(messages[0], Comment): add_queries([get_unread_comments(i._thing1)], **kw) else: add_queries([get_unread_messages(i._thing1)], **kw)
def set_unread(messages, to, unread): # Maintain backwards compatability messages = tup(messages) if isinstance(to, Subreddit): for i in ModeratorInbox.set_unread(messages, unread): kw = dict(insert_items = i) if unread else dict(delete_items = i) add_queries([get_unread_subreddit_messages(i._thing1)], **kw) else: # All messages should be of the same type for i in Inbox.set_unread(messages, unread, to=to): kw = dict(insert_items = i) if unread else dict(delete_items = i) if isinstance(messages[0], Comment) and not unread: add_queries([get_unread_comments(i._thing1)], **kw) add_queries([get_unread_selfreply(i._thing1)], **kw) elif i._name == 'selfreply': add_queries([get_unread_selfreply(i._thing1)], **kw) elif isinstance(messages[0], Comment): add_queries([get_unread_comments(i._thing1)], **kw) else: add_queries([get_unread_messages(i._thing1)], **kw)
def set_unread(messages, to, unread, mutator=None): # Maintain backwards compatability messages = tup(messages) if not mutator: m = CachedQueryMutator() else: m = mutator if isinstance(to, Subreddit): for i in ModeratorInbox.set_unread(messages, unread): q = get_unread_subreddit_messages(i._thing1_id) if unread: m.insert(q, [i]) else: m.delete(q, [i]) else: # All messages should be of the same type # (asserted by Inbox.set_unread) for i in Inbox.set_unread(messages, unread, to=to): query = None if isinstance(messages[0], Comment): if i._name == "inbox": query = get_unread_comments(i._thing1_id) elif i._name == "selfreply": query = get_unread_selfreply(i._thing1_id) elif isinstance(messages[0], Message): query = get_unread_messages(i._thing1_id) assert query is not None if unread: m.insert(query, [i]) else: m.delete(query, [i]) if not mutator: m.send()
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)
def get_unread_and_unemailed(user): inbox_items = Inbox.get_unread_and_unemailed(user._id) return sorted(inbox_items, key=lambda x: x[1]._date)
def notify_mention(user, thing): inbox_rel = Inbox._add(user, thing, "mention") with query_cache.CachedQueryMutator() as m: m.insert(queries.get_inbox_comment_mentions(user), [inbox_rel]) queries.set_unread(thing, user, unread=True, mutator=m)