blob: 55a494f861b23b98c983cc31da4435ccfc3c4a04 [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"""Servlet to show the original email that caused an issue comment.
7
8The text of the body the email is shown in an HTML page with <pre>.
9All the text is automatically escaped by EZT to make it safe to
10include in an HTML page.
11"""
12from __future__ import print_function
13from __future__ import division
14from __future__ import absolute_import
15
16import logging
17import ezt
18
19from businesslogic import work_env
20from framework import filecontent
21from framework import permissions
22from framework import servlet
23from services import issue_svc
24
25
26class IssueOriginal(servlet.Servlet):
27 """IssueOriginal shows an inbound email that caused an issue comment."""
28
29 _PAGE_TEMPLATE = 'tracker/issue-original-page.ezt'
30
31 def AssertBasePermission(self, mr):
32 """Make sure that the logged in user has permission to view this page."""
33 super(IssueOriginal, self).AssertBasePermission(mr)
34 issue, comment = self._GetIssueAndComment(mr)
35
36 # TODO(jrobbins): take granted perms into account here.
37 if issue and not permissions.CanViewIssue(
38 mr.auth.effective_ids, mr.perms, mr.project, issue,
39 allow_viewing_deleted=True):
40 raise permissions.PermissionException(
41 'User is not allowed to view this issue')
42
43 can_view_inbound_message = self.CheckPerm(
44 mr, permissions.VIEW_INBOUND_MESSAGES, art=issue)
45 issue_perms = permissions.UpdateIssuePermissions(
46 mr.perms, mr.project, issue, mr.auth.effective_ids)
47 commenter = self.services.user.GetUser(mr.cnxn, comment.user_id)
48 can_view_comment = permissions.CanViewComment(
49 comment, commenter, mr.auth.user_id, issue_perms)
50 if not can_view_inbound_message or not can_view_comment:
51 raise permissions.PermissionException(
52 'Only project members may view original email text')
53
54 def GatherPageData(self, mr):
55 """Build up a dictionary of data values to use when rendering the page.
56
57 Args:
58 mr: commonly used info parsed from the request.
59
60 Returns:
61 Dict of values used by EZT for rendering the page.
62 """
63 issue, comment = self._GetIssueAndComment(mr)
64 message_body_unicode, is_binary, _is_long = (
65 filecontent.DecodeFileContents(comment.inbound_message))
66
67 # Take out the iso8859-1 non-breaking-space characters that gmail
68 # inserts between consecutive spaces when quoting text in a reply.
69 # You can see this in gmail by sending a plain text reply to a
70 # message that had multiple spaces on some line, then use the
71 # "Show original" menu item to view your reply, you will see "=A0".
72 #message_body_unicode = message_body_unicode.replace(u'\xa0', u' ')
73
74 page_data = {
75 'local_id': issue.local_id,
76 'seq': comment.sequence,
77 'is_binary': ezt.boolean(is_binary),
78 'message_body': message_body_unicode,
79 }
80
81 return page_data
82
83 def _GetIssueAndComment(self, mr):
84 """Wait on retriving the specified issue and issue comment."""
85 if mr.seq is None:
86 self.abort(404, 'comment not specified')
87
88 with work_env.WorkEnv(mr, self.services) as we:
89 issue = self.services.issue.GetIssueByLocalID(
90 mr.cnxn, mr.project_id, mr.local_id)
91 comments = we.ListIssueComments(issue)
92
93 try:
94 comment = comments[mr.seq]
95 except IndexError:
96 self.abort(404, 'comment not found')
97
98 return issue, comment