Esempio n. 1
0
        public async Task <IEnumerable <DialogViewModel> > GetDialogs()
        {
            string identityName = Context.User.Identity.Name;
            User   user         = _chatRepo.GetUserByName(identityName);

            if (user != null)
            {
                List <Dialog> lastDialogs = await _chatRepo.GetLastDialogs(user).ToListAsync();

                List <DialogViewModel> dialogViewModels = lastDialogs.Select(c => c.CreatedBy == user ?
                                                                             new DialogViewModel
                {
                    DialogId         = c.Id,
                    LastActivity     = c.LastActivity.ToString("hh:mm tt", new CultureInfo("en-US")),
                    LastMessage      = c.LastMessage,
                    SenderId         = c.CreatedById,
                    RecieverName     = String.Format("{0} {1}", c.Reciever.FirstName, c.Reciever.LastName),
                    RecieverUsername = c.Reciever.UserName,
                    UnreadMessage    = _chatRepo.GetCountUnreadMessages(c.Id, user.Id),
                    Status           = c.Reciever.Status.ToString()
                } :
                                                                             new DialogViewModel
                {
                    DialogId         = c.Id,
                    LastActivity     = c.LastActivity.ToString("hh:mm tt", new CultureInfo("en-US")),
                    LastMessage      = c.LastMessage,
                    SenderId         = c.RecieverId,
                    RecieverName     = String.Format("{0} {1}", c.CreatedBy.FirstName, c.CreatedBy.LastName),
                    RecieverUsername = c.CreatedBy.UserName,
                    UnreadMessage    = _chatRepo.GetCountUnreadMessages(c.Id, user.Id),
                    Status           = c.CreatedBy.Status.ToString()
                }
                                                                             ).ToList();


                if (dialogViewModels != null)
                {
                    dialogs = dialogViewModels;
                    await CheckUserStatus(dialogViewModels, user);

                    return(dialogViewModels);
                }
            }
            return(null);
        }