Beispiel #1
0
        public async Task SendBatchNotifications(NotificationType type, bool useHttps, string host, int?count = null)
        {
            if (!type.ShouldBeBatched())
            {
                throw new InvalidOperationException($"Notifications of type {type} can't be batched");
            }

            using (var db = _dbService.GetConnection())
            {
                var userIds = await db.QueryAsync <int>($@"
Select Id
From   Users
Where  {type.GetUserColumnName()} < @now", new { now = DateTime.UtcNow });

                var successfulUserIds = new List <int>();
                foreach (var userId in userIds)
                {
                    var message = new Models.Services.PushNotificationMessage(
                        type.GetTitle(count),
                        type.GetUrl(useHttps, host, userId),
                        type.GetBody(),
                        type.ToString(),
                        true);
                    if (await SendNotification(userId, message))
                    {
                        successfulUserIds.Add(userId);
                    }
                }

                await db.ExecuteAsync($@"
Update Users
Set    {type.GetUserColumnName()} = DateAdd(minute, NotificationsIntervalId * NotificationsIntervalValue, @now)
Where  Id In @successfulUserIds", new { successfulUserIds, columnName = type.GetUserColumnName(), now = DateTime.UtcNow });
            }
        }
Beispiel #2
0
        public async Task <bool> SendNotification(int userId, NotificationType type, bool useHttps, string host)
        {
            if (type.ShouldBeBatched())
            {
                throw new InvalidOperationException($"Notifications of type {type} should be batched");
            }

            using (var db = _dbService.GetConnection())
            {
                var shouldSendIt = await db.QueryFirstOrDefaultAsync <bool>($@"
Select Top 1 1
From   Users
Where  Id = @userId
And    {type.GetUserColumnName()} < @now", new { userId, now = DateTime.UtcNow });

                if (shouldSendIt)
                {
                    var message = new Models.Services.PushNotificationMessage(
                        type.GetTitle(),
                        type.GetUrl(useHttps, host, userId),
                        type.GetBody(),
                        type.ToString(),
                        true);
                    var success = await SendNotification(userId, message);

                    if (success)
                    {
                        await db.ExecuteAsync($@"
Update Users
Set    {type.GetUserColumnName()} = DateAdd(minute, NotificationsIntervalId * NotificationsIntervalValue, @now)
Where  Id = @userId", new { userId, now = DateTime.UtcNow });
                    }

                    return(success);
                }
            }

            return(false);
        }