118 lines
3.8 KiB
C#
118 lines
3.8 KiB
C#
using LiteDB;
|
|
using static DeluxeBackend.Enums;
|
|
|
|
namespace DeluxeBackend.Models
|
|
{
|
|
public class Account
|
|
{
|
|
[BsonId]
|
|
public long Id { get; set; }
|
|
|
|
public required string Username { get; set; } = string.Empty;
|
|
|
|
public string PasswordHash { get; set; } = string.Empty;
|
|
|
|
public List<string> Roles { get; set; } = [];
|
|
|
|
public string DisplayName { get; set; } = string.Empty;
|
|
public string ImageName { get; set; } = "DefaultProfileImage";
|
|
public DateOnly? Birthday { get; set; } = null;
|
|
public string Bio { get; set; } = string.Empty;
|
|
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
|
|
public ulong? DiscordId { get; set; } = null;
|
|
|
|
public PronounsType Pronouns { get; set; } = PronounsType.None;
|
|
public IdentityFlagsType IdentityFlags { get; set; } = IdentityFlagsType.None;
|
|
|
|
public Dictionary<long, DateTime> VisitedRooms { get; set; } = [];
|
|
public bool IsRecentHistoryVisible { get; set; } = true;
|
|
|
|
[BsonIgnore]
|
|
public bool? IsJunior
|
|
{
|
|
get
|
|
{
|
|
if (!Birthday.HasValue) return null;
|
|
|
|
var today = DateOnly.FromDateTime(DateTime.UtcNow);
|
|
return Birthday.Value.AddYears(13) > today;
|
|
}
|
|
}
|
|
|
|
[BsonIgnore]
|
|
private readonly Dictionary<string, bool> _roleCache = new(StringComparer.OrdinalIgnoreCase);
|
|
|
|
[BsonIgnore]
|
|
private DateTime _cacheExpiration = DateTime.MinValue;
|
|
|
|
[BsonIgnore]
|
|
private static readonly TimeSpan CacheDuration = TimeSpan.FromMinutes(5);
|
|
|
|
[BsonIgnore]
|
|
public async Task<bool> HasRoleAsync(DiscordBotService discord, string role)
|
|
{
|
|
if (Roles.Any(r => r.Equals(role, StringComparison.OrdinalIgnoreCase)))
|
|
return true;
|
|
|
|
if (!DiscordId.HasValue)
|
|
return false;
|
|
|
|
if (DateTime.UtcNow > _cacheExpiration)
|
|
{
|
|
_roleCache.Clear();
|
|
_cacheExpiration = DateTime.UtcNow.Add(CacheDuration);
|
|
}
|
|
|
|
if (_roleCache.TryGetValue(role, out bool hasRole))
|
|
{
|
|
return hasRole;
|
|
}
|
|
|
|
bool externalHasRole = await discord.HasRole(DiscordId.Value, role);
|
|
_roleCache[role] = externalHasRole;
|
|
|
|
return externalHasRole;
|
|
}
|
|
|
|
#pragma warning disable CS8601 // Possible null reference assignment.
|
|
|
|
[BsonIgnore]
|
|
public Dictionary<string, object> ToDictionaryMe()
|
|
{
|
|
return new Dictionary<string, object>
|
|
{
|
|
["accountId"] = Id,
|
|
["availableUsernameChanges"] = 0,
|
|
["birthday"] = Birthday?.ToString("o"),
|
|
["username"] = Username,
|
|
["displayName"] = DisplayName ?? Username,
|
|
["profileImage"] = ImageName,
|
|
["isJunior"] = IsJunior,
|
|
["platforms"] = 1,//steam,
|
|
["personalPronouns"] = (int)Pronouns,
|
|
["identityFlags"] = (int)IdentityFlags,
|
|
["createdAt"] = CreatedAt.ToString("o")
|
|
};
|
|
}
|
|
[BsonIgnore]
|
|
public Dictionary<string, object> ToDictionary()
|
|
{
|
|
return new Dictionary<string, object>
|
|
{
|
|
["accountId"] = Id,
|
|
["username"] = Username,
|
|
["displayName"] = DisplayName ?? Username,
|
|
["profileImage"] = ImageName,
|
|
["isJunior"] = IsJunior,
|
|
["platforms"] = 1,//steam,
|
|
["personalPronouns"] = (int)Pronouns,
|
|
["identityFlags"] = (int)IdentityFlags,
|
|
["createdAt"] = CreatedAt.ToString("o")
|
|
};
|
|
}
|
|
#pragma warning restore CS8601
|
|
|
|
}
|
|
|
|
}
|