Line data Source code
1 : // Copyright (C) 2014 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.restapi.change; 16 : 17 : import static com.google.common.base.MoreObjects.firstNonNull; 18 : 19 : import com.google.gerrit.entities.Change; 20 : import com.google.gerrit.extensions.api.changes.DeleteVoteInput; 21 : import com.google.gerrit.extensions.api.changes.NotifyHandling; 22 : import com.google.gerrit.extensions.restapi.BadRequestException; 23 : import com.google.gerrit.extensions.restapi.MethodNotAllowedException; 24 : import com.google.gerrit.extensions.restapi.Response; 25 : import com.google.gerrit.extensions.restapi.RestApiException; 26 : import com.google.gerrit.extensions.restapi.RestModifyView; 27 : import com.google.gerrit.server.CurrentUser; 28 : import com.google.gerrit.server.change.AddToAttentionSetOp; 29 : import com.google.gerrit.server.change.AttentionSetUnchangedOp; 30 : import com.google.gerrit.server.change.NotifyResolver; 31 : import com.google.gerrit.server.change.ReviewerResource; 32 : import com.google.gerrit.server.change.VoteResource; 33 : import com.google.gerrit.server.update.BatchUpdate; 34 : import com.google.gerrit.server.update.UpdateException; 35 : import com.google.gerrit.server.util.time.TimeUtil; 36 : import com.google.inject.Inject; 37 : import com.google.inject.Provider; 38 : import com.google.inject.Singleton; 39 : import java.io.IOException; 40 : import org.eclipse.jgit.errors.ConfigInvalidException; 41 : 42 : @Singleton 43 : public class DeleteVote implements RestModifyView<VoteResource, DeleteVoteInput> { 44 : private final BatchUpdate.Factory updateFactory; 45 : private final NotifyResolver notifyResolver; 46 : 47 : private final AddToAttentionSetOp.Factory attentionSetOpFactory; 48 : private final Provider<CurrentUser> currentUserProvider; 49 : private final DeleteVoteOp.Factory deleteVoteOpFactory; 50 : 51 : @Inject 52 : DeleteVote( 53 : BatchUpdate.Factory updateFactory, 54 : NotifyResolver notifyResolver, 55 : AddToAttentionSetOp.Factory attentionSetOpFactory, 56 : Provider<CurrentUser> currentUserProvider, 57 142 : DeleteVoteOp.Factory deleteVoteOpFactory) { 58 142 : this.updateFactory = updateFactory; 59 142 : this.notifyResolver = notifyResolver; 60 142 : this.attentionSetOpFactory = attentionSetOpFactory; 61 142 : this.currentUserProvider = currentUserProvider; 62 142 : this.deleteVoteOpFactory = deleteVoteOpFactory; 63 142 : } 64 : 65 : @Override 66 : public Response<Object> apply(VoteResource rsrc, DeleteVoteInput input) 67 : throws RestApiException, UpdateException, IOException, ConfigInvalidException { 68 9 : if (input == null) { 69 9 : input = new DeleteVoteInput(); 70 : } 71 9 : if (input.label != null && !rsrc.getLabel().equals(input.label)) { 72 0 : throw new BadRequestException("label must match URL"); 73 : } 74 9 : if (input.notify == null) { 75 0 : input.notify = NotifyHandling.ALL; 76 : } 77 9 : ReviewerResource r = rsrc.getReviewer(); 78 9 : Change change = r.getChange(); 79 : 80 9 : if (r.getRevisionResource() != null && !r.getRevisionResource().isCurrent()) { 81 0 : throw new MethodNotAllowedException("Cannot delete vote on non-current patch set"); 82 : } 83 : 84 9 : try (BatchUpdate bu = 85 9 : updateFactory.create( 86 9 : change.getProject(), r.getChangeResource().getUser(), TimeUtil.now())) { 87 9 : bu.setNotify( 88 9 : notifyResolver.resolve( 89 9 : firstNonNull(input.notify, NotifyHandling.ALL), input.notifyDetails)); 90 9 : bu.addOp( 91 9 : change.getId(), 92 9 : deleteVoteOpFactory.create( 93 9 : r.getChange().getProject(), 94 9 : r.getReviewerUser().state(), 95 9 : rsrc.getLabel(), 96 : input, 97 : true)); 98 9 : if (!input.ignoreAutomaticAttentionSetRules 99 9 : && !r.getReviewerUser().getAccountId().equals(currentUserProvider.get().getAccountId())) { 100 4 : bu.addOp( 101 4 : change.getId(), 102 4 : attentionSetOpFactory.create( 103 4 : r.getReviewerUser().getAccountId(), 104 : /* reason= */ "Their vote was deleted", 105 : /* notify= */ false)); 106 : } 107 9 : if (input.ignoreAutomaticAttentionSetRules) { 108 1 : bu.addOp(change.getId(), new AttentionSetUnchangedOp()); 109 : } 110 9 : bu.execute(); 111 : } 112 : 113 9 : return Response.none(); 114 : } 115 : }