public async Task DownloadByIdTest()
        {
            var contentHelper = new ContentClientHelper(client);

            var uri         = new Uri("ms-appx:///Modules/ContentModule/Assets/1.jpg");
            var storageFile = await StorageFile.GetFileFromApplicationUriAsync(uri);

            var stream = await storageFile.OpenReadAsync();

            var bytes = new byte[stream.Size];

            using (var dataReader = new DataReader(stream))
            {
                await dataReader.LoadAsync((uint)stream.Size);

                dataReader.ReadBytes(bytes);
            }

            var uploadId = await contentHelper.UploadPrivateImage(bytes);

            Assert.IsNotNull(uploadId);

            var downloadedBytes = await client.ContentClient.DownloadFileById(uploadId.Value);

            Assert.AreEqual(bytes.Length, downloadedBytes.Result.Length);
        }
        public async Task UploadFileViaHelperTest()
        {
            var contentHelper = new ContentClientHelper(client);

            var uri         = new Uri("ms-appx:///Modules/ContentModule/Assets/1.jpg");
            var storageFile = await StorageFile.GetFileFromApplicationUriAsync(uri);

            var stream = await storageFile.OpenReadAsync();

            var bytes = new byte[stream.Size];

            using (var dataReader = new DataReader(stream))
            {
                await dataReader.LoadAsync((uint)stream.Size);

                dataReader.ReadBytes(bytes);
            }

            var imageLink = await contentHelper.UploadPublicImage(bytes);

            Assert.IsNotNull(imageLink);
        }
        public async void ContinueFileOpenPicker(IReadOnlyList<StorageFile> files)
        {
            if (files == null || !files.Any()) return;
            IsLoading = true;
            using (var stream = (FileRandomAccessStream)await files[0].OpenAsync(FileAccessMode.Read))
            {
                var newImageBytes = new byte[stream.Size];
                using (var dataReader = new DataReader(stream))
                {
                    await dataReader.LoadAsync((uint)stream.Size);
                    dataReader.ReadBytes(newImageBytes);
                }

                var contentHelper = new ContentClientHelper(QuickbloxClient);
                var blobUploadInfo = await contentHelper.UploadImage(newImageBytes, false);
                if (blobUploadInfo != null)
                {
                    SendAttachment(blobUploadInfo);
                }
            }
            IsLoading = false;
        }
        private async void SaveCommandExecute()
        {
            IsLoading = true;

            var updateUserRequest = new UpdateUserRequest {User = new UserRequest() {FullName = UserName}};

            if (newImageBytes != null)
            {
                var contentHelper = new ContentClientHelper(QuickbloxClient);
                var imageUploadResult = await contentHelper.UploadPublicImage(newImageBytes);
                if (imageUploadResult != null)
                {
                    var customData = new CustomData { AvatarUrl = imageUploadResult.Url, IsImport = "1" };
                    var customDataJson = JsonConvert.SerializeObject(customData);
                    updateUserRequest.User.BlobId = imageUploadResult.BlodId;
                    updateUserRequest.User.CustomData = customDataJson;
                }
            }

            var updateUserResponse = await QuickbloxClient.UsersClient.UpdateUserAsync(currentUser.Id, updateUserRequest);

            if (updateUserResponse.StatusCode == HttpStatusCode.OK)
            {
                var cachingQbClient = ServiceLocator.Locator.Get<ICachingQuickbloxClient>();
                cachingQbClient.DeleteUserFromCacheById(currentUser.Id);

                NavigationService.GoBack();
            }
            IsLoading = false;
        }
        private async Task UploadUserImage(User user, byte[] imageBytes)
        {
            var contentHelper = new ContentClientHelper(QuickbloxClient.ContentClient);
            var uploadId = await contentHelper.UploadPrivateImage(imageBytes);
            if (uploadId == null)
            {
                await QmunicateLoggerHolder.Log(QmunicateLogLevel.Warn, "SignUpViewModel. Failed to upload user image");
                return;
            }

            UpdateUserRequest updateUserRequest = new UpdateUserRequest { User = new UserRequest { BlobId = uploadId } };
            await QuickbloxClient.UsersClient.UpdateUserAsync(user.Id, updateUserRequest);
        }
        private async Task<MessageViewModel> CreateMessageViewModelFromMessage(Message message)
        {
            var messageViewModel = new MessageViewModel
            {
                MessageText = WebUtility.HtmlDecode(message.MessageText),
                DateTime = message.DateSent.ToDateTime(),
                NotificationType = message.NotificationType,
                SenderId = message.SenderId
            };

            int currentUserId = SettingsManager.Instance.ReadFromSettings<int>(SettingsKeys.CurrentUserId);
            messageViewModel.MessageType = messageViewModel.SenderId == currentUserId ? MessageType.Outgoing : MessageType.Incoming;

            var cachingQbClient = ServiceLocator.Locator.Get<ICachingQuickbloxClient>();
            var senderUser = await cachingQbClient.GetUserById(message.SenderId);
            if (senderUser != null)
            {
                messageViewModel.SenderName = senderUser.FullName;

                if (senderUser.CustomData != null)
                {
                    var customData = JsonConvert.DeserializeObject<CustomData>(senderUser.CustomData);
                    messageViewModel.SenderImage = await ServiceLocator.Locator.Get<IImageService>().GetPublicImage(customData?.AvatarUrl);
                }
            }

            bool isImageAttached = message.Attachments != null && message.Attachments.Any() && !string.IsNullOrEmpty(message.Attachments[0].Id);

            if (isImageAttached)
            {
                var contentHelper = new ContentClientHelper(ServiceLocator.Locator.Get<IQuickbloxClient>());
                var attachedImageUrl = contentHelper.GenerateImageUrl(message.Attachments[0].Id, false);
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    messageViewModel.AttachedImage = new BitmapImage(new Uri(attachedImageUrl));
                });
            }

            return messageViewModel;
        }
        private async void SaveCommandExecute()
        {
            IsLoading = true;
            var updateDialogRequest = new UpdateDialogRequest { DialogId = currentDialog.Id };

            if (newImageBytes != null)
            {
                var contentHelper = new ContentClientHelper(QuickbloxClient);
                updateDialogRequest.PhotoLink = (await contentHelper.UploadPublicImage(newImageBytes))?.Url;
            }

            if (currentDialog.Name != ChatName)
            {
                updateDialogRequest.Name = ChatName;
            }

            if (!string.IsNullOrEmpty(updateDialogRequest.PhotoLink) || !string.IsNullOrEmpty(updateDialogRequest.Name))
            {
                var updateDialogResponse = await QuickbloxClient.ChatClient.UpdateDialogAsync(updateDialogRequest);

                if (updateDialogResponse.StatusCode == HttpStatusCode.OK)
                {
                    var dialogsManager = ServiceLocator.Locator.Get<IDialogsManager>();
                    var dialog = dialogsManager.Dialogs.FirstOrDefault(d => d.Id == currentDialog.Id);

                    var groupChatManager = QuickbloxClient.ChatXmppClient.GetGroupChatManager(currentDialog.XmppRoomJid, currentDialog.Id);

                    if (!string.IsNullOrEmpty(updateDialogRequest.Name))
                    {
                        dialog.Name = updateDialogRequest.Name;
                        groupChatManager.NotifyGroupNameChanged(updateDialogRequest.Name, updateDialogResponse.Result.UpdateAt);
                    }

                    if (!string.IsNullOrEmpty(updateDialogRequest.PhotoLink))
                    {
                        dialog.Photo = updateDialogRequest.PhotoLink;
                        dialog.Image = ChatImage;
                        groupChatManager.NotifyGroupImageChanged(updateDialogRequest.PhotoLink, updateDialogResponse.Result.UpdateAt);
                    }
                    
                    IsLoading = false;
                    NavigationService.GoBack();
                }
            }

            IsLoading = false;
        }
        private async Task CreateGroup()
        {
            var selectedContactsIds = allContacts.Where(c => c.IsSelected).Select(c => c.Item.UserId).ToList();
            string selectedUsersString = BuildUsersString(selectedContactsIds);

            string imageLink = null;
            if (chatImageBytes != null)
            {
                var contentHelper = new ContentClientHelper(QuickbloxClient);
                imageLink = (await contentHelper.UploadPublicImage(chatImageBytes))?.Url;
            }

            var createDialogResponse = await QuickbloxClient.ChatClient.CreateDialogAsync(GroupName, DialogType.Group, selectedUsersString, imageLink);
            if (createDialogResponse.StatusCode == HttpStatusCode.Created)
            {
                var dialogVm = DialogViewModel.FromDialog(createDialogResponse.Result);
                dialogVm.Image = ChatImage;

                var dialogsManager = ServiceLocator.Locator.Get<IDialogsManager>();
                dialogsManager.Dialogs.Insert(0, dialogVm);

                int currentUserId = SettingsManager.Instance.ReadFromSettings<int>(SettingsKeys.CurrentUserId);
                var groupChatManager = QuickbloxClient.ChatXmppClient.GetGroupChatManager(createDialogResponse.Result.XmppRoomJid, createDialogResponse.Result.Id);
                groupChatManager.JoinGroup(currentUserId.ToString());


                groupChatManager.NotifyAboutGroupCreation(selectedContactsIds, createDialogResponse.Result);

                NavigationService.Navigate(ViewLocator.GroupChat, createDialogResponse.Result.Id);
            }
        }