Line data Source code
1 : // Copyright (C) 2020 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 com.google.common.base.Strings; 18 : import com.google.gerrit.entities.Account; 19 : import com.google.gerrit.extensions.api.changes.AttentionSetInput; 20 : import com.google.gerrit.extensions.api.changes.NotifyHandling; 21 : import com.google.gerrit.extensions.restapi.BadRequestException; 22 : import com.google.gerrit.extensions.restapi.Response; 23 : import com.google.gerrit.extensions.restapi.RestApiException; 24 : import com.google.gerrit.extensions.restapi.RestModifyView; 25 : import com.google.gerrit.server.account.AccountResolver; 26 : import com.google.gerrit.server.change.AttentionSetEntryResource; 27 : import com.google.gerrit.server.change.ChangeResource; 28 : import com.google.gerrit.server.change.NotifyResolver; 29 : import com.google.gerrit.server.change.RemoveFromAttentionSetOp; 30 : import com.google.gerrit.server.permissions.PermissionBackendException; 31 : import com.google.gerrit.server.update.BatchUpdate; 32 : import com.google.gerrit.server.update.UpdateException; 33 : import com.google.gerrit.server.util.AttentionSetUtil; 34 : import com.google.gerrit.server.util.time.TimeUtil; 35 : import com.google.inject.Inject; 36 : import java.io.IOException; 37 : import org.eclipse.jgit.errors.ConfigInvalidException; 38 : 39 : /** Removes a single user from the attention set. */ 40 : public class RemoveFromAttentionSet 41 : implements RestModifyView<AttentionSetEntryResource, AttentionSetInput> { 42 : private final BatchUpdate.Factory updateFactory; 43 : private final RemoveFromAttentionSetOp.Factory opFactory; 44 : private final AccountResolver accountResolver; 45 : private final NotifyResolver notifyResolver; 46 : 47 : @Inject 48 : RemoveFromAttentionSet( 49 : BatchUpdate.Factory updateFactory, 50 : RemoveFromAttentionSetOp.Factory opFactory, 51 : AccountResolver accountResolver, 52 13 : NotifyResolver notifyResolver) { 53 13 : this.updateFactory = updateFactory; 54 13 : this.opFactory = opFactory; 55 13 : this.accountResolver = accountResolver; 56 13 : this.notifyResolver = notifyResolver; 57 13 : } 58 : 59 : @Override 60 : public Response<Object> apply( 61 : AttentionSetEntryResource attentionResource, AttentionSetInput input) 62 : throws RestApiException, PermissionBackendException, IOException, ConfigInvalidException, 63 : UpdateException { 64 13 : if (input == null) { 65 0 : throw new BadRequestException("input may not be null"); 66 : } 67 13 : input.reason = Strings.nullToEmpty(input.reason).trim(); 68 13 : if (input.reason.isEmpty()) { 69 0 : throw new BadRequestException("missing field: reason"); 70 : } 71 13 : input.user = Strings.nullToEmpty(input.user).trim(); 72 13 : if (!input.user.isEmpty()) { 73 2 : Account.Id attentionUserId = 74 2 : AttentionSetUtil.resolveAccount( 75 2 : accountResolver, attentionResource.getChangeResource().getNotes(), input.user); 76 2 : if (attentionUserId.get() != attentionResource.getAccountId().get()) { 77 1 : throw new BadRequestException( 78 : "The field \"user\" must be empty, or must match the user specified in the URL."); 79 : } 80 : } 81 13 : ChangeResource changeResource = attentionResource.getChangeResource(); 82 13 : try (BatchUpdate bu = 83 13 : updateFactory.create( 84 13 : changeResource.getProject(), changeResource.getUser(), TimeUtil.now())) { 85 13 : RemoveFromAttentionSetOp op = 86 13 : opFactory.create(attentionResource.getAccountId(), input.reason, true); 87 13 : bu.addOp(changeResource.getId(), op); 88 13 : NotifyHandling notify = input.notify == null ? NotifyHandling.OWNER : input.notify; 89 13 : NotifyResolver.Result notifyResult = notifyResolver.resolve(notify, input.notifyDetails); 90 13 : bu.setNotify(notifyResult); 91 13 : bu.execute(); 92 : } 93 13 : return Response.none(); 94 : } 95 : }