Search in sources :

Example 1 with HumanCommentFormatter

use of com.google.gerrit.server.restapi.change.CommentJson.HumanCommentFormatter in project gerrit by GerritCodeReview.

the class DeleteDraftComments method apply.

@Override
public Response<ImmutableList<DeletedDraftCommentInfo>> apply(AccountResource rsrc, DeleteDraftCommentsInput input) throws RestApiException, UpdateException {
    CurrentUser user = userProvider.get();
    if (!user.isIdentifiedUser()) {
        throw new AuthException("Authentication required");
    }
    if (!rsrc.getUser().hasSameAccountId(user)) {
        // hasSameAccountId check.)
        throw new AuthException("Cannot delete drafts of other user");
    }
    HumanCommentFormatter humanCommentFormatter = commentJsonProvider.get().newHumanCommentFormatter();
    Account.Id accountId = rsrc.getUser().getAccountId();
    Instant now = TimeUtil.now();
    Map<Project.NameKey, BatchUpdate> updates = new LinkedHashMap<>();
    List<Op> ops = new ArrayList<>();
    for (ChangeData cd : queryProvider.get().enforceVisibility(true).query(predicate(accountId, input))) {
        BatchUpdate update = updates.computeIfAbsent(cd.project(), p -> batchUpdateFactory.create(p, rsrc.getUser(), now));
        Op op = new Op(humanCommentFormatter, accountId);
        update.addOp(cd.getId(), op);
        ops.add(op);
    }
    // Currently there's no way to let some updates succeed even if others fail. Even if there were,
    // all updates from this operation only happen in All-Users and thus are fully atomic, so
    // allowing partial failure would have little value.
    BatchUpdate.execute(updates.values(), ImmutableList.of(), false);
    return Response.ok(ops.stream().map(Op::getResult).filter(Objects::nonNull).collect(toImmutableList()));
}
Also used : Account(com.google.gerrit.entities.Account) BatchUpdateOp(com.google.gerrit.server.update.BatchUpdateOp) CurrentUser(com.google.gerrit.server.CurrentUser) Instant(java.time.Instant) ArrayList(java.util.ArrayList) AuthException(com.google.gerrit.extensions.restapi.AuthException) ChangeData(com.google.gerrit.server.query.change.ChangeData) BatchUpdate(com.google.gerrit.server.update.BatchUpdate) LinkedHashMap(java.util.LinkedHashMap) Objects(java.util.Objects) HumanCommentFormatter(com.google.gerrit.server.restapi.change.CommentJson.HumanCommentFormatter)

Aggregations

Account (com.google.gerrit.entities.Account)1 AuthException (com.google.gerrit.extensions.restapi.AuthException)1 CurrentUser (com.google.gerrit.server.CurrentUser)1 ChangeData (com.google.gerrit.server.query.change.ChangeData)1 HumanCommentFormatter (com.google.gerrit.server.restapi.change.CommentJson.HumanCommentFormatter)1 BatchUpdate (com.google.gerrit.server.update.BatchUpdate)1 BatchUpdateOp (com.google.gerrit.server.update.BatchUpdateOp)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Objects (java.util.Objects)1