private async Task InitImpl(IRedditService redditService) { _redditService = redditService; _currentUser = await TryDefaultUser(); if (_currentUser == null) _currentUser = CreateAnonUser(); Messenger.Default.Send<UserLoggedInMessage>(new UserLoggedInMessage { CurrentUser = _currentUser, UserTriggered = false }); }
public async Task<User> TryStoredLogin(string username) { var result = await DoLogin(username, true); if (result != null) { _currentUser = result; Messenger.Default.Send<UserLoggedInMessage>(new UserLoggedInMessage { CurrentUser = result, UserTriggered = true }); } return result; }
public async Task<User> GetUser() { //if (!_initTask.IsCompleted) // await _initTask; if(_currentUser == null) _currentUser = CreateAnonUser(); return _currentUser; }
public async Task<User> TryLogin(string username, string password) { var result = await _redditService.Login(username, password); if (result != null) { _currentUser = result; Messenger.Default.Send<UserLoggedInMessage>(new UserLoggedInMessage { CurrentUser = result, UserTriggered = true }); } return result; }
//this one is seperated out so we can use it interally on initial user login public async Task<Account> GetMe(User user) { bool needsRetry = false; try { var meString = await _simpleHttpService.SendGet(user.LoginCookie, "http://www.reddit.com/api/me.json"); if (!string.IsNullOrWhiteSpace(meString) && meString != "{}") { var thing = JsonConvert.DeserializeObject<Thing>(meString); return (new TypedThing<Account>(thing)).Data; } else return null; } catch (WebException webException) { if (webException.Status == WebExceptionStatus.RequestCanceled) needsRetry = true; else { _notificationService.CreateErrorNotification(webException); return null; } } catch (Exception ex) { _notificationService.CreateErrorNotification(ex); return null; } if (needsRetry) { return await GetMe(); } else return null; }
public void Logout() { _currentUser = CreateAnonUser(); Messenger.Default.Send<UserLoggedInMessage>(new UserLoggedInMessage { CurrentUser = _currentUser, UserTriggered = true }); }
private async Task<User> LoginWithCredentials(UserCredential credential, bool userInitiated) { var originalCookie = credential.LoginCookie; if (!string.IsNullOrWhiteSpace(credential.LoginCookie)) { var loggedInUser = new User { Username = credential.Username, LoginCookie = credential.LoginCookie, NeedsCaptcha = false }; if (userInitiated) { loggedInUser.Me = await _redditService.GetMe(loggedInUser); } else { ThreadPool.RunAsync(async (o) => { await Task.Delay(5000); try { loggedInUser.Me = await _redditService.GetMe(loggedInUser); } catch { } if (loggedInUser.Me == null) ServiceLocator.Current.GetInstance<INotificationService>().CreateNotification(string.Format("Failed to login with user {0}", credential.Username)); }); } return loggedInUser; } else { //we dont currently posses a valid login cookie, see if windows has a stored credential we can use for this username var userInfoDb = await GetUserInfoDB(); var passwordCursor = await userInfoDb.SelectAsync(userInfoDb.GetKeys().First(), "passwords", DBReadFlags.NoLock); if (passwordCursor != null) { using (passwordCursor) { do { try { var passwordData = JsonConvert.DeserializeObject<PasswordData>(passwordCursor.GetString()); if (credential.LoginCookie == passwordData.LastCookie) { return await _redditService.Login(credential.Username, passwordData.Password); } } catch { } } while (await passwordCursor.MoveNextAsync()); } } } return null; }
public async Task<User> Login(string username, string password) { var loginUri = "https://ssl.reddit.com/api/login"; var postContent = new Dictionary<string, string> { { "api_type", "json" }, { "user", username }, { "passwd", password } }; var loginResult = await _simpleHttpService.SendPostForCookies(postContent, loginUri); var jsonResult = loginResult.Item1; var loginResultThing = JsonConvert.DeserializeObject<LoginJsonThing>(jsonResult); if (loginResultThing == null || loginResultThing.Json == null || (loginResultThing.Json.Errors != null && loginResultThing.Json.Errors.Length != 0)) { _notificationService.CreateNotification(string.Format("Failed to login as User:{0}", username)); return null; //errors in the login process } else { var loginCookie = loginResult.Item2["reddit_session"]; var user = new User { Authenticated = true, LoginCookie = loginCookie, Username = username, NeedsCaptcha = false }; user.Me = await GetMe(user); return user; } }
private async Task<User> LoginWithCredentials(UserCredential credential) { if (await _redditService.CheckLogin(credential.LoginCookie)) { var loggedInUser = new User { Username = credential.Username, LoginCookie = credential.LoginCookie, NeedsCaptcha = false }; loggedInUser.Me = await _redditService.GetMe(loggedInUser); return loggedInUser; } else { //we dont currently posses a valid login cookie, see if windows has a stored credential we can use for this username var passwordVault = new Windows.Security.Credentials.PasswordVault(); try { var windowsCredentials = passwordVault.FindAllByResource("Baconography"); var matchingWindowsCredential = windowsCredentials.FirstOrDefault(windowsCredential => string.Compare(windowsCredential.UserName, credential.Username, StringComparison.CurrentCultureIgnoreCase) == 0); if (matchingWindowsCredential != null) { matchingWindowsCredential.RetrievePassword(); return await _redditService.Login(matchingWindowsCredential.UserName, matchingWindowsCredential.Password); } } catch { } } return null; }
public async Task<Listing> GetMessages(User user) { await Initialize(); try { var things = await RetrieveOrderedThings("messages-" + user.Username, TimeSpan.FromDays(1)); if (things == null) return new Listing { Data = new ListingData { Children = new List<Thing>() } }; else return new Listing { Data = new ListingData { Children = things.ToList() } }; } catch { return new Listing { Data = new ListingData { Children = new List<Thing>() } }; } }
public async Task<bool> UserHasOfflineMessages(User user) { await Initialize(); if (user == null || string.IsNullOrEmpty(user.Username)) return false; string key = "messages-" + user.Username; using (var cursor = await _blobStoreDb.SeekAsync(_blobStoreDb.GetKeys()[0], BitConverter.GetBytes(key.GetHashCode()), DBReadFlags.NoLock)) { if (cursor != null) { var gottenBlob = cursor.Get(); var microseconds = BitConverter.ToInt64(gottenBlob, 4); var updatedTime = new DateTime(microseconds * 10).AddYears(1969); var blobAge = DateTime.Now - updatedTime; if (blobAge <= TimeSpan.FromDays(1)) { return true; } } } return false; }
public async Task StoreMessages(User user, Listing listing) { await Initialize(); try { if (listing == null || listing.Data.Children.Count == 0) return; if (user == null || String.IsNullOrEmpty(user.Username)) return; await StoreOrderedThings("messages-" + user.Username, listing.Data.Children); } catch (Exception ex) { //_notificationService.CreateErrorNotification(ex); } }
private static string CleanRedditLink(string userInput, User user) { if (string.IsNullOrWhiteSpace(userInput)) return "/"; if (userInput == "/") return userInput; if (user != null && !string.IsNullOrWhiteSpace(user.Username)) { var selfMulti = "/" + user.Username + "/m/"; if (userInput.Contains(selfMulti)) { return "/me/m/" + userInput.Substring(userInput.IndexOf(selfMulti) + selfMulti.Length); } } if (userInput.StartsWith("me/m/")) return "/" + userInput; else if (userInput.StartsWith("/m/")) return "/me" + userInput; else if (userInput.StartsWith("/me/m/")) return userInput; if (userInput.StartsWith("/u/")) { return userInput.Replace("/u/", "/user/"); } if (userInput.StartsWith("r/")) return "/" + userInput; else if (userInput.StartsWith("/") && !userInput.StartsWith("/r/")) return "/r" + userInput; else if (userInput.StartsWith("/r/")) return userInput; else return "/r/" + userInput; }
private Listing MaybeStoreMessages(User user, Listing listing) { if (user == null || string.IsNullOrEmpty(user.Username)) return listing; lock (_currentlyStoringMessages) { if (_currentlyStoringMessages.ContainsKey(user.Username)) return listing; _currentlyStoringMessages.Add(user.Username, listing); } _offlineService.StoreMessages(user, listing).ContinueWith(task => { lock (_currentlyStoringMessages) { _currentlyStoringMessages.Remove(user.Username); } }); return listing; }
private async Task MaybeStoreSubscribedSubredditListing(Listing listing, User user) { try { if (user != null && user.Username != null && listing != null && listing.Data.Children != null && listing.Data.Children.Count > 0) { await _offlineService.StoreOrderedThings("sublist:" + user.Username, listing.Data.Children); } } catch { } }
public async Task<Account> GetMe(User user) { if (_settingsService.IsOnline()) { return await _redditService.GetMe(); } else { var thing = await _offlineService.RetrieveThing(string.Format("account-user:{0}", user.Username), TimeSpan.FromDays(1024)); return thing != null ? thing.Data as Account : null; } }