diff --git a/src/main/java/org/whispersystems/textsecuregcm/controllers/MessageController.java b/src/main/java/org/whispersystems/textsecuregcm/controllers/MessageController.java index bf854326e..d5972fde6 100644 --- a/src/main/java/org/whispersystems/textsecuregcm/controllers/MessageController.java +++ b/src/main/java/org/whispersystems/textsecuregcm/controllers/MessageController.java @@ -26,7 +26,7 @@ import org.whispersystems.textsecuregcm.entities.IncomingMessage; import org.whispersystems.textsecuregcm.entities.IncomingMessageList; import org.whispersystems.textsecuregcm.entities.MessageProtos.OutgoingMessageSignal; import org.whispersystems.textsecuregcm.entities.MessageResponse; -import org.whispersystems.textsecuregcm.entities.MissingDevices; +import org.whispersystems.textsecuregcm.entities.MismatchedDevices; import org.whispersystems.textsecuregcm.federation.FederatedClient; import org.whispersystems.textsecuregcm.federation.FederatedClientManager; import org.whispersystems.textsecuregcm.federation.NoSuchPeerException; @@ -92,15 +92,15 @@ public class MessageController { else sendRelayMessage(source, destinationName, messages); } catch (NoSuchUserException e) { throw new WebApplicationException(Response.status(404).build()); - } catch (MissingDevicesException e) { + } catch (MismatchedDevicesException e) { throw new WebApplicationException(Response.status(409) - .entity(new MissingDevices(e.getMissingDevices())) + .entity(new MismatchedDevices(e.getMissingDevices(), + e.getExtraDevices())) .build()); } } @Timed - @Path("/") @POST @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) @@ -123,7 +123,7 @@ public class MessageController { private void sendLocalMessage(Account source, String destinationName, IncomingMessageList messages) - throws NoSuchUserException, MissingDevicesException, IOException + throws NoSuchUserException, MismatchedDevicesException, IOException { Account destination = getDestinationAccount(destinationName); @@ -196,23 +196,34 @@ public class MessageController { } private void validateCompleteDeviceList(Account account, List messages) - throws MissingDevicesException + throws MismatchedDevicesException { - Set destinationDeviceIds = new HashSet<>(); - List missingDeviceIds = new LinkedList<>(); + Set messageDeviceIds = new HashSet<>(); + Set accountDeviceIds = new HashSet<>(); + + List missingDeviceIds = new LinkedList<>(); + List extraDeviceIds = new LinkedList<>(); for (IncomingMessage message : messages) { - destinationDeviceIds.add(message.getDestinationDeviceId()); + messageDeviceIds.add(message.getDestinationDeviceId()); } for (Device device : account.getDevices()) { - if (!destinationDeviceIds.contains(device.getId())) { + accountDeviceIds.add(device.getId()); + + if (!messageDeviceIds.contains(device.getId())) { missingDeviceIds.add(device.getId()); } } - if (!missingDeviceIds.isEmpty()) { - throw new MissingDevicesException(missingDeviceIds); + for (IncomingMessage message : messages) { + if (!accountDeviceIds.contains(message.getDestinationDeviceId())) { + extraDeviceIds.add(message.getDestinationDeviceId()); + } + } + + if (!missingDeviceIds.isEmpty() || !extraDeviceIds.isEmpty()) { + throw new MismatchedDevicesException(missingDeviceIds, extraDeviceIds); } } diff --git a/src/main/java/org/whispersystems/textsecuregcm/controllers/MismatchedDevicesException.java b/src/main/java/org/whispersystems/textsecuregcm/controllers/MismatchedDevicesException.java new file mode 100644 index 000000000..3748b638c --- /dev/null +++ b/src/main/java/org/whispersystems/textsecuregcm/controllers/MismatchedDevicesException.java @@ -0,0 +1,23 @@ +package org.whispersystems.textsecuregcm.controllers; + +import java.util.List; +import java.util.Set; + +public class MismatchedDevicesException extends Exception { + + private final List missingDevices; + private final List extraDevices; + + public MismatchedDevicesException(List missingDevices, List extraDevices) { + this.missingDevices = missingDevices; + this.extraDevices = extraDevices; + } + + public List getMissingDevices() { + return missingDevices; + } + + public List getExtraDevices() { + return extraDevices; + } +} diff --git a/src/main/java/org/whispersystems/textsecuregcm/controllers/MissingDevicesException.java b/src/main/java/org/whispersystems/textsecuregcm/controllers/MissingDevicesException.java deleted file mode 100644 index 513ad50d4..000000000 --- a/src/main/java/org/whispersystems/textsecuregcm/controllers/MissingDevicesException.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.whispersystems.textsecuregcm.controllers; - -import java.util.List; -import java.util.Set; - -public class MissingDevicesException extends Exception { - private final List missingDevices; - - public MissingDevicesException(List missingDevices) { - this.missingDevices = missingDevices; - } - - public List getMissingDevices() { - return missingDevices; - } -} diff --git a/src/main/java/org/whispersystems/textsecuregcm/entities/MissingDevices.java b/src/main/java/org/whispersystems/textsecuregcm/entities/MismatchedDevices.java similarity index 53% rename from src/main/java/org/whispersystems/textsecuregcm/entities/MissingDevices.java rename to src/main/java/org/whispersystems/textsecuregcm/entities/MismatchedDevices.java index e9c10e1c1..20df30c1d 100644 --- a/src/main/java/org/whispersystems/textsecuregcm/entities/MissingDevices.java +++ b/src/main/java/org/whispersystems/textsecuregcm/entities/MismatchedDevices.java @@ -4,13 +4,17 @@ import com.fasterxml.jackson.annotation.JsonProperty; import java.util.List; -public class MissingDevices { +public class MismatchedDevices { @JsonProperty public List missingDevices; - public MissingDevices(List missingDevices) { + @JsonProperty + public List extraDevices; + + public MismatchedDevices(List missingDevices, List extraDevices) { this.missingDevices = missingDevices; + this.extraDevices = extraDevices; } } diff --git a/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java b/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java index 46eb87257..345569f7f 100644 --- a/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java +++ b/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java @@ -19,19 +19,11 @@ package org.whispersystems.textsecuregcm.storage; import com.google.common.base.Optional; import net.spy.memcached.MemcachedClient; -import org.whispersystems.textsecuregcm.controllers.MissingDevicesException; import org.whispersystems.textsecuregcm.entities.ClientContact; -import org.whispersystems.textsecuregcm.util.Pair; import org.whispersystems.textsecuregcm.util.Util; -import sun.util.logging.resources.logging_zh_CN; -import java.util.HashMap; -import java.util.HashSet; import java.util.Iterator; -import java.util.LinkedList; import java.util.List; -import java.util.Map; -import java.util.Set; public class AccountsManager {