Default to preferred server-local time if account has no phone number

This commit is contained in:
Ameya Lokare authored and Jon Chambers committed 2026-07-29 14:38:39 -04:00
1 parent 4440022796
commit d46818e465
2 files changed
+55 -50

No files matched your search

@@ -26,25 +26,24 @@ public class SchedulingUtil {
private static final String PARSED_TIMEZONE_COUNTER_NAME = MetricsUtil.name(SchedulingUtil.class, "parsedTimezone");
private static final String HAS_TIMEZONE_TAG_NAME = "hasTimezone";
/**
* Gets a present or future time at which to send a notification to a device associated with the given account. This
* is mainly intended to facilitate scheduling notifications such that they arrive during a recipient's waking hours.
* <p/>
* This method will attempt to use a timezone derived from the account's phone number to choose an appropriate time
* to send a notification. If a timezone cannot be derived from the account's phone number, then this method will
* default to the preferred time in the server's timezone.
*
* @param account the account that will receive the notification
* @param preferredTime the preferred local time (e.g. "noon") at which to deliver the notification
* @param clock a source of the current time
*
* @return the next time in the present or future at which to send a notification for the target account
*/
/// Gets a present or future time at which to send a notification to a device associated with the given account. This
/// is mainly intended to facilitate scheduling notifications such that they arrive during a recipient's waking hours.
///
/// This method will attempt to use a timezone derived from the account's phone number to choose an appropriate time
/// to send a notification. If a timezone cannot be derived from the account's phone number, or there is no phone number
/// associated with the account, then this method will default to the preferred time in the server's timezone.
///
/// @param account the account that will receive the notification
/// @param preferredTime the preferred local time (e.g. "noon") at which to deliver the notification
/// @param clock a source of the current time
///
/// @return the next time in the present or future at which to send a notification for the target account
public static Instant getNextRecommendedNotificationTime(final Account account,
final LocalTime preferredTime,
final Clock clock) {
final ZonedDateTime candidateNotificationTime = getZoneId(account.getNumber(), clock)
final ZonedDateTime candidateNotificationTime = account.getNumberOptional()
.flatMap(number -> getZoneId(number, clock))
.map(zoneId -> {
Metrics.counter(PARSED_TIMEZONE_COUNTER_NAME, HAS_TIMEZONE_TAG_NAME, String.valueOf(true)).increment();
return ZonedDateTime.now(clock.withZone(zoneId)).with(preferredTime);
@@ -12,59 +12,65 @@ import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Optional;
import com.google.i18n.phonenumbers.Phonenumber;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.whispersystems.textsecuregcm.storage.Account;
import org.whispersystems.textsecuregcm.util.TestClock;
import javax.annotation.Nullable;
class SchedulingUtilTest {
@Test
void getNextRecommendedNotificationTime() {
{
final Account account = mock(Account.class);
final Account account = mock(Account.class);
// The account has a phone number that can be resolved to a region with known timezones
when(account.getNumber()).thenReturn(PhoneNumberUtil.getInstance().format(
PhoneNumberUtil.getInstance().getExampleNumber("DE"), PhoneNumberUtil.PhoneNumberFormat.E164));
// The account has a phone number that can be resolved to a region with known timezones
when(account.getNumberOptional()).thenReturn(Optional.of(PhoneNumberUtil.getInstance().format(
PhoneNumberUtil.getInstance().getExampleNumber("DE"), PhoneNumberUtil.PhoneNumberFormat.E164)));
final ZoneId berlinZoneId = ZoneId.of("Europe/Berlin");
final ZonedDateTime beforeNotificationTime = ZonedDateTime.now(berlinZoneId).with(LocalTime.of(13, 0));
final ZoneId berlinZoneId = ZoneId.of("Europe/Berlin");
final ZonedDateTime beforeNotificationTime = ZonedDateTime.now(berlinZoneId).with(LocalTime.of(13, 0));
assertEquals(
beforeNotificationTime.with(LocalTime.of(14, 0)).toInstant(),
SchedulingUtil.getNextRecommendedNotificationTime(account, LocalTime.of(14, 0),
Clock.fixed(beforeNotificationTime.toInstant(), berlinZoneId)));
assertEquals(
beforeNotificationTime.with(LocalTime.of(14, 0)).toInstant(),
SchedulingUtil.getNextRecommendedNotificationTime(account, LocalTime.of(14, 0),
Clock.fixed(beforeNotificationTime.toInstant(), berlinZoneId)));
final ZonedDateTime afterNotificationTime = ZonedDateTime.now(berlinZoneId).with(LocalTime.of(15, 0));
final ZonedDateTime afterNotificationTime = ZonedDateTime.now(berlinZoneId).with(LocalTime.of(15, 0));
assertEquals(
afterNotificationTime.with(LocalTime.of(14, 0)).plusDays(1).toInstant(),
SchedulingUtil.getNextRecommendedNotificationTime(account, LocalTime.of(14, 0),
Clock.fixed(afterNotificationTime.toInstant(), berlinZoneId)));
}
assertEquals(
afterNotificationTime.with(LocalTime.of(14, 0)).plusDays(1).toInstant(),
SchedulingUtil.getNextRecommendedNotificationTime(account, LocalTime.of(14, 0),
Clock.fixed(afterNotificationTime.toInstant(), berlinZoneId)));
}
{
final Account account = mock(Account.class);
@ParameterizedTest
@NullSource
@ValueSource(strings = {"Not a parseable number"})
void getNextRecommendedNotificationTimeDefaultsToPreferredTime(@Nullable final String number) {
final Account account = mock(Account.class);
// The account does not have a phone number that can be connected to a region/time zone
when(account.getNumber()).thenReturn("Not a parseable number");
// The account does not have a phone number that can be connected to a region/time zone
when(account.getNumberOptional()).thenReturn(Optional.ofNullable(number));
final ZonedDateTime beforeNotificationTime = ZonedDateTime.now(ZoneId.systemDefault()).with(LocalTime.of(13, 59));
final LocalTime preferredNotificationTime = LocalTime.of(14, 0);
final ZonedDateTime beforeNotificationTime = ZonedDateTime.now(ZoneId.systemDefault()).with(LocalTime.of(13, 59));
final LocalTime preferredNotificationTime = LocalTime.of(14, 0);
assertEquals(
beforeNotificationTime.with(preferredNotificationTime).toInstant(),
SchedulingUtil.getNextRecommendedNotificationTime(account, preferredNotificationTime,
Clock.fixed(beforeNotificationTime.toInstant(), ZoneId.systemDefault())));
assertEquals(
beforeNotificationTime.with(preferredNotificationTime).toInstant(),
SchedulingUtil.getNextRecommendedNotificationTime(account, preferredNotificationTime,
Clock.fixed(beforeNotificationTime.toInstant(), ZoneId.systemDefault())));
final ZonedDateTime afterNotificationTime = ZonedDateTime.now(ZoneId.systemDefault()).with(LocalTime.of(14, 1));
final ZonedDateTime afterNotificationTime = ZonedDateTime.now(ZoneId.systemDefault()).with(LocalTime.of(14, 1));
assertEquals(
afterNotificationTime.with(preferredNotificationTime).plusDays(1).toInstant(),
SchedulingUtil.getNextRecommendedNotificationTime(account, preferredNotificationTime,
Clock.fixed(afterNotificationTime.toInstant(), ZoneId.systemDefault())));
}
assertEquals(
afterNotificationTime.with(preferredNotificationTime).plusDays(1).toInstant(),
SchedulingUtil.getNextRecommendedNotificationTime(account, preferredNotificationTime,
Clock.fixed(afterNotificationTime.toInstant(), ZoneId.systemDefault())));
}
@Test
@@ -72,8 +78,8 @@ class SchedulingUtilTest {
final Account account = mock(Account.class);
// The account has a phone number that can be resolved to a region with known timezones
when(account.getNumber()).thenReturn(PhoneNumberUtil.getInstance().format(
PhoneNumberUtil.getInstance().getExampleNumber("DE"), PhoneNumberUtil.PhoneNumberFormat.E164));
when(account.getNumberOptional()).thenReturn(Optional.of(PhoneNumberUtil.getInstance().format(
PhoneNumberUtil.getInstance().getExampleNumber("DE"), PhoneNumberUtil.PhoneNumberFormat.E164)));
final LocalDateTime afterNotificationTime = LocalDateTime.of(2025, 3, 29, 15, 0);
final ZoneId berlinZoneId = ZoneId.of("Europe/Berlin");