Exemple #1
0
        public Task <int> CreateOrUpdateAsync(CreateOrUpdateNotification model)
        {
            var notification = _mapper.Map <Notification>(model);

            notification.CreatedDate = notification.ModifiedDate = DateTime.UtcNow;

            return(_notificationsRepository.CreateOrUpdateAsync(notification));
        }
        public async Task <IActionResult> SetTasksAsNotCompleted([FromBody] SetTasksAsNotCompletedDto dto)
        {
            if (dto == null)
            {
                return(BadRequest());
            }

            int userId;

            try
            {
                userId = IdentityHelper.GetUserId(User);
            }
            catch (UnauthorizedAccessException)
            {
                return(Unauthorized());
            }

            bool nonPrivateTasksWereUncompleted = await _listService.SetTasksAsNotCompletedAsync(dto.ListId, userId);

            if (nonPrivateTasksWereUncompleted)
            {
                // Notify
                var usersToBeNotified = await _userService.GetToBeNotifiedOfListChangeAsync(dto.ListId, userId);

                if (usersToBeNotified.Any())
                {
                    var currentUser = await _userService.GetAsync(userId);

                    SimpleList list = await _listService.GetAsync(dto.ListId);

                    foreach (var user in usersToBeNotified)
                    {
                        CultureInfo.CurrentCulture = new CultureInfo(user.Language, false);
                        var message = _localizer["UncompletedAllTasksNotification", IdentityHelper.GetUserName(User), list.Name];

                        var createNotificationDto = new CreateOrUpdateNotification(user.Id, userId, list.Id, null, message);
                        var notificationId        = await _notificationService.CreateOrUpdateAsync(createNotificationDto);

                        var pushNotification = new PushNotification
                        {
                            SenderImageUri = currentUser.ImageUri,
                            UserId         = user.Id,
                            Application    = "To Do Assistant",
                            Message        = message,
                            OpenUrl        = GetNotificationsPageUrl(notificationId)
                        };

                        _senderService.Enqueue(pushNotification);
                    }
                }
            }

            return(NoContent());
        }
Exemple #3
0
        public async Task <IActionResult> BulkCreate([FromBody] BulkCreate dto)
        {
            if (dto == null)
            {
                return(BadRequest());
            }

            try
            {
                dto.UserId = IdentityHelper.GetUserId(User);
            }
            catch (UnauthorizedAccessException)
            {
                return(Unauthorized());
            }

            IEnumerable <CreatedTask> createdTasks = await _taskService.BulkCreateAsync(dto, _bulkCreateValidator);

            // Notify
            var usersToBeNotified = await _userService.GetToBeNotifiedOfListChangeAsync(dto.ListId, dto.UserId, dto.TasksArePrivate);

            if (usersToBeNotified.Any())
            {
                var currentUser = await _userService.GetAsync(dto.UserId);

                SimpleList list = await _listService.GetAsync(dto.ListId);

                foreach (CreatedTask task in createdTasks)
                {
                    foreach (var user in usersToBeNotified)
                    {
                        CultureInfo.CurrentCulture = new CultureInfo(user.Language, false);
                        var message = _localizer["CreatedTaskNotification", IdentityHelper.GetUserName(User), task.Name, list.Name];

                        var createNotificationDto = new CreateOrUpdateNotification(user.Id, dto.UserId, list.Id, task.Id, message);
                        var notificationId        = await _notificationService.CreateOrUpdateAsync(createNotificationDto);

                        var pushNotification = new PushNotification
                        {
                            SenderImageUri = currentUser.ImageUri,
                            UserId         = user.Id,
                            Application    = "To Do Assistant",
                            Message        = message,
                            OpenUrl        = GetNotificationsPageUrl(notificationId)
                        };

                        _senderService.Enqueue(pushNotification);
                    }
                }
            }

            return(StatusCode(201));
        }
        public async Task <IActionResult> Leave(int id)
        {
            int userId;

            try
            {
                userId = IdentityHelper.GetUserId(User);
            }
            catch (UnauthorizedAccessException)
            {
                return(Unauthorized());
            }

            bool shareWasAccepted = await _listService.LeaveAsync(id, userId);

            // Notify if joined in the first place
            if (shareWasAccepted)
            {
                var usersToBeNotified = await _userService.GetToBeNotifiedOfListChangeAsync(id, userId);

                if (usersToBeNotified.Any())
                {
                    var currentUser = await _userService.GetAsync(userId);

                    SimpleList list = await _listService.GetAsync(id);

                    foreach (var user in usersToBeNotified)
                    {
                        CultureInfo.CurrentCulture = new CultureInfo(user.Language, false);
                        var message = _localizer["LeftListNotification", IdentityHelper.GetUserName(User), list.Name];

                        var createNotificationDto = new CreateOrUpdateNotification(user.Id, userId, list.Id, null, message);
                        var notificationId        = await _notificationService.CreateOrUpdateAsync(createNotificationDto);

                        var pushNotification = new PushNotification
                        {
                            SenderImageUri = currentUser.ImageUri,
                            UserId         = user.Id,
                            Application    = "To Do Assistant",
                            Message        = message,
                            OpenUrl        = GetNotificationsPageUrl(notificationId)
                        };

                        _senderService.Enqueue(pushNotification);
                    }
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> Share([FromBody] ShareList dto)
        {
            if (dto == null)
            {
                return(BadRequest());
            }

            try
            {
                dto.UserId = IdentityHelper.GetUserId(User);
            }
            catch (UnauthorizedAccessException)
            {
                return(Unauthorized());
            }

            foreach (ShareUserAndPermission removedShare in dto.RemovedShares)
            {
                // Notify
                if (await _userService.CheckIfUserCanBeNotifiedOfListChangeAsync(dto.ListId, removedShare.UserId))
                {
                    var currentUser = await _userService.GetAsync(dto.UserId);

                    var user = await _userService.GetAsync(removedShare.UserId);

                    SimpleList list = await _listService.GetAsync(dto.ListId);

                    CultureInfo.CurrentCulture = new CultureInfo(user.Language, false);
                    var message = _localizer["RemovedShareNotification", IdentityHelper.GetUserName(User), list.Name];

                    var createNotificationDto = new CreateOrUpdateNotification(user.Id, dto.UserId, null, null, message);
                    var notificationId        = await _notificationService.CreateOrUpdateAsync(createNotificationDto);

                    var pushNotification = new PushNotification
                    {
                        SenderImageUri = currentUser.ImageUri,
                        UserId         = user.Id,
                        Application    = "To Do Assistant",
                        Message        = message,
                        OpenUrl        = GetNotificationsPageUrl(notificationId)
                    };

                    _senderService.Enqueue(pushNotification);
                }
            }

            await _listService.ShareAsync(dto, _shareValidator);

            return(NoContent());
        }
Exemple #6
0
        public async Task <IActionResult> Uncomplete([FromBody] CompleteUncomplete dto)
        {
            if (dto == null)
            {
                return(BadRequest());
            }

            try
            {
                dto.UserId = IdentityHelper.GetUserId(User);
            }
            catch (UnauthorizedAccessException)
            {
                return(Unauthorized());
            }

            SimpleTask task = await _taskService.UncompleteAsync(dto);

            // Notify
            var usersToBeNotified = await _userService.GetToBeNotifiedOfListChangeAsync(task.ListId, dto.UserId, task.Id);

            if (usersToBeNotified.Any())
            {
                var currentUser = await _userService.GetAsync(dto.UserId);

                SimpleList list = await _listService.GetAsync(task.ListId);

                foreach (var user in usersToBeNotified)
                {
                    CultureInfo.CurrentCulture = new CultureInfo(user.Language, false);
                    string message = _localizer["UncompletedTaskNotification", IdentityHelper.GetUserName(User), task.Name, list.Name];

                    var updateNotificationDto = new CreateOrUpdateNotification(user.Id, dto.UserId, list.Id, task.Id, message);
                    var notificationId        = await _notificationService.CreateOrUpdateAsync(updateNotificationDto);

                    var pushNotification = new PushNotification
                    {
                        SenderImageUri = currentUser.ImageUri,
                        UserId         = user.Id,
                        Application    = "To Do Assistant",
                        Message        = message,
                        OpenUrl        = GetNotificationsPageUrl(notificationId)
                    };

                    _senderService.Enqueue(pushNotification);
                }
            }

            return(NoContent());
        }
Exemple #7
0
        public async Task <IActionResult> Delete(int id)
        {
            int userId;

            try
            {
                userId = IdentityHelper.GetUserId(User);
            }
            catch (UnauthorizedAccessException)
            {
                return(Unauthorized());
            }

            SimpleTask deletedTask = await _taskService.DeleteAsync(id, userId);

            // Notify
            var usersToBeNotified = await _userService.GetToBeNotifiedOfListChangeAsync(deletedTask.ListId, userId, deletedTask.PrivateToUserId == userId);

            if (usersToBeNotified.Any())
            {
                var currentUser = await _userService.GetAsync(userId);

                SimpleList list = await _listService.GetAsync(deletedTask.ListId);

                foreach (var user in usersToBeNotified)
                {
                    CultureInfo.CurrentCulture = new CultureInfo(user.Language, false);
                    var message = _localizer["RemovedTaskNotification", IdentityHelper.GetUserName(User), deletedTask.Name, list.Name];

                    var createNotificationDto = new CreateOrUpdateNotification(user.Id, userId, list.Id, null, message);
                    var notificationId        = await _notificationService.CreateOrUpdateAsync(createNotificationDto);

                    var pushNotification = new PushNotification
                    {
                        SenderImageUri = currentUser.ImageUri,
                        UserId         = user.Id,
                        Application    = "To Do Assistant",
                        Message        = message,
                        OpenUrl        = GetNotificationsPageUrl(notificationId)
                    };

                    _senderService.Enqueue(pushNotification);
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> Update([FromBody] UpdateList dto)
        {
            if (dto == null)
            {
                return(BadRequest());
            }

            try
            {
                dto.UserId = IdentityHelper.GetUserId(User);
            }
            catch (UnauthorizedAccessException)
            {
                return(Unauthorized());
            }

            UpdateListOriginal original = await _listService.UpdateAsync(dto, _updateValidator);

            // Notify
            var usersToBeNotified = await _userService.GetToBeNotifiedOfListChangeAsync(original.Id, dto.UserId);

            if (usersToBeNotified.Any())
            {
                var currentUser = await _userService.GetAsync(dto.UserId);

                if (dto.Name != original.Name || dto.Icon != original.Icon)
                {
                    var resourceKey = "UpdatedListNotification";

                    if (dto.Name != original.Name && dto.Icon == original.Icon)
                    {
                        resourceKey = "UpdatedListNameNotification";
                    }
                    else if (dto.Name == original.Name && dto.Icon != original.Icon)
                    {
                        resourceKey = "UpdatedListIconNotification";
                    }

                    foreach (var user in usersToBeNotified)
                    {
                        CultureInfo.CurrentCulture = new CultureInfo(user.Language, false);
                        var message = _localizer[resourceKey, IdentityHelper.GetUserName(User), original.Name];

                        var createNotificationDto = new CreateOrUpdateNotification(user.Id, dto.UserId, original.Id, null, message);
                        var notificationId        = await _notificationService.CreateOrUpdateAsync(createNotificationDto);

                        var pushNotification = new PushNotification
                        {
                            SenderImageUri = currentUser.ImageUri,
                            UserId         = user.Id,
                            Application    = "To Do Assistant",
                            Message        = message,
                            OpenUrl        = GetNotificationsPageUrl(notificationId)
                        };

                        _senderService.Enqueue(pushNotification);
                    }
                }
            }

            return(NoContent());
        }
Exemple #9
0
        public async Task <IActionResult> Update([FromBody] UpdateTask dto)
        {
            if (dto == null)
            {
                return(BadRequest());
            }

            try
            {
                dto.UserId = IdentityHelper.GetUserId(User);
            }
            catch (UnauthorizedAccessException)
            {
                return(Unauthorized());
            }

            SimpleTask originalTask = await _taskService.GetAsync(dto.Id);

            await _taskService.UpdateAsync(dto, _updateValidator);

            // Notify
            if (dto.ListId == originalTask.ListId)
            {
                var usersToBeNotified = await _userService.GetToBeNotifiedOfListChangeAsync(dto.ListId, dto.UserId, dto.Id);

                if (usersToBeNotified.Any())
                {
                    var currentUser = await _userService.GetAsync(dto.UserId);

                    SimpleList list = await _listService.GetAsync(dto.ListId);

                    foreach (User user in usersToBeNotified)
                    {
                        CultureInfo.CurrentCulture = new CultureInfo(user.Language, false);
                        var message = _localizer["UpdatedTaskNotification", IdentityHelper.GetUserName(User), originalTask.Name, list.Name];

                        var createNotificationDto = new CreateOrUpdateNotification(user.Id, dto.UserId, list.Id, dto.Id, message);
                        var notificationId        = await _notificationService.CreateOrUpdateAsync(createNotificationDto);

                        var pushNotification = new PushNotification
                        {
                            SenderImageUri = currentUser.ImageUri,
                            UserId         = user.Id,
                            Application    = "To Do Assistant",
                            Message        = message,
                            OpenUrl        = GetNotificationsPageUrl(notificationId)
                        };

                        _senderService.Enqueue(pushNotification);
                    }
                }
            }
            else
            {
                SimpleList oldList = await _listService.GetAsync(originalTask.ListId);

                SimpleList newList = await _listService.GetAsync(dto.ListId);

                var currentUserName = IdentityHelper.GetUserName(User);

                var usersToBeNotifiedOfRemoval = await _userService.GetToBeNotifiedOfListChangeAsync(oldList.Id, dto.UserId, dto.Id);

                if (usersToBeNotifiedOfRemoval.Any())
                {
                    var currentUser = await _userService.GetAsync(dto.UserId);

                    foreach (var user in usersToBeNotifiedOfRemoval)
                    {
                        CultureInfo.CurrentCulture = new CultureInfo(user.Language, false);
                        var message = _localizer["RemovedTaskNotification", currentUserName, originalTask.Name, oldList.Name];

                        var createNotificationDto = new CreateOrUpdateNotification(user.Id, dto.UserId, oldList.Id, null, message);
                        var notificationId        = await _notificationService.CreateOrUpdateAsync(createNotificationDto);

                        var pushNotification = new PushNotification
                        {
                            SenderImageUri = currentUser.ImageUri,
                            UserId         = user.Id,
                            Application    = "To Do Assistant",
                            Message        = message,
                            OpenUrl        = GetNotificationsPageUrl(notificationId)
                        };

                        _senderService.Enqueue(pushNotification);
                    }
                }

                var usersToBeNotifiedOfCreation = await _userService.GetToBeNotifiedOfListChangeAsync(newList.Id, dto.UserId, dto.Id);

                if (usersToBeNotifiedOfCreation.Any())
                {
                    var currentUser = await _userService.GetAsync(dto.UserId);

                    foreach (var user in usersToBeNotifiedOfCreation)
                    {
                        CultureInfo.CurrentCulture = new CultureInfo(user.Language, false);
                        var message = _localizer["CreatedTaskNotification", currentUserName, dto.Name, newList.Name];

                        var createNotificationDto = new CreateOrUpdateNotification(user.Id, dto.UserId, newList.Id, dto.Id, message);
                        var notificationId        = await _notificationService.CreateOrUpdateAsync(createNotificationDto);

                        var pushNotification = new PushNotification
                        {
                            SenderImageUri = currentUser.ImageUri,
                            UserId         = user.Id,
                            Application    = "To Do Assistant",
                            Message        = message,
                            OpenUrl        = GetNotificationsPageUrl(notificationId)
                        };

                        _senderService.Enqueue(pushNotification);
                    }
                }
            }

            // Notify if assigned to another user
            if (dto.AssignedToUserId.HasValue &&
                dto.AssignedToUserId.Value != originalTask.AssignedToUserId &&
                dto.AssignedToUserId.Value != dto.UserId)
            {
                if (await _userService.CheckIfUserCanBeNotifiedOfListChangeAsync(dto.ListId, dto.AssignedToUserId.Value))
                {
                    var assignedUser = await _userService.GetAsync(dto.AssignedToUserId.Value);

                    SimpleList list = await _listService.GetAsync(dto.ListId);

                    CultureInfo.CurrentCulture = new CultureInfo(assignedUser.Language, false);
                    var message = _localizer["AssignedTaskNotification", IdentityHelper.GetUserName(User), originalTask.Name, list.Name];

                    var createNotificationDto = new CreateOrUpdateNotification(assignedUser.Id, dto.UserId, list.Id, dto.Id, message);
                    var notificationId        = await _notificationService.CreateOrUpdateAsync(createNotificationDto);

                    var pushNotification = new PushNotification
                    {
                        UserId      = assignedUser.Id,
                        Application = "To Do Assistant",
                        Message     = message,
                        OpenUrl     = GetNotificationsPageUrl(notificationId)
                    };

                    _senderService.Enqueue(pushNotification);
                }
            }

            return(NoContent());
        }