Example #1
0
        public async Task <IActionResult> Details(int messageId, CancellationToken cancellationToken)
        {
            var message = await MailAppDbContext.Messages
                          .Include(x => x.MessageAttachments)
                          .Include(x => x.MessagePersons)
                          .ThenInclude(x => x.Account)
                          .Include(x => x.MessagePersons)
                          .ThenInclude(x => x.Type)
                          .SingleOrDefaultAsync(x => x.Id == messageId, cancellationToken);

            if (message == null)
            {
                return(NotFound());
            }

            var currentAccount = await AccountProvider.GetAccountForCurrentUser(cancellationToken);

            message.MarkAsRead(currentAccount);

            await MailAppDbContext.SaveChangesAsync(cancellationToken);

            var viewModel = new MessageViewModel(message, currentAccount);

            return(PartialView(viewModel));
        }
Example #2
0
        public async Task <IActionResult> AddGroup(AddGroupViewModel viewModel, CancellationToken cancellationToken)
        {
            var account = await AccountProvider.GetAccountForCurrentUser(cancellationToken);

            var group = new Group(viewModel.Name, account);

            MailAppDbContext.Groups.Add(group);
            await MailAppDbContext.SaveChangesAsync(cancellationToken);

            return(RedirectToAction(nameof(Details), new { groupId = group.Id }));
        }
Example #3
0
        public async Task <IActionResult> RemoveMessage(RemoveMessageViewModel m, CancellationToken cancellationToken)
        {
            var message = await MailAppDbContext.Messages.SingleOrDefaultAsync(x => x.Id == m.MessageId, cancellationToken);

            if (message == null)
            {
                return(BadRequest());
            }

            MailAppDbContext.Messages.Remove(message);
            await MailAppDbContext.SaveChangesAsync(cancellationToken);

            return(RedirectToAction(nameof(Index)));
        }
Example #4
0
        public async Task <IActionResult> MarkAsUnread(MarkAsUnreadViewModel m, CancellationToken cancellationToken)
        {
            var message = await MailAppDbContext.Messages.SingleOrDefaultAsync(x => x.Id == m.MessageId, cancellationToken);

            if (message == null)
            {
                return(BadRequest());
            }

            var currentAccount = await AccountProvider.GetAccountForCurrentUser(cancellationToken);

            message.MarkAsUnread(currentAccount);
            await MailAppDbContext.SaveChangesAsync(cancellationToken);

            return(RedirectToAction(nameof(Index)));
        }
Example #5
0
        public async Task <IActionResult> RemoveGroup(RemoveGroupViewModel viewModel, CancellationToken cancellationToken)
        {
            var account = await AccountProvider.GetAccountForCurrentUser(cancellationToken);

            var group = await GetGroup(viewModel.GroupId, cancellationToken);

            if (group == null)
            {
                return(BadRequest());
            }

            if (!group.IsOwner(account))
            {
                return(Forbid());
            }

            MailAppDbContext.Remove(group);
            await MailAppDbContext.SaveChangesAsync(cancellationToken);

            return(RedirectToAction(nameof(List)));
        }
Example #6
0
        public async Task <IActionResult> AddMember(AddMemberViewModel viewModel, CancellationToken cancellationToken)
        {
            if (!ModelState.IsValid)
            {
                return(View(viewModel));
            }

            var account = await AccountProvider.GetAccountForCurrentUser(cancellationToken);

            var group = await GetGroup(viewModel.GroupId, cancellationToken);

            if (group == null)
            {
                ModelState.AddModelError(nameof(viewModel.GroupId), "Group does not exist");
                return(View(viewModel));
            }

            if (!group.IsOwner(account))
            {
                ModelState.AddModelError(String.Empty, "Only group admin can delete it's members.");
                return(View(viewModel));
            }


            var newMember = await MailAppDbContext.Accounts.SingleOrDefaultAsync(x => x.Nick == viewModel.AccountNick, cancellationToken);

            if (newMember == null)
            {
                ModelState.AddModelError(nameof(viewModel.AccountNick), "There is no Account with such nick.");
                return(View(viewModel));
            }

            group.AddAccount(newMember);
            await MailAppDbContext.SaveChangesAsync(cancellationToken);

            return(RedirectToAction(nameof(Details), new { groupId = viewModel.GroupId }));
        }
Example #7
0
        public async Task <IActionResult> NewMessage(NewMessageViewModel request, CancellationToken cancellationToken)
        {
            await ReadLatestReceivers(request, cancellationToken);

            if (!ModelState.IsValid)
            {
                return(View(request));
            }

            if (string.IsNullOrEmpty(request.Receiver) && string.IsNullOrEmpty(request.Cc) && string.IsNullOrEmpty(request.Bcc))
            {
                ModelState.AddModelError(nameof(request.Receiver), "There is no receiver.");
                return(View(request));
            }

            if (string.IsNullOrEmpty(request.Subject) && string.IsNullOrEmpty(request.Text))
            {
                ModelState.AddModelError(nameof(request.Subject), "Message should have subject or text");
                return(View(request));
            }

            var message = new Message
            {
                Subject      = request.Subject,
                Text         = request.Text,
                SentDate     = DateTime.Now,
                Notification = request.Notification,
            };

            var sender = await AccountProvider.GetAccountForCurrentUser(cancellationToken);

            foreach (var i in request.FileForm ?? new IFormFile[0])
            {
                var fileName    = i.FileName;
                var contentType = i.ContentType;
                var blobId      = Guid.NewGuid().ToString();
                message.AddAttachments(new MessageAttachment(blobId, fileName, contentType));

                var blobClient = ContainerClient.GetBlobClient(blobId);
                await blobClient.UploadAsync(i.OpenReadStream(), true, cancellationToken);
            }

            if (sender == null)
            {
                return(BadRequest());
            }

            message.SetSender(sender);

            var flag = true;

            async Task SetPersons(String addresses, Action <Account> f1, Action <Group> f2)
            {
                foreach (var address in (addresses ?? String.Empty).Split(";", StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()).ToArray())
                {
                    var group = await MailAppDbContext.Groups.Include(x => x.GroupAccounts).ThenInclude(x => x.Account).SingleOrDefaultAsync(x => x.Name == address, cancellationToken);

                    if (group != null)
                    {
                        f2(group);
                    }
                    else
                    {
                        var account = await MailAppDbContext.Accounts.SingleOrDefaultAsync(x => x.Email == address, cancellationToken);

                        if (account != null)
                        {
                            f1(account);
                        }
                        else
                        {
                            flag = false;
                        }
                    }
                }
            }

            await SetPersons(request.Receiver, account => message.AddReceiver(account), group => message.AddReceiver(group));

            if (flag == false)
            {
                ModelState.AddModelError(nameof(request.Receiver), "Invadlid mail or group name");
                return(View(request));
            }

            await SetPersons(request.Cc, account => message.AddCc(account), group => message.AddCc(group));

            if (flag == false)
            {
                ModelState.AddModelError(nameof(request.Cc), "Invadlid mail or group name");
                return(View(request));
            }

            await SetPersons(request.Bcc, account => message.AddReceiver(account), group => message.AddBcc(group));

            if (flag == false)
            {
                ModelState.AddModelError(nameof(request.Bcc), "Invadlid mail or group name");
                return(View(request));
            }

            MailAppDbContext.Messages.Add(message);
            await MailAppDbContext.SaveChangesAsync(cancellationToken);

            TelemetryClient.TrackEvent("Message sent");
            return(RedirectToAction(nameof(Index)));
        }