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