blob: fd2804572e1f8073bb890537f43b4b10b25977af [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2016 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file or at
4# https://developers.google.com/open-source/licenses/bsd
5
6"""Classes for banning spammer users"""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
Copybara854996b2021-09-07 19:36:02 +000011import json
12import time
13
14from framework import cloud_tasks_helpers
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020015from framework import flaskservlet
Copybara854996b2021-09-07 19:36:02 +000016from framework import framework_helpers
17from framework import permissions
18from framework import jsonfeed
Copybara854996b2021-09-07 19:36:02 +000019from framework import urls
20
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020021
22class BanSpammer(flaskservlet.FlaskServlet):
Copybara854996b2021-09-07 19:36:02 +000023 """Ban a user and mark their content as spam"""
24
25 def AssertBasePermission(self, mr):
26 """Check whether the user has any permission to ban users.
27
28 Args:
29 mr: commonly used info parsed from the request.
30 """
31 super(BanSpammer, self).AssertBasePermission(mr)
32 if not permissions.CanBan(mr, self.services):
33 raise permissions.PermissionException(
34 'User is not allowed to ban users.')
35
36 def ProcessFormData(self, mr, post_data):
37 self.AssertBasePermission(mr)
38 viewed_user_id = mr.viewed_user_auth.user_pb.user_id
39 reporter_id = mr.auth.user_id
40
41 # First ban or un-ban the user as a spammer.
42 framework_helpers.UserSettings.ProcessBanForm(
43 mr.cnxn, self.services.user, post_data, mr.viewed_user_auth.user_id,
44 mr.viewed_user_auth.user_pb)
45
46 params = {
47 'spammer_id': viewed_user_id,
48 'reporter_id': reporter_id,
49 'is_spammer': 'banned' in post_data
50 }
51 task = cloud_tasks_helpers.generate_simple_task(
52 urls.BAN_SPAMMER_TASK + '.do', params)
53 cloud_tasks_helpers.create_task(task)
54
55 return framework_helpers.FormatAbsoluteURL(
56 mr, mr.viewed_user_auth.user_view.profile_url, include_project=False,
57 saved=1, ts=int(time.time()))
58
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020059 def PostBanSpammerPage(self, **kwargs):
60 return self.handler(**kwargs)
Copybara854996b2021-09-07 19:36:02 +000061
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020062
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020063class BanSpammerTask(jsonfeed.FlaskInternalTask):
Copybara854996b2021-09-07 19:36:02 +000064 """This task will update all of the comments and issues created by the
65 target user with is_spam=True, and also add a manual verdict attached
66 to the user who originated the ban request. This is a potentially long
67 running operation, so it is implemented as an async task.
68 """
69
70 def HandleRequest(self, mr):
71 spammer_id = mr.GetPositiveIntParam('spammer_id')
72 reporter_id = mr.GetPositiveIntParam('reporter_id')
73 is_spammer = mr.GetBoolParam('is_spammer')
74
75 # Get all of the issues reported by the spammer.
76 issue_ids = self.services.issue.GetIssueIDsReportedByUser(mr.cnxn,
77 spammer_id)
78
79 issues = []
80
81 if len(issue_ids) > 0:
82 issues = self.services.issue.GetIssues(
83 mr.cnxn, issue_ids, use_cache=False)
84
85 # Mark them as spam/ham in bulk.
86 self.services.spam.RecordManualIssueVerdicts(mr.cnxn, self.services.issue,
87 issues, reporter_id, is_spammer)
88
89 # Get all of the comments
90 comments = self.services.issue.GetCommentsByUser(mr.cnxn, spammer_id)
91
92 for comment in comments:
93 self.services.spam.RecordManualCommentVerdict(mr.cnxn,
94 self.services.issue, self.services.user, comment.id,
95 reporter_id, is_spammer)
96
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020097 return json.dumps({
98 'comments': len(comments),
99 'issues': len(issues),
Copybara854996b2021-09-07 19:36:02 +0000100 })
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +0200101
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +0200102 def PostBanSpammer(self, **kwargs):
103 return self.handler(**kwargs)