示例#1
0
        public static MobilePushTokenDto FromDomainObject(MobilePushToken source)
        {
            var result = SimpleMapper.Map(source, new MobilePushTokenDto());

            if (source.LastWakeup != default)
            {
                result.LastWakeup = source.LastWakeup;
            }
            else
            {
                result.LastWakeup = null;
            }

            return(result);
        }
示例#2
0
        public void Should_not_wakeup_if_already_scheduled()
        {
            var now = SystemClock.Instance.GetCurrentInstant();

            var simulatedClock = A.Fake <IClock>();

            A.CallTo(() => simulatedClock.GetCurrentInstant())
            .Returns(now);

            var token = new MobilePushToken
            {
                LastWakeup = now.Plus(Duration.FromMinutes(25))
            };

            var result = token.GetNextWakeupTime(simulatedClock);

            Assert.Null(result);
        }
示例#3
0
        public void Should_wakeup_as_soon_as_possible()
        {
            var now = SystemClock.Instance.GetCurrentInstant();

            var simulatedClock = A.Fake <IClock>();

            A.CallTo(() => simulatedClock.GetCurrentInstant())
            .Returns(now);

            var token = new MobilePushToken
            {
                LastWakeup = now.Minus(Duration.FromMinutes(25))
            };

            var result = token.GetNextWakeupTime(simulatedClock);

            Assert.Equal(now.Plus(Duration.FromMinutes(5)), result);
        }
示例#4
0
        public void Should_wakeup_now_when_not_waked_up_for_a_while()
        {
            var now = SystemClock.Instance.GetCurrentInstant();

            var simulatedClock = A.Fake <IClock>();

            A.CallTo(() => simulatedClock.GetCurrentInstant())
            .Returns(now);

            var token = new MobilePushToken
            {
                LastWakeup = now.Minus(Duration.FromMinutes(35))
            };

            var result = token.GetNextWakeupTime(simulatedClock);

            var expected = now.Plus(MobilePushExtensions.WakeupDelay);

            Assert.Equal(expected, result);
        }
示例#5
0
        public static Instant?GetNextWakeupTime(this MobilePushToken token, IClock clock)
        {
            var now = clock.GetCurrentInstant().Plus(WakeupDelay);

            if (token.LastWakeup > now)
            {
                return(null);
            }

            var nextWakeup = now;

            var timeSinceLastWakeUp = now - token.LastWakeup;

            if (timeSinceLastWakeUp < TimeBetweenWakeup)
            {
                nextWakeup = token.LastWakeup + TimeBetweenWakeup;
            }

            return(nextWakeup);
        }
示例#6
0
        private async Task TryWakeupAsync(UserNotification notification, MobilePushToken token,
                                          CancellationToken ct)
        {
            var nextWakeup = token.GetNextWakeupTime(clock);

            if (nextWakeup == null)
            {
                return;
            }

            var dummyNotification = new UserNotification
            {
                AppId        = notification.AppId,
                UserId       = notification.UserId,
                UserLanguage = notification.UserLanguage
            };

            var wakeupJob = new MobilePushJob(dummyNotification, null, token.Token, token.DeviceType, false);

            await userNotificationQueue.ScheduleAsync(
                wakeupJob.ScheduleKey,
                wakeupJob,
                nextWakeup.Value,
                false, ct);

            try
            {
                var command = new UpdateMobileWakeupTime
                {
                    Token     = token.Token,
                    Timestamp = nextWakeup.Value
                };

                await userStore.UpsertAsync(notification.AppId, notification.UserId, command, ct);
            }
            catch (Exception ex)
            {
                log.LogWarning(ex, "Failed to wakeup device.");
            }
        }
示例#7
0
 public static MobilePushTokenDto FromDomainObject(MobilePushToken source)
 {
     return(SimpleMapper.Map(source, new MobilePushTokenDto()));
 }