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