Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | # 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. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | """Classes for banning spammer users""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 10 | import json |
| 11 | import time |
| 12 | |
| 13 | from framework import cloud_tasks_helpers |
| 14 | from framework import framework_helpers |
| 15 | from framework import permissions |
| 16 | from framework import jsonfeed |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 17 | from framework import servlet |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 18 | from framework import urls |
| 19 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 20 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 21 | class BanSpammer(servlet.Servlet): |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 22 | """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ínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 58 | def PostBanSpammerPage(self, **kwargs): |
| 59 | return self.handler(**kwargs) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 60 | |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 61 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 62 | class BanSpammerTask(jsonfeed.InternalTask): |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 63 | """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ínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 96 | return json.dumps({ |
| 97 | 'comments': len(comments), |
| 98 | 'issues': len(issues), |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 99 | }) |
Adrià Vilanova Martínez | de94280 | 2022-07-15 14:06:55 +0200 | [diff] [blame] | 100 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 101 | def PostBanSpammer(self, **kwargs): |
| 102 | return self.handler(**kwargs) |