Esempio n. 1
0
def perform_action(subreddit, item, condition):
    """Performs the action for the condition(s).
    
    Also delivers the comment (if set) and creates an ActionLog entry.
    """
    
    global r
    disclaimer = ('\n\n*I am a bot, and this action was performed '
                    'automatically. Please [contact the moderators of this '
                    'subreddit](http://www.reddit.com/message/compose?'
                    'to=%23'+item.subreddit.display_name+') if you have any '
                    'questions or concerns.*')

    # build the comment if multiple conditions were matched
    if isinstance(condition, list):
        if any([c.comment for c in condition]):
            if condition[0].action == 'alert':
                verb = 'alerted'
            else:
                verb = condition[0].action+'d'

            comment = ('This has been '+verb+' for the following reasons:\n\n')
            for c in condition:
                if c.comment:
                    comment += '* '+c.comment+'\n'
            post_comment(item, comment)

        # bit of a hack and only logs and uses attributes from first
        # condition matched, should find a better method
        condition = condition[0]
    else:
        comment = condition.comment

    # abort if it's an alert and we've already alerted on this item
    if condition.action == 'alert':
        try:
            ActionLog.query.filter(
                and_(ActionLog.permalink == get_permalink(item),
                     ActionLog.action == 'alert')).one()
            return
        except NoResultFound:
            pass

    # perform the action
    if condition.action == 'remove':
        item.remove(condition.spam)
    elif condition.action == 'approve':
        item.approve()
    elif condition.action == 'set_flair':
        item.set_flair(condition.set_flair_text,
                       condition.set_flair_class)

    # deliver the comment if set
    if comment:
        if condition.comment_method == 'comment':
            post_comment(item, comment+disclaimer)
        elif condition.comment_method == 'modmail':
            r.compose_message('#'+subreddit.name,
                              'AutoModerator condition matched',
                              get_permalink(item)+'\n\n'+comment)
        elif condition.comment_method == 'message':
            r.compose_message(item.author.name,
                              'AutoModerator condition matched',
                              get_permalink(item)+'\n\n'+comment+disclaimer)

    # log the action taken
    action_log = ActionLog()
    action_log.subreddit_id = subreddit.id
    action_log.user = item.author.name
    action_log.permalink = get_permalink(item)
    action_log.created_utc = datetime.utcfromtimestamp(item.created_utc)
    action_log.action_time = datetime.utcnow()
    action_log.action = condition.action
    action_log.matched_condition = condition.id

    if isinstance(item, reddit.objects.Submission):
        action_log.title = item.title
        action_log.url = item.url
        action_log.domain = item.domain
        logging.info('  /r/%s: %s submission "%s"',
                        subreddit.name,
                        condition.action,
                        item.title.encode('ascii', 'ignore'))
    elif isinstance(item, reddit.objects.Comment):
        logging.info('  /r/%s: %s comment by user %s',
                        subreddit.name,
                        condition.action,
                        item.author.name)

    db.session.add(action_log)
    db.session.commit()
Esempio n. 2
0
def perform_action(subreddit, item, condition):
    """Performs the action for the condition(s).
    
    Also delivers the comment (if set) and creates an ActionLog entry.
    """

    global r
    comment = None
    disclaimer = ('\n\n*I am a bot, and this action was performed '
                  'automatically. Please [contact the moderators of this '
                  'subreddit](http://www.reddit.com/message/compose?'
                  'to=%23' + item.subreddit.display_name + ') if you have any '
                  'questions or concerns.*')

    # build the comment if multiple conditions were matched
    if isinstance(condition, list):
        # build short reason string for modporn submission later
        short_reason = ' '.join([
            '[' + c.short_reason + ']' for c in condition
            if c.short_reason is not None
        ])

        if any([c.comment for c in condition]):
            if condition[0].action == 'alert':
                verb = 'alerted'
            else:
                verb = condition[0].action + 'd'

            comment = ('Unfortunately, your submission has been ' + verb +
                       ' for the following reasons:\n\n')
            for c in condition:
                if c.comment:
                    comment += '* ' + c.comment + '\n'
            comment += ('\nFor more information regarding these issues please '
                        ' [see the FAQ](http://www.reddit.com/r/' +
                        item.subreddit.display_name + '/faq), and feel '
                        'free to resubmit once they have been resolved. '
                        'Thank you!')

        # bit of a hack and only logs and uses attributes from first
        # condition matched, should find a better method
        condition = condition[0]
    else:
        short_reason = condition.short_reason
        comment = condition.comment

    # abort if it's an alert and we've already alerted on this item
    if condition.action == 'alert':
        try:
            ActionLog.query.filter(
                and_(ActionLog.permalink == get_permalink(item),
                     ActionLog.action == 'alert')).one()
            return
        except NoResultFound:
            pass

    # perform the action
    if condition.action == 'remove':
        item.remove(condition.spam)
    elif condition.action == 'approve':
        item.approve()
    elif condition.action == 'set_flair':
        item.set_flair(condition.set_flair_text, condition.set_flair_class)

    # deliver the comment if set
    if comment:
        if condition.comment_method == 'comment':
            post_comment(item, comment + disclaimer)
        elif condition.comment_method == 'modmail':
            r.compose_message('#' + subreddit.name,
                              'AutoModerator condition matched',
                              get_permalink(item) + '\n\n' + comment)
        elif condition.comment_method == 'message':
            r.compose_message(
                item.author.name, 'AutoModerator condition matched',
                get_permalink(item) + '\n\n' + comment + disclaimer)

    # log the action taken
    action_log = ActionLog()
    action_log.subreddit_id = subreddit.id
    action_log.user = item.author.name
    action_log.permalink = get_permalink(item)
    action_log.created_utc = datetime.utcfromtimestamp(item.created_utc)
    action_log.action_time = datetime.utcnow()
    action_log.action = condition.action
    action_log.matched_condition = condition.id

    if isinstance(item, reddit.objects.Submission):
        action_log.title = item.title
        action_log.url = item.url
        action_log.domain = item.domain
        #         logging.info('  /r/%s: %s submission "%s"',
        #                         subreddit.name,
        #                         condition.action,
        #                         item.title.encode('ascii', 'ignore'))
        logging.info('  /r/%s: %s submission "%s"', subreddit.name,
                     condition.action, item.title)
    elif isinstance(item, reddit.objects.Comment):
        logging.info('  /r/%s: %s comment by user %s', subreddit.name,
                     condition.action, item.author.name)

    db.session.add(action_log)
    db.session.commit()

    # SFWPorn network moderation requirements
    # NO LONGER NEEDED
    #     if (isinstance(item, reddit.objects.Submission) and
    #             condition.action == 'remove' and
    #             short_reason is not None):
    #         # submit to ModerationPorn
    #         r.submit('ModerationPorn',
    #                 '['+subreddit.name+'] '+
    #                 '['+item.author.name+'] '+
    #                 item.title+
    #                 ' '+short_reason,
    #                 url=item.permalink)

    if (isinstance(item, reddit.objects.Submission)
            and condition.action == 'remove' and comment):
        # send them a PM as well
        r.compose_message(item.author.name, 'SFWPorn submission removed',
                          get_permalink(item) + '\n\n' + comment + disclaimer)