parent
76bf89dda3
commit
804d4320d7
|
@ -16,6 +16,10 @@
|
||||||
*/
|
*/
|
||||||
package org.whispersystems.textsecuregcm.controllers;
|
package org.whispersystems.textsecuregcm.controllers;
|
||||||
|
|
||||||
|
import com.codahale.metrics.Meter;
|
||||||
|
import com.codahale.metrics.MetricRegistry;
|
||||||
|
import com.codahale.metrics.SharedMetricRegistries;
|
||||||
|
import com.codahale.metrics.Timer;
|
||||||
import com.codahale.metrics.annotation.Timed;
|
import com.codahale.metrics.annotation.Timed;
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
import com.google.common.base.Optional;
|
import com.google.common.base.Optional;
|
||||||
|
@ -38,8 +42,10 @@ import org.whispersystems.textsecuregcm.storage.AccountsManager;
|
||||||
import org.whispersystems.textsecuregcm.storage.Device;
|
import org.whispersystems.textsecuregcm.storage.Device;
|
||||||
import org.whispersystems.textsecuregcm.storage.MessagesManager;
|
import org.whispersystems.textsecuregcm.storage.MessagesManager;
|
||||||
import org.whispersystems.textsecuregcm.storage.PendingAccountsManager;
|
import org.whispersystems.textsecuregcm.storage.PendingAccountsManager;
|
||||||
|
import org.whispersystems.textsecuregcm.util.Constants;
|
||||||
import org.whispersystems.textsecuregcm.util.Util;
|
import org.whispersystems.textsecuregcm.util.Util;
|
||||||
import org.whispersystems.textsecuregcm.util.VerificationCode;
|
import org.whispersystems.textsecuregcm.util.VerificationCode;
|
||||||
|
import org.whispersystems.textsecuregcm.websocket.WebSocketConnection;
|
||||||
|
|
||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
import javax.ws.rs.Consumes;
|
import javax.ws.rs.Consumes;
|
||||||
|
@ -60,12 +66,15 @@ import java.security.NoSuchAlgorithmException;
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import static com.codahale.metrics.MetricRegistry.name;
|
||||||
import io.dropwizard.auth.Auth;
|
import io.dropwizard.auth.Auth;
|
||||||
|
|
||||||
@Path("/v1/accounts")
|
@Path("/v1/accounts")
|
||||||
public class AccountController {
|
public class AccountController {
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(AccountController.class);
|
private final Logger logger = LoggerFactory.getLogger(AccountController.class);
|
||||||
|
private final MetricRegistry metricRegistry = SharedMetricRegistries.getOrCreate(Constants.METRICS_NAME);
|
||||||
|
private final Meter newUserMeter = metricRegistry.meter(name(AccountController.class, "brand_new_user"));
|
||||||
|
|
||||||
private final PendingAccountsManager pendingAccounts;
|
private final PendingAccountsManager pendingAccounts;
|
||||||
private final AccountsManager accounts;
|
private final AccountsManager accounts;
|
||||||
|
@ -318,7 +327,10 @@ public class AccountController {
|
||||||
account.setNumber(number);
|
account.setNumber(number);
|
||||||
account.addDevice(device);
|
account.addDevice(device);
|
||||||
|
|
||||||
accounts.create(account);
|
if (accounts.create(account)) {
|
||||||
|
newUserMeter.mark();
|
||||||
|
}
|
||||||
|
|
||||||
messagesManager.clear(number);
|
messagesManager.clear(number);
|
||||||
pendingAccounts.remove(number);
|
pendingAccounts.remove(number);
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,11 +53,10 @@ public abstract class Accounts {
|
||||||
private static final ObjectMapper mapper = SystemMapper.getMapper();
|
private static final ObjectMapper mapper = SystemMapper.getMapper();
|
||||||
|
|
||||||
@SqlUpdate("INSERT INTO accounts (" + NUMBER + ", " + DATA + ") VALUES (:number, CAST(:data AS json))")
|
@SqlUpdate("INSERT INTO accounts (" + NUMBER + ", " + DATA + ") VALUES (:number, CAST(:data AS json))")
|
||||||
@GetGeneratedKeys
|
abstract void insertStep(@AccountBinder Account account);
|
||||||
abstract long insertStep(@AccountBinder Account account);
|
|
||||||
|
|
||||||
@SqlUpdate("DELETE FROM accounts WHERE " + NUMBER + " = :number")
|
@SqlUpdate("DELETE FROM accounts WHERE " + NUMBER + " = :number")
|
||||||
abstract void removeAccount(@Bind("number") String number);
|
abstract int removeAccount(@Bind("number") String number);
|
||||||
|
|
||||||
@SqlUpdate("UPDATE accounts SET " + DATA + " = CAST(:data AS json) WHERE " + NUMBER + " = :number")
|
@SqlUpdate("UPDATE accounts SET " + DATA + " = CAST(:data AS json) WHERE " + NUMBER + " = :number")
|
||||||
abstract void update(@AccountBinder Account account);
|
abstract void update(@AccountBinder Account account);
|
||||||
|
@ -87,9 +86,11 @@ public abstract class Accounts {
|
||||||
public abstract int getUnsignedKeysCount(@Bind("since") long since);
|
public abstract int getUnsignedKeysCount(@Bind("since") long since);
|
||||||
|
|
||||||
@Transaction(TransactionIsolationLevel.SERIALIZABLE)
|
@Transaction(TransactionIsolationLevel.SERIALIZABLE)
|
||||||
public long create(Account account) {
|
public boolean create(Account account) {
|
||||||
removeAccount(account.getNumber());
|
int rows = removeAccount(account.getNumber());
|
||||||
return insertStep(account);
|
insertStep(account);
|
||||||
|
|
||||||
|
return rows == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SqlUpdate("VACUUM accounts")
|
@SqlUpdate("VACUUM accounts")
|
||||||
|
|
|
@ -64,10 +64,12 @@ public class AccountsManager {
|
||||||
return accounts.getAll();
|
return accounts.getAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void create(Account account) {
|
public boolean create(Account account) {
|
||||||
accounts.create(account);
|
boolean freshUser = accounts.create(account);
|
||||||
memcacheSet(account.getNumber(), account);
|
memcacheSet(account.getNumber(), account);
|
||||||
updateDirectory(account);
|
updateDirectory(account);
|
||||||
|
|
||||||
|
return freshUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(Account account) {
|
public void update(Account account) {
|
||||||
|
|
Loading…
Reference in New Issue