blob: a6be31189a60d4a479f3546c543dbd3ccf9557db [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
11import logging
12import json
13import time
14
15from framework import cloud_tasks_helpers
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020016from framework import flaskservlet
Copybara854996b2021-09-07 19:36:02 +000017from framework import framework_helpers
18from framework import permissions
19from framework import jsonfeed
20from framework import servlet
21from framework import urls
22
23class BanSpammer(servlet.Servlet):
24 """Ban a user and mark their content as spam"""
25
26 def AssertBasePermission(self, mr):
27 """Check whether the user has any permission to ban users.
28
29 Args:
30 mr: commonly used info parsed from the request.
31 """
32 super(BanSpammer, self).AssertBasePermission(mr)
33 if not permissions.CanBan(mr, self.services):
34 raise permissions.PermissionException(
35 'User is not allowed to ban users.')
36
37 def ProcessFormData(self, mr, post_data):
38 self.AssertBasePermission(mr)
39 viewed_user_id = mr.viewed_user_auth.user_pb.user_id
40 reporter_id = mr.auth.user_id
41
42 # First ban or un-ban the user as a spammer.
43 framework_helpers.UserSettings.ProcessBanForm(
44 mr.cnxn, self.services.user, post_data, mr.viewed_user_auth.user_id,
45 mr.viewed_user_auth.user_pb)
46
47 params = {
48 'spammer_id': viewed_user_id,
49 'reporter_id': reporter_id,
50 'is_spammer': 'banned' in post_data
51 }
52 task = cloud_tasks_helpers.generate_simple_task(
53 urls.BAN_SPAMMER_TASK + '.do', params)
54 cloud_tasks_helpers.create_task(task)
55
56 return framework_helpers.FormatAbsoluteURL(
57 mr, mr.viewed_user_auth.user_view.profile_url, include_project=False,
58 saved=1, ts=int(time.time()))
59
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020060 # def PostBanSpammerPage(self, **kwargs):
61 # return self.handler(**kwargs)
Copybara854996b2021-09-07 19:36:02 +000062
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020063
64# when convert to flask switch jsonfeed.FlaskInternalTask
Copybara854996b2021-09-07 19:36:02 +000065class BanSpammerTask(jsonfeed.InternalTask):
66 """This task will update all of the comments and issues created by the
67 target user with is_spam=True, and also add a manual verdict attached
68 to the user who originated the ban request. This is a potentially long
69 running operation, so it is implemented as an async task.
70 """
71
72 def HandleRequest(self, mr):
73 spammer_id = mr.GetPositiveIntParam('spammer_id')
74 reporter_id = mr.GetPositiveIntParam('reporter_id')
75 is_spammer = mr.GetBoolParam('is_spammer')
76
77 # Get all of the issues reported by the spammer.
78 issue_ids = self.services.issue.GetIssueIDsReportedByUser(mr.cnxn,
79 spammer_id)
80
81 issues = []
82
83 if len(issue_ids) > 0:
84 issues = self.services.issue.GetIssues(
85 mr.cnxn, issue_ids, use_cache=False)
86
87 # Mark them as spam/ham in bulk.
88 self.services.spam.RecordManualIssueVerdicts(mr.cnxn, self.services.issue,
89 issues, reporter_id, is_spammer)
90
91 # Get all of the comments
92 comments = self.services.issue.GetCommentsByUser(mr.cnxn, spammer_id)
93
94 for comment in comments:
95 self.services.spam.RecordManualCommentVerdict(mr.cnxn,
96 self.services.issue, self.services.user, comment.id,
97 reporter_id, is_spammer)
98
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +020099 # remove the self.response.body when convert to flask
Copybara854996b2021-09-07 19:36:02 +0000100 self.response.body = json.dumps({
101 'comments': len(comments),
102 'issues': len(issues),
103 })
Adrià Vilanova Martínezde942802022-07-15 14:06:55 +0200104 # return json.dumps({
105 # 'comments': len(comments),
106 # 'issues': len(issues),
107 # })
108
109 # def GetBanSpammer(self, **kwargs):
110 # return self.handler(**kwargs)
111
112 # def PostBanSpammer(self, **kwargs):
113 # return self.handler(**kwargs)