[frontend/core] Reconstruct stored users to minimal state if they are malformed
This commit is contained in:
parent
57488e5641
commit
107160c690
2 changed files with 61 additions and 1 deletions
|
@ -9,3 +9,9 @@ public class StoredUser : UserResponse
|
||||||
public bool IsAdmin { get; set; }
|
public bool IsAdmin { get; set; }
|
||||||
public bool IsModerator { get; set; }
|
public bool IsModerator { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class MinimalStoredUser {
|
||||||
|
[JsonPropertyName("token")] public required string Token { get; set; }
|
||||||
|
public required string Id { get; set; }
|
||||||
|
public required string Username { get; set; }
|
||||||
|
}
|
|
@ -1,3 +1,4 @@
|
||||||
|
using System.Text.Json;
|
||||||
using Blazored.LocalStorage;
|
using Blazored.LocalStorage;
|
||||||
using Iceshrimp.Frontend.Core.Schemas;
|
using Iceshrimp.Frontend.Core.Schemas;
|
||||||
using Iceshrimp.Shared.Schemas.Web;
|
using Iceshrimp.Shared.Schemas.Web;
|
||||||
|
@ -15,7 +16,15 @@ internal class SessionService
|
||||||
LocalStorage = localStorage;
|
LocalStorage = localStorage;
|
||||||
Js = js;
|
Js = js;
|
||||||
Logger = logger;
|
Logger = logger;
|
||||||
|
try
|
||||||
|
{
|
||||||
Users = LocalStorage.GetItem<Dictionary<string, StoredUser>>("Users") ?? [];
|
Users = LocalStorage.GetItem<Dictionary<string, StoredUser>>("Users") ?? [];
|
||||||
|
}
|
||||||
|
catch (JsonException e)
|
||||||
|
{
|
||||||
|
Logger.LogError($"Stored users are malformed, reconstructing to a minimal form: {e.Message}");
|
||||||
|
Users = ReconstructUsers();
|
||||||
|
}
|
||||||
var lastUser = LocalStorage.GetItem<string?>("last_user");
|
var lastUser = LocalStorage.GetItem<string?>("last_user");
|
||||||
if (lastUser != null)
|
if (lastUser != null)
|
||||||
{
|
{
|
||||||
|
@ -79,6 +88,51 @@ internal class SessionService
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Dictionary<string, StoredUser> ReconstructUsers()
|
||||||
|
{
|
||||||
|
var newUsers = new Dictionary<string, StoredUser>();
|
||||||
|
|
||||||
|
var users = new Dictionary<string, MinimalStoredUser>();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
users = LocalStorage.GetItem<Dictionary<string, MinimalStoredUser>>("Users") ?? [];
|
||||||
|
}
|
||||||
|
catch (JsonException e)
|
||||||
|
{
|
||||||
|
Logger.LogWarning($"Users couldn't be reconstructed to a minimum state: {e.Message}");
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var user in users.Values)
|
||||||
|
{
|
||||||
|
var stored = new StoredUser
|
||||||
|
{
|
||||||
|
Id = user.Id,
|
||||||
|
Username = user.Username,
|
||||||
|
Host = null,
|
||||||
|
DisplayName = null,
|
||||||
|
AvatarUrl = $"/identicon/{user.Id}",
|
||||||
|
AvatarAlt = null,
|
||||||
|
BannerUrl = null,
|
||||||
|
BannerAlt = null,
|
||||||
|
InstanceName = null,
|
||||||
|
InstanceIconUrl = null,
|
||||||
|
IsBot = false,
|
||||||
|
IsCat = false,
|
||||||
|
Emojis = [],
|
||||||
|
MovedTo = null,
|
||||||
|
Token = user.Token,
|
||||||
|
IsAdmin = false,
|
||||||
|
IsModerator = false
|
||||||
|
};
|
||||||
|
|
||||||
|
newUsers.Add(user.Id, stored);
|
||||||
|
}
|
||||||
|
|
||||||
|
LocalStorage.SetItem("Users", newUsers);
|
||||||
|
LocalStorage.RemoveItem("last_user");
|
||||||
|
return newUsers;
|
||||||
|
}
|
||||||
|
|
||||||
public void DeleteUser(string id)
|
public void DeleteUser(string id)
|
||||||
{
|
{
|
||||||
if (id == Current?.Id) throw new ArgumentException("Cannot remove current user.");
|
if (id == Current?.Id) throw new ArgumentException("Cannot remove current user.");
|
||||||
|
|
Loading…
Add table
Reference in a new issue