Skip to content

Commit

Permalink
Instrument registration ID changes via the "set account attributes" e…
Browse files Browse the repository at this point in the history
…ndpoint
  • Loading branch information
jon-signal committed Nov 29, 2023
1 parent f46842c commit 8fbc1da
Showing 1 changed file with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
*/
package org.whispersystems.textsecuregcm.controllers;

import static org.whispersystems.textsecuregcm.metrics.MetricsUtil.name;

import com.google.common.net.HttpHeaders;
import io.dropwizard.auth.Auth;
import io.micrometer.core.instrument.Metrics;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
Expand Down Expand Up @@ -80,7 +81,6 @@ public class AccountController {
public static final int USERNAME_HASH_LENGTH = 32;
public static final int MAXIMUM_USERNAME_CIPHERTEXT_LENGTH = 128;

private static final String INVALID_REGISTRATION_ID = name(AccountController.class, "invalidRegistrationId");
private final AccountsManager accounts;
private final RateLimiters rateLimiters;
private final TurnTokenGenerator turnTokenGenerator;
Expand Down Expand Up @@ -222,11 +222,26 @@ public void setName(@Auth DisabledPermittedAuthenticatedAccount disabledPermitte
@ChangesDeviceEnabledState
public void setAccountAttributes(
@Auth DisabledPermittedAuthenticatedAccount disabledPermittedAuth,
@HeaderParam(HeaderUtils.X_SIGNAL_AGENT) String userAgent,
@HeaderParam(HeaderUtils.X_SIGNAL_AGENT) String signalAgent,
@HeaderParam(HttpHeaders.USER_AGENT) String userAgent,
@NotNull @Valid AccountAttributes attributes) {
final Account account = disabledPermittedAuth.getAccount();
final byte deviceId = disabledPermittedAuth.getAuthenticatedDevice().getId();

final boolean aciRegistrationIdChanged = account.getDevice(deviceId)
.map(device -> device.getRegistrationId() != attributes.getRegistrationId())
.orElse(false);

final boolean pniRegistrationIdChanged;

if (attributes.getPhoneNumberIdentityRegistrationId().isPresent()) {
pniRegistrationIdChanged = account.getDevice(deviceId)
.map(device -> !attributes.getPhoneNumberIdentityRegistrationId().equals(device.getPhoneNumberIdentityRegistrationId()))
.orElse(false);
} else {
pniRegistrationIdChanged = false;
}

final Account updatedAccount = accounts.update(account, a -> {
a.getDevice(deviceId).ifPresent(d -> {
d.setFetchesMessages(attributes.getFetchesMessages());
Expand All @@ -235,7 +250,7 @@ public void setAccountAttributes(
d.setCapabilities(attributes.getCapabilities());
d.setRegistrationId(attributes.getRegistrationId());
attributes.getPhoneNumberIdentityRegistrationId().ifPresent(d::setPhoneNumberIdentityRegistrationId);
d.setUserAgent(userAgent);
d.setUserAgent(signalAgent);
});

a.setRegistrationLockFromAttributes(attributes);
Expand All @@ -248,8 +263,12 @@ public void setAccountAttributes(
attributes.recoveryPassword().ifPresent(registrationRecoveryPassword ->
registrationRecoveryPasswordsManager.storeForCurrentNumber(updatedAccount.getNumber(), registrationRecoveryPassword));

Metrics.counter(SET_ATTRIBUTES_COUNTER_NAME,
"pniRegistrationIdPresent", String.valueOf(attributes.getPhoneNumberIdentityRegistrationId().isPresent()))
final Tags tags = Tags.of(UserAgentTagUtil.getPlatformTag(userAgent),
Tag.of("pniRegistrationIdPresent", String.valueOf(attributes.getPhoneNumberIdentityRegistrationId().isPresent())),
Tag.of("aciRegistrationIdChanged", String.valueOf(aciRegistrationIdChanged)),
Tag.of("pniRegistrationIdChanged", String.valueOf(pniRegistrationIdChanged)));

Metrics.counter(SET_ATTRIBUTES_COUNTER_NAME, tags)
.increment();
}

Expand Down

0 comments on commit 8fbc1da

Please sign in to comment.