private async Task DeserializeInboxMails(int mailCount) { var index = 1; var emails = await PhantasmaService.GetAllInboxMessages(mailCount); try { //deserialization foreach (var email in emails) { if (email.StartsWith("{") || email.StartsWith("[")) { var mailObject = JsonConvert.DeserializeObject <Message>(email, AppSettings.JsonSettings()); if (mailObject?.TextContent != null) { if (MessageUtils.IsHex(mailObject.TextContent.ToCharArray())) { var encryptedText = mailObject.TextContent.HexToBytes(); var remotePub = await PhantasmaService.GetMailboxPublicKey(mailObject.FromInbox); var decryptedText = EncryptionUtils.Decrypt(encryptedText, AuthenticationService.AuthenticatedUser.GetPrivateKey(), remotePub.HexToBytes()); mailObject.TextContent = decryptedText; } mailObject.ID = index; InboxList.Add(mailObject); } } index++; } } catch (Exception e) { Debug.WriteLine(e); } InboxList = new ObservableCollection <Message>(InboxList.OrderByDescending(p => p.Date) .ThenByDescending(p => p.Date.Hour).ToList()); _fullInboxList = InboxList.ToList(); }
//Implement login function private async void LoginImpl(string psw, bool logIt) { if (string.IsNullOrEmpty(psw)) { return; } //Update client settings UpdateSettings(UseImap); this.encryptedPassword = null; IsRequesting = true; //TODO Login await Task.Run(() => { try { //Ensure client created. if (globalClient == null) { globalClient = clientCreator.Invoke(); } if (globalClient.IsConnected) { globalClient.Disconnect(); } Application.Current.Dispatcher.Invoke(() => { InboxList.Clear(); }); //Connect string userAddress = UserAddress; IsLandingIn = true; //Connect to server. globalClient.Connect(settings.Host, settings.Port, settings.EnableSsl); //Login if (!globalClient.Login(userAddress, psw, () => { IsLandingIn = false; this.encryptedPassword = psw.Protect(); this.IsLogined = true; //Notify login state Messenger.Default.Send(new NavigationMessage(), Tokens.Login); Messenger.Default.Send(new DisplayMessage("Login Successful!", DisplayType.Toast)); //Delete old inbox folder var dir = new DirectoryInfo(InboxFolder); if (dir.Exists) { dir.Delete(true); } //Do on logined AppSettings.Default.LastLogUser = userAddress; if (logIt) { //Log to local. userAssistant.StoreUserLocal(userAddress, psw); AppSettings.Default.LastLogUser = userAddress; AppSettings.Default.AutoLogin = true; } AppSettings.Default.Save(); DisplayName = userAssistant.GetUserDisplayName(userAddress); userAssistant.SaveToRecent(userAddress, DisplayName); //End }, msg => { Application.Current.Dispatcher.Invoke(() => { InboxList.Add(msg); }); })) { Messenger.Default.Send(new DisplayMessage("Login Error, authenticate fail.")); } IsLandingIn = false; } catch (Exception e) { string tips = globalClient == null ? "Create client fail." : (globalClient.IsConnected ? (globalClient.IsAuthenticated ? "Fetch messages fail." : "Login Error, authenticate fail.") : "Connect to service fail."); Messenger.Default.Send(new DisplayMessage(tips, e.Message)); } }); IsRequesting = false; IsLandingIn = false; }