public static async Task UpdateOneCharacter(StructureContext context, Character character, bool force = false) { if (string.IsNullOrEmpty(character.AccessToken)) { throw new Exception("User had no existing token."); } if (character.ExpiresAt < DateTime.UtcNow) { var token = await EVESwagger.GetToken(Token.REFRESH, character.RefreshToken); character.ConsumeToken(token); context.Update(character); await context.SaveChangesAsync(); } //get notifications // if (force || character.ExpiresAt < DateTime.UtcNow){ var notifications = await EVESwagger.GetNotificationsByCharacterId(character.CharacterID, character.AccessToken); //deduplciated //TODO this SHould work except it doesnt so we need to figure out why. // var Current = context.Notifications.Select(x => x.NotificationId).ToArray(); notifications = notifications.Where(n => context.Notifications.Select(x => x.NotificationId).Contains(n.NotificationId) == false).ToList(); context.Notifications.AddRange(notifications); context.SaveChanges(); // } }
public async Task GetCodeAsync(string code, string state) { var token = await EVESwagger.GetToken("authorization_code", code); var auth_char = await EVESwagger.Verify(token); using (var context = new StructureContext()) { //if we have on update it to use the token we just got or bail. var fromdb = context.Characters.Where(c => c.CharacterID == auth_char.CharacterID).FirstOrDefault(); if (fromdb == null) { context.Characters.Attach(auth_char); //If we have just signed in for the first time, force update the notifications for that character var ignoreWarning = StructureWatch.Services.Polling.UpdateOneCharacter(context, auth_char, true); } else { fromdb.ConsumeToken(token); context.Characters.Update(fromdb); } context.SaveChanges(); //sign in the user: var principal = new EveSSOClaim().BuildClaimsPrincipal(context, auth_char.CharacterName); await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal); HttpContext.User = principal; } Response.Redirect("/LoggedIn"); }
public void OnGet(long id) { NotificationsAboutId = context.Notifications.Where(n => n.Text.Contains("//" + id + "\"")) .OrderByDescending(n => n.Timestamp).ToList(); Structure = context.Structures.Where(s => s.StructureId == id).FirstOrDefault(); if (Structure == null && NotificationsAboutId.Count > 0) { //create it if we have seen it before in notificiations. Structure = new Structure(); Structure.Name = Regex.Replace(NotificationsAboutId[0].ParsedData["structureLink"], "<.+?>", ""); Structure.StructureId = long.Parse(Regex .Match(NotificationsAboutId[0].ParsedData["structureLink"], @"//(\d+)").Groups[1].Value); Structure.FirstSeen = NotificationsAboutId[NotificationsAboutId.Count - 1].Timestamp; context.Attach(Structure); context.SaveChanges(); } }