Ignore/Unignore: Refactor to reuse logic

The same logic is repeated in the checks for whether to show the
UI button, and for whether to allow the operation for own changes
or changes that are already (un)ignored.

Factor the logic to helper methods.

Change-Id: If6c06e2556ac9be98f3654d204c1c1dbf0af40f4
This commit is contained in:
David Pursehouse
2017-09-16 15:39:50 +09:00
parent b46221f078
commit f710cbf25c
2 changed files with 18 additions and 12 deletions

View File

@@ -48,21 +48,24 @@ public class Ignore
return new UiAction.Description()
.setLabel("Ignore")
.setTitle("Ignore the change")
.setVisible(!rsrc.isUserOwner() && !isIgnored(rsrc));
.setVisible(canIgnore(rsrc));
}
@Override
public Response<String> apply(ChangeResource rsrc, Input input) throws RestApiException {
try {
if (rsrc.isUserOwner() || isIgnored(rsrc)) {
// early exit for own changes and already ignored changes
return Response.ok("");
// Don't try to ignore own changes or already ignored changes
if (canIgnore(rsrc)) {
stars.ignore(rsrc);
}
stars.ignore(rsrc);
return Response.ok("");
} catch (OrmException e) {
throw new RestApiException("failed to ignore change", e);
}
return Response.ok("");
}
private boolean canIgnore(ChangeResource rsrc) {
return !rsrc.isUserOwner() && !isIgnored(rsrc);
}
private boolean isIgnored(ChangeResource rsrc) {

View File

@@ -48,21 +48,24 @@ public class Unignore
return new UiAction.Description()
.setLabel("Unignore")
.setTitle("Unignore the change")
.setVisible(!rsrc.isUserOwner() && isIgnored(rsrc));
.setVisible(canUnignore(rsrc));
}
@Override
public Response<String> apply(ChangeResource rsrc, Input input) throws RestApiException {
try {
if (rsrc.isUserOwner() || !isIgnored(rsrc)) {
// early exit for own changes and not ignored changes
return Response.ok("");
// Don't try to unignore own changes or not ignored changes
if (canUnignore(rsrc)) {
stars.unignore(rsrc);
}
stars.unignore(rsrc);
return Response.ok("");
} catch (OrmException e) {
throw new RestApiException("failed to unignore change", e);
}
return Response.ok("");
}
private boolean canUnignore(ChangeResource rsrc) {
return !rsrc.isUserOwner() && isIgnored(rsrc);
}
private boolean isIgnored(ChangeResource rsrc) {