Skip to content

Commit

Permalink
Avoid annotation-driven parsing of device capabilities in `DeviceCont…
Browse files Browse the repository at this point in the history
…roller`
  • Loading branch information
jkt-signal authored and jon-signal committed Oct 31, 2024
1 parent bf741df commit 63021e0
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
*/
package org.whispersystems.textsecuregcm.controllers;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.net.HttpHeaders;
import io.dropwizard.auth.Auth;
Expand Down Expand Up @@ -414,13 +412,12 @@ public void setUnauthenticatedDelivery(@Mutable @Auth AuthenticatedDevice auth)
public void setCapabilities(@Mutable @Auth final AuthenticatedDevice auth,

@NotNull
@JsonSerialize(using = DeviceCapabilityAdapter.Serializer.class)
@JsonDeserialize(using = DeviceCapabilityAdapter.Deserializer.class)
final Set<DeviceCapability> capabilities) {
final Map<String, Boolean> capabilities) {

assert (auth.getAuthenticatedDevice() != null);
final byte deviceId = auth.getAuthenticatedDevice().getId();
accounts.updateDevice(auth.getAccount(), deviceId, d -> d.setCapabilities(capabilities));
accounts.updateDevice(auth.getAccount(), deviceId,
d -> d.setCapabilities(DeviceCapabilityAdapter.mapToSet(capabilities)));
}

@PUT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package org.whispersystems.textsecuregcm.storage;

import java.util.Optional;

public enum DeviceCapability {
STORAGE("storage", AccountCapabilityMode.ANY_DEVICE, false, false),
TRANSFER("transfer", AccountCapabilityMode.PRIMARY_DEVICE, false, false),
Expand Down Expand Up @@ -50,13 +52,12 @@ public boolean includeInProfile() {
return includeInProfile;
}

public static DeviceCapability forName(final String name) {
public static Optional<DeviceCapability> forName(final String name) {
for (final DeviceCapability capability : DeviceCapability.values()) {
if (capability.getName().equals(name)) {
return capability;
return Optional.of(capability);
}
}

throw new IllegalArgumentException("Unknown capability: " + name);
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.IOException;
import java.util.EnumSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -44,20 +45,19 @@ public static class Deserializer extends JsonDeserializer<Set<DeviceCapability>>
public Set<DeviceCapability> deserialize(final JsonParser jsonParser,
final DeserializationContext deserializationContext) throws IOException {

final Map<String, Boolean> capabilitiesMap = jsonParser.readValueAs(STRING_TO_BOOLEAN_MAP_TYPE);
final EnumSet<DeviceCapability> capabilities = EnumSet.noneOf(DeviceCapability.class);
return mapToSet(jsonParser.readValueAs(STRING_TO_BOOLEAN_MAP_TYPE));
}

capabilitiesMap.forEach((capability, active) -> {
if (active) {
try {
capabilities.add(DeviceCapability.forName(capability));
} catch (final IllegalArgumentException ignored) {
// This most likely means we've retired a capability
}
}
});
}

return capabilities;
}
public static Set<DeviceCapability> mapToSet(Map<String, Boolean> capabilitiesMap) {
return capabilitiesMap.entrySet()
.stream()
.filter(Map.Entry::getValue)
.map(entry -> DeviceCapability.forName(entry.getKey()))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toCollection(() -> EnumSet.noneOf(DeviceCapability.class)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -785,10 +785,11 @@ void putCapabilitiesSuccessTest() {
.request()
.header("Authorization", AuthHelper.getAuthHeader(AuthHelper.VALID_UUID, AuthHelper.VALID_PASSWORD))
.header(HttpHeaders.USER_AGENT, "Signal-Android/5.42.8675309 Android/30")
.put(Entity.entity(Set.of(), MediaType.APPLICATION_JSON_TYPE))) {
.put(Entity.json("{\"deleteSync\": true, \"notARealDeviceCapability\": true}"))) {

assertThat(response.getStatus()).isEqualTo(204);
assertThat(response.hasEntity()).isFalse();
verify(AuthHelper.VALID_DEVICE).setCapabilities(Set.of(DeviceCapability.DELETE_SYNC));
}
}

Expand Down

0 comments on commit 63021e0

Please sign in to comment.