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()));
}
Aggregations