Line data Source code
1 : // Copyright (C) 2009 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 com.google.gerrit.common.Nullable; 18 : import com.google.gerrit.entities.Account; 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.server.query.change.ChangeData; 23 : import java.util.ArrayList; 24 : import java.util.Collection; 25 : import java.util.HashSet; 26 : import java.util.List; 27 : import java.util.Set; 28 : 29 : /** Sends an email alerting a user to a new change for them to review. */ 30 : public abstract class NewChangeSender extends ChangeEmail { 31 103 : private final Set<Account.Id> reviewers = new HashSet<>(); 32 103 : private final Set<Address> reviewersByEmail = new HashSet<>(); 33 103 : private final Set<Account.Id> extraCC = new HashSet<>(); 34 103 : private final Set<Address> extraCCByEmail = new HashSet<>(); 35 103 : private final Set<Account.Id> removedReviewers = new HashSet<>(); 36 103 : private final Set<Address> removedByEmailReviewers = new HashSet<>(); 37 : 38 : protected NewChangeSender(EmailArguments args, ChangeData changeData) { 39 103 : super(args, "newchange", changeData); 40 103 : } 41 : 42 : public void addReviewers(Collection<Account.Id> cc) { 43 103 : reviewers.addAll(cc); 44 103 : } 45 : 46 : public void addReviewersByEmail(Collection<Address> cc) { 47 103 : reviewersByEmail.addAll(cc); 48 103 : } 49 : 50 : public void addExtraCC(Collection<Account.Id> cc) { 51 103 : extraCC.addAll(cc); 52 103 : } 53 : 54 : public void addExtraCCByEmail(Collection<Address> cc) { 55 103 : extraCCByEmail.addAll(cc); 56 103 : } 57 : 58 : public void addRemovedReviewers(Collection<Account.Id> removed) { 59 31 : removedReviewers.addAll(removed); 60 31 : } 61 : 62 : public void addRemovedByEmailReviewers(Collection<Address> removed) { 63 31 : removedByEmailReviewers.addAll(removed); 64 31 : } 65 : 66 : @Override 67 : protected void init() throws EmailException { 68 103 : super.init(); 69 103 : String threadId = getChangeMessageThreadId(); 70 103 : setHeader("References", threadId); 71 : 72 103 : switch (notify.handling()) { 73 : case NONE: 74 : case OWNER: 75 16 : break; 76 : case ALL: 77 : default: 78 103 : extraCC.stream().forEach(cc -> add(RecipientType.CC, cc)); 79 103 : extraCCByEmail.stream().forEach(cc -> add(RecipientType.CC, cc)); 80 : // $FALL-THROUGH$ 81 : case OWNER_REVIEWERS: 82 103 : reviewers.stream().forEach(r -> add(RecipientType.TO, r, true)); 83 103 : addByEmail(RecipientType.TO, reviewersByEmail, true); 84 103 : removedReviewers.stream().forEach(r -> add(RecipientType.TO, r, true)); 85 103 : addByEmail(RecipientType.TO, removedByEmailReviewers, true); 86 : break; 87 : } 88 : 89 103 : rcptToAuthors(RecipientType.CC); 90 103 : } 91 : 92 : @Override 93 : protected void formatChange() throws EmailException { 94 103 : appendText(textTemplate("NewChange")); 95 103 : if (useHtml()) { 96 103 : appendHtml(soyHtmlTemplate("NewChangeHtml")); 97 : } 98 103 : } 99 : 100 : @Nullable 101 : public List<String> getReviewerNames() { 102 103 : if (reviewers.isEmpty()) { 103 103 : return null; 104 : } 105 33 : List<String> names = new ArrayList<>(); 106 33 : for (Account.Id id : reviewers) { 107 33 : names.add(getNameFor(id)); 108 33 : } 109 33 : return names; 110 : } 111 : 112 : @Nullable 113 : public List<String> getRemovedReviewerNames() { 114 103 : if (removedReviewers.isEmpty() && removedByEmailReviewers.isEmpty()) { 115 103 : return null; 116 : } 117 3 : List<String> names = new ArrayList<>(); 118 3 : for (Account.Id id : removedReviewers) { 119 2 : names.add(getNameFor(id)); 120 2 : } 121 3 : for (Address address : removedByEmailReviewers) { 122 1 : names.add(address.toString()); 123 1 : } 124 3 : return names; 125 : } 126 : 127 : @Override 128 : protected void setupSoyContext() { 129 103 : super.setupSoyContext(); 130 103 : soyContext.put("ownerName", getNameFor(change.getOwner())); 131 103 : soyContextEmailData.put("reviewerNames", getReviewerNames()); 132 103 : soyContextEmailData.put("removedReviewerNames", getRemovedReviewerNames()); 133 103 : } 134 : }