Example #1
0
def add_comment_tree(comments):
    # update the comment cache
    add_comments(comments)
    # update last modified
    links = Link._byID(list(set(com.link_id for com in tup(comments))), data=True, return_dict=False)
    for link in links:
        set_last_modified(link, "comments")
Example #2
0
def add_comment_tree(comments):
    #update the comment cache
    add_comments(comments)
    #update last modified
    links = Link._byID(list(set(com.link_id for com in tup(comments))),
                       data = True, return_dict = False)
    for link in links:
        set_last_modified(link, 'comments')
Example #3
0
def handle_vote(user,
                thing,
                dir,
                ip,
                organic,
                cheater=False,
                foreground=False,
                timer=None):
    if timer is None:
        timer = SimpleSillyStub()

    from r2.lib.db import tdb_sql
    from sqlalchemy.exc import IntegrityError
    try:
        v = Vote.vote(user,
                      thing,
                      dir,
                      ip,
                      organic,
                      cheater=cheater,
                      timer=timer)
    except (tdb_sql.CreationError, IntegrityError):
        g.log.error("duplicate vote for: %s" % str((user, thing, dir)))
        return

    timestamps = []
    if isinstance(thing, Link):
        new_vote(v, foreground=foreground, timer=timer)

        #update the modified flags
        if user._id == thing.author_id:
            timestamps.append('Overview')
            timestamps.append('Submitted')
            #update sup listings
            sup.add_update(user, 'submitted')

            #update sup listings
            if dir:
                sup.add_update(user, 'liked')
            elif dir is False:
                sup.add_update(user, 'disliked')

    elif isinstance(thing, Comment):
        #update last modified
        if user._id == thing.author_id:
            timestamps.append('Overview')
            timestamps.append('Commented')
            #update sup listings
            sup.add_update(user, 'commented')

    timer.intermediate("sup")

    for timestamp in timestamps:
        set_last_modified(user, timestamp.lower())
    LastModified.touch(user._fullname, timestamps)
    timer.intermediate("last_modified")
Example #4
0
    def change_css(self,
                   content,
                   parsed,
                   prev=None,
                   reason=None,
                   author=None,
                   force=False):
        from r2.models import ModAction
        author = author if author else c.user.name
        if content is None:
            content = ''
        try:
            wiki = WikiPage.get(self, 'config/stylesheet')
        except tdb_cassandra.NotFound:
            wiki = WikiPage.create(self, 'config/stylesheet')
        wr = wiki.revise(content,
                         previous=prev,
                         author=author,
                         reason=reason,
                         force=force)

        minified = cssmin(parsed)
        if minified:
            if g.static_stylesheet_bucket:
                digest = hashlib.sha1(minified).digest()
                self.stylesheet_hash = (
                    base64.urlsafe_b64encode(digest).rstrip("="))

                s3cp.send_file(
                    g.static_stylesheet_bucket,
                    self.static_stylesheet_name,
                    minified,
                    content_type="text/css",
                    never_expire=True,
                    replace=False,
                )

                self.stylesheet_contents = ""
            else:
                self.stylesheet_hash = hashlib.md5(minified).hexdigest()
                self.stylesheet_contents = minified
                set_last_modified(self, 'stylesheet_contents')
        else:
            self.stylesheet_contents = ""
            self.stylesheet_hash = ""
        self.stylesheet_contents_user = ""  # reads from wiki; ensure pg clean
        self._commit()

        ModAction.create(self,
                         c.user,
                         action='wikirevise',
                         details='Updated subreddit stylesheet')
        return wr
Example #5
0
 def change_css(self, content, parsed, prev=None, reason=None, author=None, force=False):
     from r2.models import ModAction
     author = author if author else c.user.name
     if content is None:
         content = ''
     try:
         wiki = WikiPage.get(self, 'config/stylesheet')
     except tdb_cassandra.NotFound:
         wiki = WikiPage.create(self, 'config/stylesheet')
     wr = wiki.revise(content, previous=prev, author=author, reason=reason, force=force)
     self.stylesheet_contents = parsed
     self.stylesheet_hash = md5(parsed).hexdigest()
     set_last_modified(self, 'stylesheet_contents')
     c.site._commit()
     ModAction.create(self, c.user, action='wikirevise', details='Updated subreddit stylesheet')
     return wr
Example #6
0
 def change_css(self, content, parsed, prev=None, reason=None, author=None, force=False):
     from r2.models import ModAction
     author = author if author else c.user.name
     if content is None:
         content = ''
     try:
         wiki = WikiPage.get(self, 'config/stylesheet')
     except tdb_cassandra.NotFound:
         wiki = WikiPage.create(self, 'config/stylesheet')
     wr = wiki.revise(content, previous=prev, author=author, reason=reason, force=force)
     self.stylesheet_contents = parsed
     self.stylesheet_hash = md5(parsed).hexdigest()
     set_last_modified(self, 'stylesheet_contents')
     c.site._commit()
     ModAction.create(self, c.user, action='wikirevise', details='Updated subreddit stylesheet')
     return wr
Example #7
0
def handle_vote(user,
                thing,
                dir,
                ip,
                organic,
                cheater=False,
                foreground=False):
    from r2.lib.db import tdb_sql
    from sqlalchemy.exc import IntegrityError
    try:
        v = Vote.vote(user, thing, dir, ip, organic, cheater=cheater)
    except (tdb_sql.CreationError, IntegrityError):
        g.log.error("duplicate vote for: %s" % str((user, thing, dir)))
        return

    # keep track of upvotes in the hard cache by subreddit
    #sr_id = getattr(thing, "sr_id", None)
    #if (sr_id and dir > 0 and getattr(thing, "author_id", None) != user._id
    #    and v.valid_thing):
    #    now = datetime.now(g.tz).strftime("%Y/%m/%d")
    #    g.hardcache.add("subreddit_vote-%s_%s_%s" % (now, sr_id, user._id),
    #                    sr_id, time = 86400 * 7) # 1 week for now

    if isinstance(thing, Link):
        new_vote(v, foreground=foreground)

        #update the modified flags
        set_last_modified(user, 'liked')
        if user._id == thing.author_id:
            set_last_modified(user, 'overview')
            set_last_modified(user, 'submitted')
            #update sup listings
            sup.add_update(user, 'submitted')

            #update sup listings
            if dir:
                sup.add_update(user, 'liked')
            elif dir is False:
                sup.add_update(user, 'disliked')

    elif isinstance(thing, Comment):
        #update last modified
        if user._id == thing.author_id:
            set_last_modified(user, 'overview')
            set_last_modified(user, 'commented')
            #update sup listings
            sup.add_update(user, 'commented')
Example #8
0
def handle_vote(user, thing, dir, ip, organic,
                cheater=False, foreground=False, timer=None):
    if timer is None:
        timer = SimpleSillyStub()

    from r2.lib.db import tdb_sql
    from sqlalchemy.exc import IntegrityError
    try:
        v = Vote.vote(user, thing, dir, ip, organic, cheater = cheater,
                      timer=timer)
    except (tdb_sql.CreationError, IntegrityError):
        g.log.error("duplicate vote for: %s" % str((user, thing, dir)))
        return

    timestamps = []
    if isinstance(thing, Link):
        new_vote(v, foreground=foreground, timer=timer)

        #update the modified flags
        if user._id == thing.author_id:
            timestamps.append('Overview')
            timestamps.append('Submitted')
            #update sup listings
            sup.add_update(user, 'submitted')

            #update sup listings
            if dir:
                sup.add_update(user, 'liked')
            elif dir is False:
                sup.add_update(user, 'disliked')

    elif isinstance(thing, Comment):
        #update last modified
        if user._id == thing.author_id:
            timestamps.append('Overview')
            timestamps.append('Commented')
            #update sup listings
            sup.add_update(user, 'commented')

    timer.intermediate("sup")

    for timestamp in timestamps:
        set_last_modified(user, timestamp.lower())
    LastModified.touch(user._fullname, timestamps)
    timer.intermediate("last_modified")
Example #9
0
def handle_vote(user, thing, dir, ip, organic, cheater = False):
    from r2.lib.db import tdb_sql
    from sqlalchemy.exc import IntegrityError
    try:
        v = Vote.vote(user, thing, dir, ip, organic, cheater = cheater)
    except (tdb_sql.CreationError, IntegrityError):
        g.log.error("duplicate vote for: %s" % str((user, thing, dir)))
        return

    # keep track of upvotes in the hard cache by subreddit
    sr_id = getattr(thing, "sr_id", None)
    if (sr_id and dir > 0 and getattr(thing, "author_id", None) != user._id
        and v.valid_thing):
        now = datetime.now(g.tz).strftime("%Y/%m/%d")
        g.hardcache.add("subreddit_vote-%s_%s_%s" % (now, sr_id, user._id),
                        sr_id, time = 86400 * 7) # 1 week for now

    if isinstance(thing, Link):
        new_vote(v)
        if v.valid_thing:
            expire_hot(thing.subreddit_slow)

        #update the modified flags
        set_last_modified(user, 'liked')
        if user._id == thing.author_id:
            set_last_modified(user, 'overview')
            set_last_modified(user, 'submitted')
            #update sup listings
            sup.add_update(user, 'submitted')

            #update sup listings
            if dir:
                sup.add_update(user, 'liked')
            elif dir is False:
                sup.add_update(user, 'disliked')

    elif isinstance(thing, Comment):
        #update last modified
        if user._id == thing.author_id:
            set_last_modified(user, 'overview')
            set_last_modified(user, 'commented')
            #update sup listings
            sup.add_update(user, 'commented')
Example #10
0
    def change_css(self, content, parsed, prev=None, reason=None, author=None, force=False):
        from r2.models import ModAction

        author = author if author else c.user.name
        if content is None:
            content = ""
        try:
            wiki = WikiPage.get(self, "config/stylesheet")
        except tdb_cassandra.NotFound:
            wiki = WikiPage.create(self, "config/stylesheet")
        wr = wiki.revise(content, previous=prev, author=author, reason=reason, force=force)

        minified = cssmin(parsed)
        if minified:
            if g.static_stylesheet_bucket:
                digest = hashlib.sha1(minified).digest()
                self.stylesheet_hash = base64.urlsafe_b64encode(digest).rstrip("=")

                s3cp.send_file(
                    g.static_stylesheet_bucket,
                    self.static_stylesheet_name,
                    minified,
                    content_type="text/css",
                    never_expire=True,
                    replace=False,
                )

                self.stylesheet_contents = ""
            else:
                self.stylesheet_hash = hashlib.md5(minified).hexdigest()
                self.stylesheet_contents = minified
                set_last_modified(self, "stylesheet_contents")
        else:
            self.stylesheet_contents = ""
            self.stylesheet_hash = ""
        self.stylesheet_contents_user = ""  # reads from wiki; ensure pg clean
        self._commit()

        ModAction.create(self, c.user, action="wikirevise", details="Updated subreddit stylesheet")
        return wr
Example #11
0
    def _new(cls, author, link, parent, body, ip, spam = False, date = None):
        comment = Comment(body = body,
                          link_id = link._id,
                          sr_id = link.sr_id,
                          author_id = author._id,
                          ip = ip,
                          date = date)

        comment._spam = spam

        #these props aren't relations
        if parent:
            comment.parent_id = parent._id

        comment._commit()

        link._incr('num_comments', 1)

        inbox_rel = None
        if parent:
            to = Account._byID(parent.author_id)
            # only global admins can be message spammed.
            if not comment._spam or to.name in g.admins:
                inbox_rel = Inbox._add(to, comment, 'inbox')

        #clear that chache
        clear_memo('builder.link_comments2', link._id)

        # flag search indexer that something has changed
        tc.changed(comment)

        #update last modified
        set_last_modified(author, 'overview')
        set_last_modified(author, 'commented')
        set_last_modified(link, 'comments')

        #update the comment cache
        from r2.lib.comment_tree import add_comment
        add_comment(comment)

        return (comment, inbox_rel)
Example #12
0
    def _new(cls, author, link, parent, body, ip, spam=False, date=None):
        comment = Comment(body=body,
                          link_id=link._id,
                          sr_id=link.sr_id,
                          author_id=author._id,
                          ip=ip,
                          date=date)

        comment._spam = spam

        #these props aren't relations
        if parent:
            comment.parent_id = parent._id

        comment._commit()

        link._incr('num_comments', 1)

        inbox_rel = None
        if parent:
            to = Account._byID(parent.author_id)
            # only global admins can be message spammed.
            if not comment._spam or to.name in g.admins:
                inbox_rel = Inbox._add(to, comment, 'inbox')

        #clear that chache
        clear_memo('builder.link_comments2', link._id)

        # flag search indexer that something has changed
        tc.changed(comment)

        #update last modified
        set_last_modified(author, 'overview')
        set_last_modified(author, 'commented')
        set_last_modified(link, 'comments')

        #update the comment cache
        from r2.lib.comment_tree import add_comment
        add_comment(comment)

        return (comment, inbox_rel)
Example #13
0
    def _new(cls, author, link, parent, body, ip, spam=False, date=None):
        comment = Comment(body=body,
                          link_id=link._id,
                          sr_id=link.sr_id,
                          author_id=author._id,
                          ip=ip,
                          date=date)

        comment._spam = spam

        #these props aren't relations
        if parent:
            comment.parent_id = parent._id

        comment._commit()

        link._incr('num_comments', 1)

        inbox_rel = comment._send_post_notifications(link, comment, parent)

        #clear that chache
        clear_memo('builder.link_comments2', link._id)

        # flag search indexer that something has changed
        tc.changed(comment)

        #update last modified
        set_last_modified(author, 'overview')
        set_last_modified(author, 'commented')
        set_last_modified(link, 'comments')

        #update the comment cache
        from r2.lib.comment_tree import add_comment
        add_comment(comment)

        return (comment, inbox_rel)
Example #14
0
    def _new(cls, author, link, parent, body, ip, spam = False, date = None):
        comment = Comment(body = body,
                          link_id = link._id,
                          sr_id = link.sr_id,
                          author_id = author._id,
                          ip = ip,
                          date = date)

        comment._spam = spam

        #these props aren't relations
        if parent:
            comment.parent_id = parent._id

        comment._commit()

        link._incr('num_comments', 1)

        inbox_rel = comment._send_post_notifications(link, comment, parent)

        #clear that chache
        clear_memo('builder.link_comments2', link._id)

        # flag search indexer that something has changed
        tc.changed(comment)

        #update last modified
        set_last_modified(author, 'overview')
        set_last_modified(author, 'commented')
        set_last_modified(link, 'comments')

        #update the comment cache
        from r2.lib.comment_tree import add_comment
        add_comment(comment)

        return (comment, inbox_rel)
Example #15
0
def handle_vote(user, thing, dir, ip, organic, cheater=False, foreground=False):
    from r2.lib.db import tdb_sql
    from sqlalchemy.exc import IntegrityError
    try:
        v = Vote.vote(user, thing, dir, ip, organic, cheater = cheater)
    except (tdb_sql.CreationError, IntegrityError):
        g.log.error("duplicate vote for: %s" % str((user, thing, dir)))
        return

    if isinstance(thing, Link):
        new_vote(v, foreground=foreground)

        #update the modified flags
        if user._id == thing.author_id:
            set_last_modified(user, 'overview')
            set_last_modified(user, 'submitted')
            #update sup listings
            sup.add_update(user, 'submitted')

            #update sup listings
            if dir:
                set_last_modified(user, 'liked')
                sup.add_update(user, 'liked')
            elif dir is False:
                set_last_modified(user, 'disliked')
                sup.add_update(user, 'disliked')

    elif isinstance(thing, Comment):
        #update last modified
        if user._id == thing.author_id:
            set_last_modified(user, 'overview')
            set_last_modified(user, 'commented')
            #update sup listings
            sup.add_update(user, 'commented')
Example #16
0
 def add_comments(self, comments):
     impl = self.IMPLEMENTATIONS[self.link.comment_tree_version]
     impl.add_comments(self, comments)
     utils.set_last_modified(self.link, 'comments')
     LastModified.touch(self.link._fullname, 'Comments')
Example #17
0
def add_comment_tree(comment, link):
    #update the comment cache
    add_comment(comment)
    #update last modified
    set_last_modified(link, 'comments')
Example #18
0
 def add_comments(self, comments):
     impl = self.IMPLEMENTATIONS[self.link.comment_tree_version]
     impl.add_comments(self, comments)
     utils.set_last_modified(self.link, 'comments')
     LastModified.touch(self.link._fullname, 'Comments')
Example #19
0
def add_comment_tree(comment, link):
    # update the comment cache
    add_comment(comment)
    # update last modified
    set_last_modified(link, "comments")
Example #20
0
    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")
Example #21
0
def handle_vote(user,
                thing,
                dir,
                ip,
                organic,
                cheater=False,
                foreground=False):
    from r2.lib.db import tdb_sql
    from sqlalchemy.exc import IntegrityError
    try:
        v = Vote.vote(user, thing, dir, ip, organic, cheater=cheater)
    except (tdb_sql.CreationError, IntegrityError):
        g.log.error("duplicate vote for: %s" % str((user, thing, dir)))
        return

    if isinstance(thing, Link):
        new_vote(v, foreground=foreground)

        #update the modified flags
        if user._id == thing.author_id:
            set_last_modified(user, 'overview')
            set_last_modified(user, 'submitted')
            #update sup listings
            sup.add_update(user, 'submitted')

            #update sup listings
            if dir:
                set_last_modified(user, 'liked')
                sup.add_update(user, 'liked')
            elif dir is False:
                set_last_modified(user, 'disliked')
                sup.add_update(user, 'disliked')

    elif isinstance(thing, Comment):
        #update last modified
        if user._id == thing.author_id:
            set_last_modified(user, 'overview')
            set_last_modified(user, 'commented')
            #update sup listings
            sup.add_update(user, 'commented')