예제 #1
0
        public List <MessageDto> TopLatestMessagesOfDialog(DialogDto dialog, int top, bool withAuthor = false)
        {
            UserDto userOne = dialog?.UserOne ?? new UserDto()
            {
                Id = dialog.UserOneId
            };
            UserDto userTwo = dialog?.UserTwo ?? new UserDto()
            {
                Id = dialog.UserTwoId
            };

            if (top <= 0)
            {
                throw new ArgumentOutOfRangeException("top");
            }
            ValidateUser(userOne);
            ValidateUser(userTwo);

            return(ExecuteSelectQuery(uow => {
                var res = uow.MessageRepository
                          .GetTopLatestMessagesOfDialogBetween(userTwo.MapToDbEntity(), userOne.MapToDbEntity(), top);

                if (withAuthor)
                {
                    foreach (var m in res)
                    {
                        m.From = uow.UserRepository.GetById(m.FromId);
                        m.To = uow.UserRepository.GetById(m.ToId);
                    }
                }
                return res;
            }));
        }
예제 #2
0
        public JsonResult NewConversation(List <string> listUsersId, string nameConversation)
        {
            string currentUserId = User.Identity.GetUserId();
            var    users         = db.Users.Join(listUsersId, u => u.Id, i => i, (u, i) => u).Where(u => u.Id != currentUserId).ToList();
            Dialog dialog        = new Dialog()
            {
                IsConversation = true, Name = nameConversation
            };

            db.Dialogs.Add(dialog);
            db.SaveChanges();
            var allDialogs = db.Dialogs.ToList();
            int dialogId   = allDialogs.Last().Id;

            listUsersId.Add(currentUserId);
            var usersToDialog = listUsersId.Select(i => new UserToDialog()
            {
                DialogId = dialogId, UserId = i
            }).ToList();

            db.UserToDialogs.AddRange(usersToDialog);
            db.SaveChanges();

            DialogDto dialogDto = new DialogDto();

            dialogDto.Id             = dialogId;
            dialogDto.IsConversation = true;
            dialogDto.Name           = nameConversation;
            dialogDto.Members        = users;

            return(Json(dialogDto, JsonRequestBehavior.AllowGet));
        }
예제 #3
0
        public List <MessageDto> TopLatestMessagesOfDialog(DialogDto dialog, int skip, int top)
        {
            UserDto userOne = dialog?.UserOne ?? new UserDto()
            {
                Id = dialog.UserOneId
            };
            UserDto userTwo = dialog?.UserTwo ?? new UserDto()
            {
                Id = dialog.UserTwoId
            };

            if (top <= 0)
            {
                throw new ArgumentOutOfRangeException("top");
            }
            if (skip <= 0)
            {
                throw new ArgumentOutOfRangeException("skip");
            }
            ValidateUser(userOne);
            ValidateUser(userTwo);

            return(ExecuteSelectQuery(uow => uow.MessageRepository
                                      .GetTopLatestMessagesOfDialogBetween(userTwo.MapToDbEntity(), userOne.MapToDbEntity(), skip, top)));
        }
예제 #4
0
        public DialogDto GetDialogDto(string login, int firstUserId, int secondUserId)
        {
            DialogDto dialogDto = new DialogDto()
            {
                User   = GetTalker(login),
                WallId = GetDialogWallId(firstUserId, secondUserId)
            };

            return(dialogDto);
        }
예제 #5
0
        public IActionResult GetMessagesRequest([FromBody] DialogDto DialogDto)
        {
            List <Message> messages = repository.context.Messages
                                      .Where(message => message.DialogId == DialogDto.Id)
                                      .Include(message => message.User).Include(message => message.Dialog).OrderBy(message => message.InsertDate).ToList();
            List <MessageDto> messagesDto = new List <MessageDto>();

            foreach (Message msg in messages)
            {
                messagesDto.Add(new MessageDto(msg));
            }
            return(Ok(messagesDto));
        }
예제 #6
0
        private static Dialog FromDto(
            DialogDto dialog,
            IReadOnlyCollection <Profile> profiles,
            IReadOnlyCollection <Group> groups,
            IReadOnlyCollection <Message>?lastMessages)
        {
            var conversation = dialog.conversation;
            var peerId       = Math.Abs(conversation.peer.id); // id in peerDto can be negative
            var dialogType   = Enum.Parse <DialogType>(conversation.peer.type, true);
            var unreadCount  = conversation.unread_count ?? 0;
            var canWrite     = conversation.can_write?.allowed != false;

            var lastMessage = dialog.last_message != null
                ? MessagesClient.FromDto(dialog.last_message, profiles, groups)
                : lastMessages?.SingleOrDefault(e => e.Id == conversation.last_message_id);

            var messages = lastMessage != null
                ? new[] { lastMessage }
                : null;

            Dialog result = default !;
예제 #7
0
        // GET: Communion
        public ActionResult Index()
        {
            var currentUserId = User.Identity.GetUserId();
            var listDialogs   = db.Dialogs.Join(db.UserToDialogs.Where(d => d.UserId == currentUserId), d => d.Id, i => i.DialogId, (d, i) => d).ToList();
            var usersDialogs  = db.UserToDialogs.Join(db.UserToDialogs.Where(d => d.UserId == currentUserId), u1 => u1.DialogId, u2 => u2.DialogId, (u1, u2) => u1)
                                .GroupBy(u => u.DialogId, (key, elements) => new { Key = key, Count = elements.Distinct().Count() })
                                .Where(d => d.Count == 2).Join(db.Dialogs, i => i.Key, d => d.Id, (i, d) => d).Where(d => d.IsConversation == false)
                                .Join(db.UserToDialogs, d => d.Id, ud => ud.DialogId, (d, ud) => ud)
                                .Join(db.Users, ud => ud.UserId, u => u.Id, (ud, u) => new { User = u, DialogId = ud.DialogId }).ToList();

            List <DialogDto> dialogs = new List <DialogDto>();

            var listRelationsUsers = db.Friends.Where(t => t.UserOneId == currentUserId || t.UserTwoId == currentUserId).Select(t => t);
            var listFriends        = db.Users.Join(listRelationsUsers, u => u.Id, f => f.UserOneId == currentUserId ? f.UserTwoId : f.UserOneId, (u, f) => u).
                                     OrderBy(u => u.SurName).ToList();

            ViewData["Friends"] = listFriends;

            foreach (var dialog in listDialogs)
            {
                DialogDto dialogDto = new DialogDto();

                dialogDto.Id             = dialog.Id;
                dialogDto.IsConversation = dialog.IsConversation;

                if (!dialog.IsConversation)
                {
                    List <ApplicationUser> membersToDialog = usersDialogs.Where(u => u.DialogId == dialog.Id).Select(u => u.User).ToList();
                    dialogDto.Name = GetDialogName(membersToDialog);
                }
                else
                {
                    dialogDto.Name = dialog.Name;
                }

                dialogs.Add(dialogDto);
            }
            return(View(dialogs));
        }
예제 #8
0
        public async Task <IActionResult> CreateDialogRequest([FromBody] CreateDialogDto createDialogDto)
        {
            if (createDialogDto.Dialog == null ||
                createDialogDto.Dialog.Name == null ||
                createDialogDto.Users == null ||
                createDialogDto.Users.Count == 0)
            {
                return(BadRequest("Не верная команда."));
            }
            Dialog dialog = new Dialog();

            dialog.Id   = Guid.NewGuid();
            dialog.Name = createDialogDto.Dialog.Name;
            List <DialogUserLink> links = new List <DialogUserLink>();

            foreach (UserDto userDto in createDialogDto.Users)
            {
                User sameUser = this.repository.GetFirst <User>(x => x.Name == userDto.Name);
                if (sameUser == null)
                {
                    return(BadRequest($"Пользователь {userDto.Name} не найден."));
                }
                DialogUserLink link = new DialogUserLink();
                link.Id       = Guid.NewGuid();
                link.DialogId = dialog.Id;
                link.UserId   = sameUser.Id;
                links.Add(link);
            }

            await repository.Add(dialog);

            await repository.AddRange(links);

            await repository.SaveChangesAsync();

            DialogDto dialogDto = new DialogDto(dialog);

            return(Ok(dialogDto));
        }
예제 #9
0
        private DialogDto GetDialogDto(Dialog dialog)
        {
            string    currentUserId = User.Identity.GetUserId();
            DialogDto dialogDto     = new DialogDto();

            dialogDto.IsConversation = dialog.IsConversation;
            dialogDto.Id             = dialog.Id;
            var dialogUsers = db.UserToDialogs.Where(ud => ud.DialogId == dialog.Id);

            dialogDto.Members = db.Users.Join(dialogUsers, u => u.Id, d => d.UserId, (u, d) => u).ToList();

            dialogDto.Name = dialog.IsConversation ? dialog.Name : GetDialogName(dialogDto.Members);

            var messages = db.Messages.Where(m => m.DialogId == dialog.Id)
                           .Join(db.Users, m => m.SenderId, u => u.Id, (m, u) =>
                                 new
            {
                Id        = m.Id,
                DateSend  = m.DateSend,
                Text      = m.Text,
                FirstName = u.FirstName,
                SurName   = u.SurName
            }).ToList();

            foreach (var message in messages)
            {
                dialogDto.Messages.Add(new MessageDto()
                {
                    Id        = message.Id,
                    DateSend  = GetStringDate(message.DateSend),
                    Text      = message.Text,
                    FirstName = message.FirstName,
                    SurName   = message.SurName
                });
            }

            return(dialogDto);
        }
예제 #10
0
        public virtual ActionResult FileReferenceConfirmed(DialogDto dialogDto, HttpPostedFileBase file)
        {
            // TODO change the implementation
            string fileName = null;
            Stream stream   = null;

            if (file != null)
            {
                fileName = file.FileName;
                stream   = file.InputStream;
            }
            try
            {
                GetUnitOfWork().StartTransaction();
                PhotoResourceDto photoResourceDto = null;// (PhotoResourceDto)GetService().UploadFile(dialogDto.Id, typeof(T), fileName, stream);
                GetUnitOfWork().EndTransaction();
                return(Json(new JsonFileSelectDialogResult(true, photoResourceDto.GetRelativeThumbnailFile(), photoResourceDto.GetRelativeFilePath(), JsonRefreshMode.IMAGE_TO_RICHTEXTBOX)));
            }
            catch (ValidationException ex)
            {
                return(Json(new JsonDialogResult(false, HtmlConstants.DIALOG_VALIDATION_SUMMARY, ValidationSummaryExtensions.CustomValidationSummary(ex.Message).ToString())));
            }
        }
예제 #11
0
 public override ActionResult DeleteConfirmed(DialogDto dialogDto)
 {
     return(base.DoDeleteConfirmed(dialogDto.Id, Message.CreateSuccessMessage(MessageKeyConstants.SEMINAR_INFORMATION_DELETE_MESSAGE), WebConstants.VIEW_INDEX, WebConstants.CONTROLLER_SEMINAR));
 }
예제 #12
0
        public ActionResult OpenDialog(string login, int firstUserId, int secondUserId)
        {
            DialogDto dialog = dialogService.GetDialogDto(login, firstUserId, secondUserId);

            return(View("Dialog", dialog));
        }
예제 #13
0
 /// <summary>
 /// Delete the entity after the confirmation.
 /// </summary>
 /// <param name="dialogDto">The DTO for dialog</param>
 /// <returns>Returns the appropriate view</returns>
 public abstract ActionResult DeleteConfirmed(DialogDto dialogDto);
        public async void SendNewMessageNoticeToDialogUsers(IEnumerable <MessageDto> messages, ClientConnection senderClientConnection, long receiverId, bool saveMessageFlag = true)
        {
            try
            {
                long senderId = messages.FirstOrDefault().SenderId.GetValueOrDefault();
                var  dialogs  = await loadDialogsService.GetUsersDialogsAsync(senderId, receiverId);

                var    currentDialog = dialogs.FirstOrDefault(dial => dial.FirstUserId == receiverId);
                Notice senderNotice  = default;
                if (senderId != receiverId)
                {
                    var message = messages.FirstOrDefault(mess => mess.ConversationId != currentDialog.Id);
                    senderNotice = new NewMessageNotice(
                        MessageConverter.GetMessageVm(message, senderId),
                        MarkdownHelper.ContainsMarkdownUserCalling(message.Text, receiverId));
                }
                else
                {
                    var message = messages.FirstOrDefault();
                    senderNotice = new NewMessageNotice(
                        MessageConverter.GetMessageVm(message, senderId),
                        MarkdownHelper.ContainsMarkdownUserCalling(message.Text, receiverId));
                }
                var senderClients = connectionsService.GetUserClientConnections(messages.ElementAt(0).SenderId.GetValueOrDefault());
                if (senderClients != null)
                {
                    IEnumerable <ClientConnection> senderConnectionsExceptCurrent = senderClients.Where(connection => !Equals(senderClientConnection, connection));
                    await SendNoticeToClientsAsync(senderConnectionsExceptCurrent, senderNotice).ConfigureAwait(false);
                }
                if (messages.Count() == 2)
                {
                    var    message         = messages.FirstOrDefault(mess => mess.ConversationId == currentDialog.Id);
                    var    receiverClients = connectionsService.GetUserClientConnections(receiverId);
                    Notice receiverNotice  = new NewMessageNotice(
                        MessageConverter.GetMessageVm(message, receiverId),
                        MarkdownHelper.ContainsMarkdownUserCalling(message.Text, receiverId));
                    if (receiverClients != null && receiverClients.Any())
                    {
                        await SendNoticeToClientsAsync(receiverClients, receiverNotice).ConfigureAwait(false);
                    }
                    else
                    {
                        var receiver = await loadUsersService.GetUserAsync(receiverId).ConfigureAwait(false);

                        if (receiver.NodeId == NodeSettings.Configs.Node.Id && !saveMessageFlag)
                        {
                            await pendingMessagesService.AddUserPendingMessageAsync(receiverId, receiverNotice, message.GlobalId).ConfigureAwait(false);
                        }
                    }
                }
                DialogDto dialog = await loadDialogsService.GetDialogAsync(currentDialog.Id).ConfigureAwait(false);

                pushNotificationsService.SendMessageNotificationAsync(
                    messages.FirstOrDefault(opt => opt.ConversationId == currentDialog.Id),
                    new List <NotificationUser> {
                    new NotificationUser(dialog.FirstUserId, dialog.IsMuted)
                });
            }
            catch (Exception ex)
            {
                Logger.WriteLog(ex);
            }
        }
예제 #15
0
 public override ActionResult DeleteConfirmed(DialogDto dialogDto)
 {
     return(DoDeleteConfirmed(dialogDto.Id, null, WebConstants.VIEW_LIST, WebConstants.CONTROLLER_MENU_ITEM, null, HtmlConstants.TREE_MENU_ITEM));
 }
예제 #16
0
 public async Task EnterGroup(DialogDto dialogDto)
 {
     await Groups.AddToGroupAsync(Context.ConnectionId, dialogDto.Id.ToString());
 }
예제 #17
0
 public override ActionResult DeleteConfirmed(DialogDto dialogDto)
 {
     return(DoDeleteConfirmed(dialogDto.Id, null, WebConstants.VIEW_PAGED_LIST, WebConstants.CONTROLLER_BLOG_CATEGORY, null, HtmlConstants.PAGED_LIST_BLOG_CATEGORY));
 }
예제 #18
0
 public void AddMembers(string groipId, DialogDto dialog)
 {
     Clients.Group(groipId).addMembers(dialog);
 }