def perform_actions(self, item, data):
        """Execute the defined actions on the item."""
        # only approve if it's currently removed or reported
        should_approve = item._spam or (self.reports and item.reported)
        if self.action == "approve" and should_approve:
            approvable_author = not data["author"]._spam or self.approve_banned
            if approvable_author:
                # TODO: shouldn't need to set train_spam/insert values
                admintools.unspam(
                    item, moderator_unbanned=True, unbanner=ACCOUNT.name, train_spam=True, insert=item._spam
                )

                log_action = None
                if isinstance(item, Link):
                    log_action = "approvelink"
                elif isinstance(item, Comment):
                    log_action = "approvecomment"

                if log_action:
                    ModAction.create(data["subreddit"], ACCOUNT, log_action, target=item, details="unspam")

                g.stats.simple_event("automoderator.approve")

        if self.action in {"remove", "spam"}:
            spam = self.action == "spam"
            admintools.spam(item, auto=False, moderator_banned=True, banner=ACCOUNT.name, train_spam=spam)

            # TODO: shouldn't need to do all of this here
            modified_thing = None
            log_action = None
            if isinstance(item, Link):
                modified_thing = item
                log_action = "removelink"
            elif isinstance(item, Comment):
                modified_thing = data["link"]
                log_action = "removecomment"
                queries.unnotify(item)

            if modified_thing:
                set_last_modified(modified_thing, "comments")
                LastModified.touch(modified_thing._fullname, "Comments")

            if log_action:
                log_details = "spam" if spam else "remove"
                ModAction.create(data["subreddit"], ACCOUNT, log_action, target=item, details=log_details)

            g.stats.simple_event("automoderator.%s" % self.action)

        if self.action == "report":
            if self.report_reason:
                reason = replace_placeholders(self.report_reason, data, self.parent.matches)
            else:
                reason = None
            Report.new(ACCOUNT, item, reason)
            admintools.report(item)

            g.stats.simple_event("automoderator.report")

        if self.set_nsfw is not None:
            if item.over_18 != self.set_nsfw:
                item.over_18 = self.set_nsfw
                item._commit()
                # TODO: shouldn't need to do this here
                log_details = None
                if not self.set_nsfw:
                    log_details = "remove"
                ModAction.create(data["subreddit"], ACCOUNT, "marknsfw", target=item, details=log_details)
                item.update_search_index()

        if self.set_contest_mode is not None:
            if item.contest_mode != self.set_contest_mode:
                item.contest_mode = self.set_contest_mode
                item._commit()

        if self.set_sticky is not None:
            already_stickied = data["subreddit"].sticky_fullname == item._fullname
            if already_stickied != self.set_sticky:
                if not self.set_sticky:
                    data["subreddit"].sticky_fullname = None
                else:
                    data["subreddit"].sticky_fullname = item._fullname
                data["subreddit"]._commit()

                # TODO: shouldn't need to do this here
                if self.set_sticky:
                    log_action = "sticky"
                else:
                    log_action = "unsticky"
                ModAction.create(data["subreddit"], ACCOUNT, log_action, target=item)

        if self.set_flair:
            # don't overwrite existing flair unless that was specified
            can_update_flair = False
            if isinstance(item, Link):
                if item.flair_text or item.flair_css_class:
                    can_update_flair = self.overwrite_flair
                else:
                    can_update_flair = True
            elif isinstance(item, Account):
                if data["subreddit"].is_flair(item):
                    can_update_flair = self.overwrite_flair
                else:
                    can_update_flair = True

            if can_update_flair:
                text = replace_placeholders(self.set_flair["text"], data, self.parent.matches)
                cls = replace_placeholders(self.set_flair["class"], data, self.parent.matches)

                # apply same limits as API to text and class
                text = text[:64]
                cls = re.sub(r"[^\w -]", "", cls)
                classes = cls.split()[:10]
                classes = [cls[:100] for cls in classes]
                cls = " ".join(classes)

                if isinstance(item, Link):
                    item.set_flair(text, cls)
                elif isinstance(item, Account):
                    item.set_flair(data["subreddit"], text, cls)

                g.stats.simple_event("automoderator.set_flair")
Exemple #2
0
 def acquit(self, details=''):
     admintools.unspam(self.defendant,
                       unbanner="deputy moderation" + details)
Exemple #3
0
 def acquit(self, details = ''):
     admintools.unspam(self.defendant, unbanner="deputy moderation" + details)
Exemple #4
0
def insert(title, sr_name, url, description, date, author='ArxivBot', cross_srs=[]):
    a = Account._by_name(author)
    sr = subreddit_or_create(sr_name, a)
    srs = [subreddit_or_create(sr_name, a) for sr_name in cross_srs]
    ups = 0
    if author=='AnnalsBot':
        ups = 1
    downs = 0
    if False:
        try:
            ls = Link._by_url(url, None)
            print 'Found %d links' % len(ls)
            for l in ls:
                if l.author_id == a._id and l.sr_id != sr._id:
                    ups = ups + l._ups - 1
                    downs = downs + l._downs
                    l._deleted=True
                    l._commit()
                    changed(l)
                    x = l.subreddit_slow
                    queries.delete_links(l)
                    print 'Deleting ' + str(l)
                else:
                    print 'Not deleting ' + str(l)
            print 'Seed votes %s %s' % (ups, downs)
        except NotFound:
            pass


    try:
        l = Link._by_url(url, sr)
        print "!! Link already exists"
        return l
    except NotFound:
        print "Submitting link"



    user = a
    l = Link(_ups = ups,
            _downs = downs,
            title = title,
            url = url,
            _spam = False,
            author_id = user._id,
            sr_id = sr._id,
            lang = sr.lang,
            ip = '127.0.0.1',
            multi_sr_id = [sr._id]+[sr._id for sr in srs],
            selftext = description)
    l.verdict = 'admin-approved'
    l.approval_checkmark = _("auto-approved")
    l._date = datetime(date.year,date.month,date.day,tzinfo=g.tz)
    l.selftext = description
    l._commit()
    #for cross_sr in cross_srs:
    #  LinkSR(l, subreddit_or_create(cross_sr, a), 'crosspost')._commit()
    l.set_url_cache()
    vote = None
    if author == 'AnnalsBot':
      vote = True
    queries.queue_vote(user, l, vote, '127.0.0.1')
    queries.new_savehide(l._save(user))

    queries.new_link(l)
    changed(l)

    queries.worker.join()
    
    end_trial(l, "admin-approved")
    admintools.unspam(l, user.name)
    ModAction.create(sr, user, 'approvelink', target=l)