From 6e595a0959224553f4d0133512031390dc4d585c Mon Sep 17 00:00:00 2001 From: Ravi Khadiwala Date: Mon, 26 Sep 2022 10:26:13 -0500 Subject: [PATCH] add an optionals utility and fix push challenge metric --- .../controllers/AccountController.java | 15 ++++++------- .../textsecuregcm/util/Optionals.java | 21 +++++++++++++++++++ 2 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 service/src/main/java/org/whispersystems/textsecuregcm/util/Optionals.java diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountController.java b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountController.java index 5185d6610..d5c219081 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountController.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/controllers/AccountController.java @@ -102,6 +102,7 @@ import org.whispersystems.textsecuregcm.util.ForwardedIpUtil; import org.whispersystems.textsecuregcm.util.Hex; import org.whispersystems.textsecuregcm.util.ImpossiblePhoneNumberException; import org.whispersystems.textsecuregcm.util.NonNormalizedPhoneNumberException; +import org.whispersystems.textsecuregcm.util.Optionals; import org.whispersystems.textsecuregcm.util.UsernameGenerator; import org.whispersystems.textsecuregcm.util.Util; import org.whispersystems.textsecuregcm.util.VerificationCode; @@ -832,14 +833,14 @@ public class AccountController { final Optional storedVerificationCode) { final String countryCode = Util.getCountryCode(number); final String region = Util.getRegion(number); - - final List tags = new ArrayList<>(); - tags.add(Tag.of(COUNTRY_CODE_TAG_NAME, countryCode)); - tags.add(Tag.of(REGION_TAG_NAME, region)); - tags.add(Tag.of(CHALLENGE_PRESENT_TAG_NAME, Boolean.toString(pushChallenge.isPresent()))); Optional storedPushChallenge = storedVerificationCode.map(StoredVerificationCode::getPushCode); - boolean match = pushChallenge.isPresent() && storedPushChallenge.isPresent() && pushChallenge.get().equals(storedPushChallenge.get()); - tags.add(Tag.of(CHALLENGE_MATCH_TAG_NAME, Boolean.toString(match))); + boolean match = Optionals.zipWith(pushChallenge, storedPushChallenge, String::equals).orElse(false); + Metrics.counter(PUSH_CHALLENGE_COUNTER_NAME, + COUNTRY_CODE_TAG_NAME, countryCode, + REGION_TAG_NAME, region, + CHALLENGE_PRESENT_TAG_NAME, Boolean.toString(pushChallenge.isPresent()), + CHALLENGE_MATCH_TAG_NAME, Boolean.toString(match)) + .increment(); return match; } diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/util/Optionals.java b/service/src/main/java/org/whispersystems/textsecuregcm/util/Optionals.java new file mode 100644 index 000000000..4e3e49038 --- /dev/null +++ b/service/src/main/java/org/whispersystems/textsecuregcm/util/Optionals.java @@ -0,0 +1,21 @@ +package org.whispersystems.textsecuregcm.util; + +import java.util.Optional; +import java.util.function.BiFunction; + +public class Optionals { + + private Optionals() {} + + /** + * Apply a function to two optional arguments, returning empty if either argument is empty + * + * @param optionalT Optional of type T + * @param optionalU Optional of type U + * @param fun Function of T and U that returns R + * @return The function applied to the values of optionalT and optionalU, or empty + */ + public static Optional zipWith(Optional optionalT, Optional optionalU, BiFunction fun) { + return optionalT.flatMap(t -> optionalU.map(u -> fun.apply(t, u))); + } +}