StarredChangesUtil: Simplify API to check if a change is muted
Change-Id: I72566a934c9da13bd37299620b57eb15c756d9c5
This commit is contained in:
@@ -356,6 +356,10 @@ public class StarredChangesUtil {
|
||||
return getLabels(accountId, change.getId()).contains(getMuteLabel(change));
|
||||
}
|
||||
|
||||
public boolean isMuted(ChangeResource rsrc) throws OrmException {
|
||||
return isMutedBy(rsrc.getChange(), rsrc.getUser().asIdentifiedUser().getAccountId());
|
||||
}
|
||||
|
||||
private static StarRef readLabels(Repository repo, String refName) throws IOException {
|
||||
Ref ref = repo.exactRef(refName);
|
||||
if (ref == null) {
|
||||
|
||||
@@ -701,7 +701,7 @@ class ChangeApiImpl implements ChangeApi {
|
||||
@Override
|
||||
public boolean muted() throws RestApiException {
|
||||
try {
|
||||
return stars.isMutedBy(change.getChange(), change.getUser().getAccountId());
|
||||
return stars.isMuted(change);
|
||||
} catch (OrmException e) {
|
||||
throw asRestApiException("Cannot check if muted", e);
|
||||
}
|
||||
|
||||
@@ -18,13 +18,10 @@ import com.google.gerrit.extensions.restapi.Response;
|
||||
import com.google.gerrit.extensions.restapi.RestApiException;
|
||||
import com.google.gerrit.extensions.restapi.RestModifyView;
|
||||
import com.google.gerrit.extensions.webui.UiAction;
|
||||
import com.google.gerrit.reviewdb.client.Change;
|
||||
import com.google.gerrit.server.IdentifiedUser;
|
||||
import com.google.gerrit.server.StarredChangesUtil;
|
||||
import com.google.gerrit.server.StarredChangesUtil.IllegalLabelException;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
import com.google.inject.Singleton;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -35,12 +32,10 @@ public class Mute implements RestModifyView<ChangeResource, Mute.Input>, UiActio
|
||||
|
||||
public static class Input {}
|
||||
|
||||
private final Provider<IdentifiedUser> self;
|
||||
private final StarredChangesUtil stars;
|
||||
|
||||
@Inject
|
||||
Mute(Provider<IdentifiedUser> self, StarredChangesUtil stars) {
|
||||
this.self = self;
|
||||
Mute(StarredChangesUtil stars) {
|
||||
this.stars = stars;
|
||||
}
|
||||
|
||||
@@ -49,13 +44,13 @@ public class Mute implements RestModifyView<ChangeResource, Mute.Input>, UiActio
|
||||
return new UiAction.Description()
|
||||
.setLabel("Mute")
|
||||
.setTitle("Mute the change to unhighlight it in the dashboard")
|
||||
.setVisible(!rsrc.isUserOwner() && isMuteable(rsrc.getChange()));
|
||||
.setVisible(!rsrc.isUserOwner() && isMuteable(rsrc));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response<String> apply(ChangeResource rsrc, Input input)
|
||||
throws RestApiException, OrmException, IllegalLabelException {
|
||||
if (rsrc.isUserOwner() || isMuted(rsrc.getChange())) {
|
||||
if (rsrc.isUserOwner() || isMuted(rsrc)) {
|
||||
// early exit for own changes and already muted changes
|
||||
return Response.ok("");
|
||||
}
|
||||
@@ -63,18 +58,18 @@ public class Mute implements RestModifyView<ChangeResource, Mute.Input>, UiActio
|
||||
return Response.ok("");
|
||||
}
|
||||
|
||||
private boolean isMuted(Change change) {
|
||||
private boolean isMuted(ChangeResource rsrc) {
|
||||
try {
|
||||
return stars.isMutedBy(change, self.get().getAccountId());
|
||||
return stars.isMuted(rsrc);
|
||||
} catch (OrmException e) {
|
||||
log.error("failed to check muted star", e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isMuteable(Change change) {
|
||||
private boolean isMuteable(ChangeResource rsrc) {
|
||||
try {
|
||||
return !isMuted(change) && !stars.isIgnoredBy(change.getId(), self.get().getAccountId());
|
||||
return !isMuted(rsrc) && !stars.isIgnored(rsrc);
|
||||
} catch (OrmException e) {
|
||||
log.error("failed to check ignored star", e);
|
||||
}
|
||||
|
||||
@@ -18,13 +18,10 @@ import com.google.gerrit.extensions.restapi.Response;
|
||||
import com.google.gerrit.extensions.restapi.RestApiException;
|
||||
import com.google.gerrit.extensions.restapi.RestModifyView;
|
||||
import com.google.gerrit.extensions.webui.UiAction;
|
||||
import com.google.gerrit.reviewdb.client.Change;
|
||||
import com.google.gerrit.server.IdentifiedUser;
|
||||
import com.google.gerrit.server.StarredChangesUtil;
|
||||
import com.google.gerrit.server.StarredChangesUtil.IllegalLabelException;
|
||||
import com.google.gwtorm.server.OrmException;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Provider;
|
||||
import com.google.inject.Singleton;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -36,12 +33,10 @@ public class Unmute
|
||||
|
||||
public static class Input {}
|
||||
|
||||
private final Provider<IdentifiedUser> self;
|
||||
private final StarredChangesUtil stars;
|
||||
|
||||
@Inject
|
||||
Unmute(Provider<IdentifiedUser> self, StarredChangesUtil stars) {
|
||||
this.self = self;
|
||||
Unmute(StarredChangesUtil stars) {
|
||||
this.stars = stars;
|
||||
}
|
||||
|
||||
@@ -50,13 +45,13 @@ public class Unmute
|
||||
return new UiAction.Description()
|
||||
.setLabel("Unmute")
|
||||
.setTitle("Unmute the change")
|
||||
.setVisible(!rsrc.isUserOwner() && isUnMuteable(rsrc.getChange()));
|
||||
.setVisible(!rsrc.isUserOwner() && isUnMuteable(rsrc));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response<String> apply(ChangeResource rsrc, Input input)
|
||||
throws RestApiException, OrmException, IllegalLabelException {
|
||||
if (rsrc.isUserOwner() || !isMuted(rsrc.getChange())) {
|
||||
if (rsrc.isUserOwner() || !isMuted(rsrc)) {
|
||||
// early exit for own changes and not muted changes
|
||||
return Response.ok("");
|
||||
}
|
||||
@@ -64,18 +59,18 @@ public class Unmute
|
||||
return Response.ok("");
|
||||
}
|
||||
|
||||
private boolean isMuted(Change change) {
|
||||
private boolean isMuted(ChangeResource rsrc) {
|
||||
try {
|
||||
return stars.isMutedBy(change, self.get().getAccountId());
|
||||
return stars.isMuted(rsrc);
|
||||
} catch (OrmException e) {
|
||||
log.error("failed to check muted star", e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isUnMuteable(Change change) {
|
||||
private boolean isUnMuteable(ChangeResource rsrc) {
|
||||
try {
|
||||
return isMuted(change) && !stars.isIgnoredBy(change.getId(), self.get().getAccountId());
|
||||
return isMuted(rsrc) && !stars.isIgnored(rsrc);
|
||||
} catch (OrmException e) {
|
||||
log.error("failed to check ignored star", e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user