Example #1
0
    def _run_realtime_email_queue(msgs, chan):

        if time.time() - run_realtime_email_queue.last_got_accounts > 600:
            #-- Pick up a fresh list of accounts, if we havenn't done so recently, in case settings change
            if g.email_debug:
                g.log.info('Getting accounts')
            run_realtime_email_queue.accounts = Account._query(Account.c.email != None, sort = asc('_date'), data=True)
            run_realtime_email_queue.last_got_accounts = time.time()
        
        for msg in msgs:
            # msg.body contains the unique name of the post, comment or message, e.g. 't1_2n'(comment #95) or 't6_q'(post #26)
            fullname = str(msg.body)
            fullname_type = fullname[0:2]
            id36 = fullname[3:]
            if g.email_debug:
                g.log.info('msg: %r', fullname)
            howold = (datetime.datetime.now() - msg.timestamp).total_seconds() 
            if  howold < 110:
                # Wait until this item is 2 minutes old, to allow time for corrections
                if g.email_debug:
                    g.log.info('waiting for a moment')
                time.sleep(120 - howold)

            is_com = is_post = False
            thing = link = comment = None
            if fullname_type == 't1':
                # a comment
                is_com = True
                comment = Comment._byID36(id36, data=True)
                if g.email_debug:
                    g.log.info('comment: %r', comment.body)
                thing = comment
                author = Account._byID(comment.author_id, True)
                kind = Email.Kind.REALTIME_COMMENT
                template = 'email_realtime_comment.html'
                link = Link._byID(comment.link_id, data=True)  
                subject = 'Re: %s' % link.title
                sr_id = comment.sr_id
                
            elif fullname_type == 't6':
                # a post/link
                is_post = True
                link = Link._byID36(id36, data=True)
                if g.email_debug:
                    g.log.info('post: %r', link.title)
                thing = link
                author = Account._byID(link.author_id, True)
                kind = Email.Kind.REALTIME_POST
                template = 'email_realtime_post.html'
                subject = link.title
                sr_id = link.sr_id
                
            else:
                return
            
            sr = Subreddit._byID(sr_id, data=True)
            
            subject = "[%s] %s" % (sr.name, subject)
            
            for account in run_realtime_email_queue.accounts:
                
                sub = sr.get_subscriber(account)
                
                if is_com: 
                    if hasattr(sub,'email_comments') and sub.email_comments:
                        if g.email_debug:
                            g.log.info('  account %r: we should send this comment, because of the space setting', account.name)
                        whysend = 'space'
                    else:
                        email_thread = Link._somethinged(SaveHide, account, link, 'email')[account,link,'email']
                        if email_thread:
                            if g.email_debug:
                                g.log.info('  account %r: we should send this comment, because of the thread setting', account.name)
                            whysend = 'thread'
                        else:    
                            continue
                    
                elif is_post:
                    if hasattr(sub,'email_posts') and sub.email_posts:
                        if g.email_debug:
                            g.log.info('  account %r: we should send this post', account.name)
                        whysend = 'space'
                    else:
                        continue

                if not ('session' in locals()):
                    # Open the SMTP session
                    if g.email_debug:
                        g.log.info('Opening SMTP session')
                    session = open_smtp_session()

                # Render the template
                html_email_template = g.mako_lookup.get_template(template)
                html_body = html_email_template.render(link=link, comment=comment, thing=thing, account=account, sub=sub, whysend=whysend)
            
                from_email = '"%s" <%s>' % (g.realtime_email_from_name, g.share_reply,)
                send_html_email(account.email, g.share_reply, subject, html_body, from_full=from_email, session=session)
                if g.email_debug:
                    g.log.info('    sent to %r at %r', account.name, account.email)

        if g.email_debug:
            g.log.info('Done running queue')

        if 'session' in locals():
            # Close the session.
            session.quit()