コード例 #1
0
ファイル: MainViewModel.cs プロジェクト: Dominiworld/CryptIt
        private async void GetDialogsInfo()
        {
            try
            {
                var unreadDialogs = await _messageService.GetDialogs(true);

                foreach (var dialog in unreadDialogs)
                {
                    if (dialog.Message.ChatId != 0)
                    {
                        continue;
                    }

                    var friend = FoundFriends.FirstOrDefault(f => f.Id == dialog.Message.UserId);
                    if (friend != null)
                    {
                        friend.NumberOfNewMessages = dialog.UnreadMessagesAmount;
                        //UnreadDialogs.Add(friend);
                    }
                }
                var newDialogFriends  = FoundFriends.Where(f => f.NumberOfNewMessages != null).ToList();
                var readDialofFriends = FoundFriends.Where(f => f.NumberOfNewMessages == null).ToList();
                newDialogFriends.AddRange(readDialofFriends);
                FoundFriends = newDialogFriends;
                OnPropertyChanged("FoundFriends");
            }
            catch (WebException)
            {
            }
        }
コード例 #2
0
ファイル: MainViewModel.cs プロジェクト: Dominiworld/CryptIt
        private async void AddMessages(Message message)
        {
            if (message.Out && message.Body.StartsWith(_cryptTool._isCryptedFlag)) //не выводим свое отправленное зашифрованное сообщение - незачем
            {
                return;
            }

            if ((SelectedUser == null || message.UserId != SelectedUser.Id) && !message.Out)
            {
                var friend = FoundFriends.FirstOrDefault(f => f.Id == message.UserId);
                if (friend != null)
                {
                    if (friend.NumberOfNewMessages == null)
                    {
                        friend.NumberOfNewMessages = 1;
                    }
                    else
                    {
                        friend.NumberOfNewMessages++;
                    }
                    //UnreadDialogs.Add(friend);
                    FoundFriends.Remove(friend);
                    FoundFriends.Insert(0, friend);
                    FoundFriends = new List <User>(FoundFriends);
                }
                OnPropertyChanged("FoundFriends");
            }

            if (SelectedUser == null)
            {
                return;
            }


            var decryptedMessage = _cryptTool.SplitAndUnpackReceivedMessage(message.Body);

            message.Body = decryptedMessage;
            TakeFileNamesFromBody(message);
            if (message.Attachments != null && message.Attachments.Where(a => a.File == null).ToList().Count != 0)
            {
                message.HasUndefinedContent = true;
                message.Attachments         = new ObservableCollection <Attachment>(message.Attachments.Where(a => a.File != null));
            }


            if (message.UserId != SelectedUser.Id)
            {
                return;
            }
            message.User = message.Out ? AuthorizeService.Instance.CurrentUser : SelectedUser;

            if (!message.Out)
            {
                try
                {
                    message.IsNotRead = false;
                    _messageService.MarkMessagesAsRead(new List <int> {
                        message.Id
                    }, SelectedUser.Id);
                    await FindKeyRequestAndReply(message);
                    await GetKeyFileFromMessage(message);
                }
                catch (Exception)
                {
                    // ignored
                }
            }
            //todo переделать биндинг
            var messages = new List <Message>();

            messages.AddRange(Messages);
            messages.Add(message);
            Messages    = messages;
            ScrollToEnd = true;
        }
コード例 #3
0
ファイル: MainViewModel.cs プロジェクト: Dominiworld/CryptIt
        private async Task GetMessages(User user)
        {
            var previousSelected = SelectedUser;

            try
            {
                SelectedUser = user;
                var query = (await _messageService.GetDialog(SelectedUser.Id)).ToList();

                PraseMessages(query);

                var messages = query.OrderBy(m => m.Date).ToList();

                foreach (var message in messages.ToArray())
                {
                    message.Body = _cryptTool.SplitAndUnpackReceivedMessage(message.Body);
                    TakeFileNamesFromBody(message);
                    if (message.Attachments != null &&
                        message.Attachments.Where(a => a.File == null).ToList().Count != 0)
                    {
                        message.HasUndefinedContent = true;
                        message.Attachments         =
                            new ObservableCollection <Attachment>(message.Attachments.Where(a => a.File != null));
                    }
                }

                if (messages.Count > 0 && messages[0].UserId != SelectedUser.Id)
                {
                    var inMessage = messages.FirstOrDefault(m => !m.Out);
                    SelectedUser = inMessage != null ? inMessage.User : await _userService.GetUser(SelectedUser.Id);
                }

                Messages = messages;
            }
            catch (NullReferenceException ex)
            {
                SelectedUser = previousSelected;
                return;
            }
            catch (WebException ex)
            {
                Messages = null;
                ShowWebErrorMessage();
                return;
            }
            finally
            {
                ScrollToEnd = true;
            }
            SelectedUser.NumberOfNewMessages = null;

            var sortedUnreadList =
                FoundFriends.Where(f => f.NumberOfNewMessages != null).ToList();

            sortedUnreadList.AddRange(FoundFriends.Where(f => f.NumberOfNewMessages == null).OrderBy(f => f.FullName).ToList());
            FoundFriends = new List <User>(sortedUnreadList);

            if (Messages.Count != 0 && !Messages[0].Out)
            {
                _messageService.MarkMessagesAsRead(new List <int> {
                    Messages[0].Id
                }, SelectedUser.Id);
            }
        }