def POST_unmute_participant(self, conversation): if conversation.is_internal or conversation.is_auto: return self.send_error(400, errors.CANT_RESTRICT_MODERATOR) sr = Subreddit._by_fullname(conversation.owner_fullname) try: participant = conversation.get_participant_account() except NotFound: abort(404, errors.USER_DOESNT_EXIST) if not c.user_is_admin: if not sr.is_moderator_with_perms(c.user, 'access', 'mail'): return self.send_error(403, errors.INVALID_MOD_PERMISSIONS) removed = sr.remove_muted(participant) if not removed: return simplejson.dumps( self._convo_to_serializable(conversation, all_messages=True)) MutedAccountsBySubreddit.unmute(sr, participant) ModAction.create(sr, c.user, 'unmuteuser', target=participant) conversation.add_action(c.user, 'unmuted', commit=True) result = self._get_updated_convo(conversation.id, c.user) result['user'] = self._get_modmail_userinfo(conversation, sr=sr) return simplejson.dumps(result)
def POST_mute_participant(self, conversation): if conversation.is_internal or conversation.is_auto: return self.send_error(400, errors.CANT_RESTRICT_MODERATOR) sr = Subreddit._by_fullname(conversation.owner_fullname) try: participant = conversation.get_participant_account() except NotFound: return self.send_error(404, errors.USER_DOESNT_EXIST) if not sr.can_mute(c.user, participant): return self.send_error(400, errors.CANT_RESTRICT_MODERATOR) if not c.user_is_admin: if not sr.is_moderator_with_perms(c.user, 'access', 'mail'): return self.send_error(403, errors.INVALID_MOD_PERMISSIONS) if sr.use_quotas: sr_ratelimit = SimpleRateLimit( name="sr_muted_%s" % sr._id36, seconds=g.sr_quota_time, limit=g.sr_muted_quota, ) if not sr_ratelimit.record_and_check(): return self.send_error(403, errors.SUBREDDIT_RATELIMIT) # Add the mute record but only if successful create the # appropriate notifications, this prevents duplicate # notifications from being sent added = sr.add_muted(participant) if not added: return simplejson.dumps( self._convo_to_serializable(conversation, all_messages=True)) MutedAccountsBySubreddit.mute(sr, participant, c.user) permalink = conversation.make_permalink() # Create the appropriate objects to be displayed on the # mute moderation log, use the permalink to the new modmail # system ModAction.create(sr, c.user, 'muteuser', target=participant, description=permalink) sr.add_rel_note('muted', participant, permalink) # Add the muted mod action to the conversation conversation.add_action(c.user, 'muted', commit=True) result = self._get_updated_convo(conversation.id, c.user) result['user'] = self._get_modmail_userinfo(conversation, sr=sr) return simplejson.dumps(result)
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 = "" self.stylesheet_modified = None else: self.stylesheet_hash = hashlib.md5(minified).hexdigest() self.stylesheet_contents = minified self.stylesheet_modified = datetime.datetime.now(g.tz) else: self.stylesheet_contents = "" self.stylesheet_hash = "" self.stylesheet_modified = datetime.datetime.now(g.tz) 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
def backfill_sr(sr): print "processing %s" % sr.name after = None count = 100 q = ModAction.get_actions(sr, after=after, count=count) actions = list(q) while actions: for ma in actions: ModActionBySRActionMod.add_object(ma) q = ModAction.get_actions(sr, after=actions[-1], count=count) actions = list(q)
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
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 = "" self.stylesheet_modified = None else: self.stylesheet_hash = hashlib.md5(minified).hexdigest() self.stylesheet_contents = minified self.stylesheet_modified = datetime.datetime.now(g.tz) else: self.stylesheet_contents = "" self.stylesheet_hash = "" self.stylesheet_modified = datetime.datetime.now(g.tz) 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
def get_modactions(cls, srs, mod=None, action=None): # Get a query that will yield ModAction objects with mod and action from r2.models import ModAction return ModAction.get_actions(srs, mod=mod, action=action)
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")