LCOV - code coverage report
Current view: top level - server/mail/send - InboundEmailRejectionSender.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 30 30 100.0 %
Date: 2022-11-19 15:00:39 Functions: 6 6 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2018 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.mail.send;
      16             : 
      17             : import static java.util.Objects.requireNonNull;
      18             : 
      19             : import com.google.gerrit.entities.Address;
      20             : import com.google.gerrit.exceptions.EmailException;
      21             : import com.google.gerrit.extensions.api.changes.RecipientType;
      22             : import com.google.gerrit.mail.MailHeader;
      23             : import com.google.inject.Inject;
      24             : import com.google.inject.assistedinject.Assisted;
      25             : import org.apache.james.mime4j.dom.field.FieldName;
      26             : 
      27             : /** Send an email to inform users that parsing their inbound email failed. */
      28             : public class InboundEmailRejectionSender extends OutgoingEmail {
      29             : 
      30             :   /** Used by the templating system to determine what error message should be sent */
      31         152 :   public enum InboundEmailError {
      32         152 :     PARSING_ERROR,
      33         152 :     INACTIVE_ACCOUNT,
      34         152 :     UNKNOWN_ACCOUNT,
      35         152 :     INTERNAL_EXCEPTION,
      36         152 :     COMMENT_REJECTED,
      37         152 :     CHANGE_NOT_FOUND
      38             :   }
      39             : 
      40             :   public interface Factory {
      41             :     InboundEmailRejectionSender create(Address to, String threadId, InboundEmailError reason);
      42             :   }
      43             : 
      44             :   private final Address to;
      45             :   private final InboundEmailError reason;
      46             :   private final String threadId;
      47             : 
      48             :   @Inject
      49             :   public InboundEmailRejectionSender(
      50             :       EmailArguments args,
      51             :       @Assisted Address to,
      52             :       @Assisted String threadId,
      53             :       @Assisted InboundEmailError reason) {
      54           2 :     super(args, "error");
      55           2 :     this.to = requireNonNull(to);
      56           2 :     this.threadId = requireNonNull(threadId);
      57           2 :     this.reason = requireNonNull(reason);
      58           2 :   }
      59             : 
      60             :   @Override
      61             :   protected void init() throws EmailException {
      62           2 :     super.init();
      63           2 :     setListIdHeader();
      64           2 :     setHeader(FieldName.SUBJECT, "[Gerrit Code Review] Unable to process your email");
      65             : 
      66           2 :     add(RecipientType.TO, to);
      67             : 
      68           2 :     if (!threadId.isEmpty()) {
      69           2 :       setHeader(MailHeader.REFERENCES.fieldName(), threadId);
      70             :     }
      71           2 :   }
      72             : 
      73             :   private void setListIdHeader() {
      74             :     // Set a reasonable list id so that filters can be used to sort messages
      75           2 :     setHeader("List-Id", "<gerrit-noreply." + getGerritHost() + ">");
      76           2 :     if (getSettingsUrl() != null) {
      77           2 :       setHeader("List-Unsubscribe", "<" + getSettingsUrl() + ">");
      78             :     }
      79           2 :   }
      80             : 
      81             :   @Override
      82             :   protected void format() throws EmailException {
      83           2 :     appendText(textTemplate("InboundEmailRejection_" + reason.name()));
      84           2 :     if (useHtml()) {
      85           2 :       appendHtml(soyHtmlTemplate("InboundEmailRejectionHtml_" + reason.name()));
      86             :     }
      87           2 :   }
      88             : 
      89             :   @Override
      90             :   protected void setupSoyContext() {
      91           2 :     super.setupSoyContext();
      92           2 :     footers.add(MailHeader.MESSAGE_TYPE.withDelimiter() + messageClass);
      93           2 :   }
      94             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750