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