Exemple #1
0
        public async Task markAsRead(int Id)
        {
            User      currentUser = Users.FirstOrDefault(u => u.ConnectionId == Context.ConnectionId && u.Id == GetUserId());
            DialogDTO dialog      = await dialogService.GetById(currentUser.DialogId);

            await dialogService.MarkMessagesAsRead(dialog.Messages.Where(m => m.ToId == currentUser.Id && !m.Read && m.Id <= Id).ToList());
        }
 public async Task AddUsersToDialog(DialogDTO dialog, List <UserDTO> accounts)
 {
     foreach (UserDTO acc in accounts)
     {
         await AddUserToDialog(dialog, acc);
     }
 }
Exemple #3
0
        public async Task sendMessage(int userId, string text)
        {
            int       currentUserId = GetUserId();
            DialogDTO dialog        = await dialogService.GetDialogBetweenUsers(new int[] { currentUserId, userId });

            if (dialog != null && !string.IsNullOrEmpty(text))
            {
                var message = new MessageDTO()
                {
                    ToId   = userId,
                    Body   = text.Trim(),
                    FromId = currentUserId,
                    Date   = DateTime.Now
                };
                await dialogService.WriteMessage(dialog.Id, message);

                dialog = await dialogService.GetDialogBetweenUsers(new int[] { currentUserId, userId });

                // обновляем окно диалога у тех кто его открыл
                var participantsForRedreshDialog = Users.Where(u => dialog.Participants.Any(p => p.ProfileId == u.Id) && u.DialogId == dialog.Id).Select(u => u.ConnectionId).ToList();
                Clients.Clients(participantsForRedreshDialog).refreshDialog(dialog.Messages);

                // обновляем список диалогов у отправителя и получателя
                var participantsForRedreshDialogs = Users.Where(u => dialog.Participants.Any(p => p.ProfileId == u.Id));
                foreach (User p in participantsForRedreshDialogs)
                {
                    await GetDialogsPanel(p);
                }
            }
        }
        public async Task <ActionResult> OpenDialog(int id, int page = 1)
        {
            DialogDTO dialog = await DialogService.GetDialogById(id);

            List <UserDTO> userList = await DialogService.GetUsersInDialog(dialog);

            UserDTO user = await UserService.GetUserById(User.Identity.GetUserId());

            if (userList.Find(x => x.Id == user.Id) == null)
            {
                return(RedirectToAction("AccessDenied", "Home"));
            }

            List <MessageDTO> list = (await MessageService
                                      .ListDialogMessages(new MessageFilter {
                Chat = dialog
            }, page))
                                     .ResultMessages.ToList();

            if (Request.IsAjaxRequest())
            {
                return(PartialView("_MessageItems", list));
            }

            return(View(new OpenDialogModel
            {
                Dialog = dialog,
                Messages = list,
                DialogId = dialog.Id,
                Page = page,
                Accounts = userList
            }));
        }
Exemple #5
0
        public IEnumerable <DialogDTO> GetDialogs(string currentUser)
        {
            var dialogsdto = new List <DialogDTO>();

            foreach (var dialog in DB.Messages.GetDialogs(currentUser))
            {
                var dialogDto = new DialogDTO
                {
                    DialogId    = dialog.DialogId,
                    RecipientId = dialog.RecipientId,
                    SenderId    = dialog.SenderId
                };
                var messagesdto = new List <MessageDTO>();
                foreach (var message in dialog.Messages)
                {
                    var messageDto = new MessageDTO
                    {
                        MessageId   = message.MessageId,
                        RecipientId = message.RecipientId,
                        SenderId    = message.SenderId,
                        DialogId    = message.DialogId,
                        Text        = message.Text,
                        Time        = message.Time
                    };
                    messagesdto.Add(messageDto);
                }
                dialogDto.Messages = messagesdto;
                dialogsdto.Add(dialogDto);
            }
            return(dialogsdto);
        }
        public async Task <ActionResult> Leave(DialogDTO Dialog)
        {
            UserDTO user = await UserService.GetUserById(User.Identity.GetUserId());

            await DialogService.RemoveUserFromDialog(Dialog, user);

            return(RedirectToAction("Index"));
        }
        public async Task EditDialogName(DialogDTO dialogDto)
        {
            DAL.Entities.Dialog dialog = await Database.DialogManager.GetById(dialogDto.Id);

            dialog.Name = dialogDto.Name;
            await Database.DialogManager.Update(dialog);

            await Database.SaveAsync();
        }
Exemple #8
0
        public ActionResult CurrentDialog(int id)
        {
            DialogDTO d = messService.GetDialog(id, Helper.GetUser(User.Identity.Name).Id).Data;

            ViewBag.DialogId           = id;
            ViewBag.DialogName         = d.Name;
            ViewBag.DialogProfileImage = d.ProfileImage.FilePath;
            ViewBag.PartCount          = messService.GetMessPartsCount(id, 80).Data;
            return(PartialView("_Dialog"));
        }
        public async Task AddUserToDialog(DialogDTO dialog, UserDTO account)
        {
            DAL.Entities.Dialog dialogEnt = await Database.DialogManager.GetById(dialog.Id);

            DAL.Entities.ClientProfile userEnt = await Database.ClientManager.GetById(account.Id);

            dialogEnt.ChatUsers.Add(userEnt);
            await Database.DialogManager.Update(dialogEnt);

            await Database.SaveAsync();
        }
Exemple #10
0
        public int CreateDialog(DialogDTO dialogDto)
        {
            Dialog dialog = new Dialog
            {
                RecipientId = dialogDto.RecipientId,
                SenderId    = dialogDto.SenderId,
            };


            return(DB.Messages.Create(dialog));
        }
        public async Task <ActionResult> Leave(int id)
        {
            DialogDTO Dialog = await DialogService.GetDialogById(id);

            if (!(await DialogService.GetUsersInDialog(Dialog)).Any(x => x.Id == User.Identity.GetUserId()))
            {
                return(RedirectToAction("AccessDenied", "Home"));
            }

            return(PartialView(Dialog));
        }
Exemple #12
0
        public void ReadMessages(int dialogId)
        {
            UserDTO          owner;
            UserDTO          reader = Helper.GetUser(Context.User.Identity.Name);
            DialogDTO        dDTO   = messService.GetDialog(dialogId, reader.Id).Data;
            DialogMessageDTO lastM;

            if (dDTO.LastMessInDialog != null)
            {
                lastM = dDTO.LastMessInDialog;
                owner = lastM.FromUser;

                ConnectedUser cuR = users.FirstOrDefault(us => us.userId == reader.Id);
                ConnectedUser cuO = users.FirstOrDefault(us => us.userId == owner.Id);
                if (cuR != null && cuO != null)
                {
                    if (messService.DialogNewMessCount(dialogId, reader.Id).Data != 0)
                    {
                        messService.ReadMessages(dialogId, reader.Id);
                        Clients.Client(cuR.ConnectionId).showReadMessages(cuR.ConnectionId, cuO.ConnectionId);
                        Clients.Client(cuO.ConnectionId).showReadMessages(cuR.ConnectionId, cuO.ConnectionId);
                    }
                }
            }


            //foreach (UserDTO uDTO in messService.GetUsersInDialog(dDTO.Id).Data.Where(u => u.Id == reader.Id || u.Id == owner.Id))
            //{
            //    ConnectedUser cu = users.FirstOrDefault(us => us.userId == uDTO.Id);
            //    if (cu != null)
            //    {
            //        if(messService.DialogNewMessCount(dialogId,reader.Id).Data != 0)
            //        {
            //            messService.ReadMessages(dialogId, uDTO.Id);
            //            Clients.Client(cu.ConnectionId).showReadMessages();
            //        }
            //    }
            //}



            //int id = Helper.GetUser(Context.User.Identity.Name).Data.Id;
            //if (messService.DialogNewMessCount(dialogId, id).Data!=0)
            //{
            //    messService.ReadMessages(dialogId, id);
            //    Clients.All.showReadMessages();
            //}
        }
        public async Task <List <UserDTO> > GetUsersInDialog(DialogDTO dialog)
        {
            DAL.Entities.Dialog dialogEnt = await Database.DialogManager.GetById(dialog.Id);

            List <UserDTO> result = new List <UserDTO>();

            dialogEnt.ChatUsers.ForEach(m => result.Add(new UserDTO
            {
                Id       = m.Id,
                Email    = m.ApplicationUser.Email,
                Address  = m.Address,
                Name     = m.Name,
                UserName = m.ApplicationUser.UserName
            }));
            return(result);
        }
Exemple #14
0
        public ActionResult PutInquiry(int?id)
        {
            try
            {
                DialogDTO dialog  = inquiryService.GetDialog(id);
                var       request = new InquiryVM
                {
                    Id = dialog.Id
                };

                return(View(request));
            }
            catch (ValidationException ex)
            {
                return(Content(ex.Message));
            }
        }
        public async Task RemoveUserFromDialog(DialogDTO dialog, UserDTO account)
        {
            DAL.Entities.Dialog dialogEnt = await Database.DialogManager.GetById(dialog.Id);

            DAL.Entities.ClientProfile userEnt = await Database.ClientManager.GetById(account.Id);

            dialogEnt.ChatUsers.Remove(userEnt);

            if (dialogEnt.ChatUsers.Count == 0)
            {
                await Database.DialogManager.Delete(dialogEnt);

                return;
            }
            await Database.DialogManager.Update(dialogEnt);

            await Database.SaveAsync();
        }
        private async Task <int> CheckIfPrivateDialogExists(DialogDTO privateChat)
        {
            DialogListDTO tmpChatList = await ListDialogs(new DialogFilter { Account = privateChat.Users[0] });

            foreach (DialogDTO chatTmp in tmpChatList.ResultDialogs)
            {
                if (chatTmp.Users.Count != 2)
                {
                    continue;
                }
                if (chatTmp.Users.Any(x => x.Id == privateChat.Users[1].Id))
                {
                    return(chatTmp.Id);
                }
            }

            return(-1);
        }
Exemple #17
0
        public async Task <int> PostMessageToDialog(DialogDTO dialog, UserDTO user, MessageDTO message)
        {
            DAL.Entities.Dialog dialogEnt = await Database.DialogManager.GetById(dialog.Id);

            DAL.Entities.ClientProfile userEnt = await Database.ClientManager.GetById(user.Id);

            DAL.Entities.Message newMessage = new DAL.Entities.Message
            {
                Content = message.Content
            };
            newMessage.Time   = DateTime.Now;
            newMessage.Sender = userEnt;
            newMessage.Dialog = dialogEnt;
            await Database.MessageManager.Create(newMessage);

            await Database.SaveAsync();

            return(newMessage.Id);
        }
        public async Task <DialogDTO> GetDialogById(int Id)
        {
            DAL.Entities.Dialog dialog = await Database.DialogManager.GetById(Id);

            DialogDTO result = new DialogDTO
            {
                Id   = dialog.Id,
                Name = dialog.Name
            };

            dialog.ChatUsers.ForEach(m => result.Users.Add(new UserDTO
            {
                Id       = m.Id,
                Email    = m.ApplicationUser.Email,
                Address  = m.Address,
                Name     = m.Name,
                UserName = m.ApplicationUser.UserName
            }));

            return(result);
        }
Exemple #19
0
        public ServiceResult <DialogDTO> GetDialog(int dialogId, int userId)
        {
            UserDTO   uDTO;
            DialogDTO dialogDTO = Mapper.Map <Dialog, DialogDTO>(db.Dialogs.Get(dialogId));

            if (GetLastMessInDialog(dialogDTO.Id).Data != null)
            {
                dialogDTO.LastMessInDialog = GetLastMessInDialog(dialogDTO.Id).Data;
            }

            if (dialogDTO.Name == "")
            {
                uDTO = Mapper.Map <User, UserDTO>(db.Users.Get(GetCompanion(dialogId, userId).Data.Id));
                dialogDTO.ProfileImage = uDTO.ProfileImage;
                dialogDTO.Name         = uDTO.FirstName + " " + uDTO.LastName;
                return(new ServiceResult <DialogDTO>(dialogDTO, null));
            }
            else
            {
                return(new ServiceResult <DialogDTO>(dialogDTO, null));
            }
        }
        public async Task <int> CreateDialog(DialogDTO dialogDto)
        {
            int id;

            if (dialogDto.Users.Count == 2)
            {
                int tmp = await CheckIfPrivateDialogExists(dialogDto);

                if (tmp != -1)
                {
                    return(tmp);
                }
            }
            List <DAL.Entities.ClientProfile> list = dialogDto.Users
                                                     .Select(User => Database.ClientManager.GetById(User.Id).Result)
                                                     .ToList();

            StringBuilder dialogName = new StringBuilder();

            if (string.IsNullOrEmpty(dialogDto.Name))
            {
                list.ForEach(u => dialogName.Append(u.Name + ", "));
                dialogDto.Name = dialogName.ToString();
            }
            DAL.Entities.Dialog dialogEnt = new DAL.Entities.Dialog
            {
                Name = dialogDto.Name
            };
            dialogEnt.ChatUsers.AddRange(list);

            await Database.DialogManager.Create(dialogEnt);

            await Database.SaveAsync();

            id = dialogEnt.Id;
            return(id);
        }
Exemple #21
0
        //===================Messages===================//
        public void SendMessage(int dialogId, string Text)
        {
            bool                  isToday  = true;
            UserDTO               fromUser = Helper.GetUser(Context.User.Identity.Name);
            DialogDTO             dDTO     = messService.GetDialog(dialogId, fromUser.Id).Data;
            DialogMessageDTO      prevdm   = dDTO.LastMessInDialog;
            IEnumerable <UserDTO> uid      = messService.GetUsersInDialog(dDTO.Id).Data;
            DialogMessageDTO      dm       = messService.SendMessage(new DialogMessageDTO()
            {
                DialogId = dialogId, FromUserId = fromUser.Id, Text = Text, Date = DateTime.Now, Status = false
            }).Data;

            if (prevdm != null)
            {
                if (prevdm.Date.Year == DateTime.Now.Year && prevdm.Date.Month == DateTime.Now.Month && prevdm.Date.Day == prevdm.Date.Day)
                {
                    isToday = false;
                }
            }
            foreach (UserDTO uDTO in uid)
            {
                ConnectedUser cu = users.FirstOrDefault(us => us.userId == uDTO.Id);
                if (cu != null)
                {
                    if (uDTO.Id == fromUser.Id)
                    {
                        if (uid.Count() == 2)
                        {
                            Clients.Client(cu.ConnectionId).showMessageForCaller(
                                new
                            {
                                messId        = dm.Id,
                                userId        = fromUser.Id,
                                UserProfImage = fromUser.ProfileImage.FilePath,
                                FromUserName  = fromUser.FirstName + " " + fromUser.LastName,
                                dialogId      = dDTO.Id,
                                DialProfImage = dDTO.ProfileImage.FilePath,
                                DialogName    = fromUser.FirstName + " " + fromUser.LastName,
                                Date          = dm.Date.ToString("HH:mm"),
                                DateSec       = TimeSpan.FromTicks(dm.Date.Ticks).TotalSeconds,
                                Text          = Text,
                                isToday       = isToday
                            });
                        }
                        else
                        {
                            Clients.Client(cu.ConnectionId).showMessageForCaller(
                                new
                            {
                                messId        = dm.Id,
                                userId        = fromUser.Id,
                                UserProfImage = fromUser.ProfileImage.FilePath,
                                FromUserName  = fromUser.FirstName + " " + fromUser.LastName,
                                dialogId      = dDTO.Id,
                                DialProfImage = dDTO.ProfileImage.FilePath,
                                DialogName    = dDTO.Name,
                                Date          = dm.Date.ToString("HH:mm"),
                                DateSec       = TimeSpan.FromTicks(dm.Date.Ticks).TotalSeconds,
                                Text          = Text,
                                isToday       = isToday
                            });
                        }
                    }
                    else
                    {
                        if (uid.Count() == 2)
                        {
                            Clients.Client(cu.ConnectionId).showMessage(
                                new
                            {
                                messId        = dm.Id,
                                userId        = fromUser.Id,
                                UserProfImage = fromUser.ProfileImage.FilePath,
                                FromUserName  = fromUser.FirstName + " " + fromUser.LastName,
                                dialogId      = dDTO.Id,
                                DialProfImage = dDTO.ProfileImage.FilePath,
                                DialogName    = fromUser.FirstName + " " + fromUser.LastName,
                                Date          = dm.Date.ToString("HH:mm"),
                                DateSec       = TimeSpan.FromTicks(dm.Date.Ticks).TotalSeconds,
                                Text          = Text,
                                isToday       = isToday
                            });
                        }
                        else
                        {
                            Clients.Client(cu.ConnectionId).showMessage(
                                new
                            {
                                messId        = dm.Id,
                                userId        = fromUser.Id,
                                UserProfImage = fromUser.ProfileImage.FilePath,
                                FromUserName  = fromUser.FirstName + " " + fromUser.LastName,
                                dialogId      = dDTO.Id,
                                DialProfImage = dDTO.ProfileImage.FilePath,
                                DialogName    = dDTO.Name,
                                Date          = dm.Date.ToString("HH:mm"),
                                DateSec       = TimeSpan.FromTicks(dm.Date.Ticks).TotalSeconds,
                                Text          = Text,
                                isToday       = isToday
                            });
                        }
                    }
                }
            }
        }
        public async Task <ActionResult> Edit(DialogDTO dialog)
        {
            await DialogService.EditDialogName(dialog);

            return(RedirectToAction("OpenDialog", new { id = dialog.Id }));
        }
        public async Task <ActionResult> Delete(DialogDTO Dialog)
        {
            await DialogService.DeleteDialog(Dialog.Id);

            return(RedirectToAction("Index"));
        }