Uwaa/Pleroma/Models/Account.cs
2024-12-21 19:01:57 +00:00

97 lines
No EOL
2.8 KiB
C#

namespace Uwaa.Pleroma;
public class Account : ASObject
{
[JsonPropertyName("bot")]
public bool Bot { get; set; }
[JsonPropertyName("display_name")]
public string DisplayName { get; set; } = null!;
[JsonPropertyName("fields")]
public AccountField[] Fields { get; set; } = Array.Empty<AccountField>();
[JsonPropertyName("followers_count")]
public uint Followers { get; set; }
[JsonPropertyName("following_count")]
public uint Following { get; set; }
[JsonPropertyName("local")]
public bool Local { get; set; }
[JsonPropertyName("locked")]
public bool Locked { get; set; }
[JsonPropertyName("note")]
public string Bio { get; set; } = null!;
[JsonPropertyName("statuses_count")]
public uint StatusesCount { get; set; }
[JsonPropertyName("url")]
public string URL { get; set; } = null!;
[JsonPropertyName("username")]
public string Username { get; set; } = null!;
[JsonPropertyName("acct")]
public string Webfinger { get; set; } = null!;
public string? Instance
{
get
{
if (Webfinger == null)
return null;
int spl = Webfinger.IndexOf('@');
if (spl == -1)
return null;
return Webfinger[(spl + 1)..];
}
}
public override string ToString() => $"@{Webfinger}";
}
[JsonConverter(typeof(AccountIDConverter))]
public readonly struct AccountID(string id)
{
public static implicit operator AccountID(string id) => new AccountID(id);
public static implicit operator AccountID(Account account) => new AccountID(account.ID);
public static implicit operator AccountID(Relationship relationship) => new AccountID(relationship.ID);
public readonly string ID = id;
public override string ToString() => ID;
public static bool operator ==(AccountID left, AccountID right) => left.Equals(right);
public static bool operator !=(AccountID left, AccountID right) => !(left == right);
public override bool Equals(object? obj) => (obj is AccountID id && Equals(id)) || (obj is Account status && Equals(status));
public bool Equals(AccountID other) => ID == other.ID;
public override int GetHashCode() => ID.GetHashCode();
}
class AccountIDConverter : JsonConverter<AccountID>
{
public override bool CanConvert(Type type) => type.IsAssignableTo(typeof(AccountID));
public override AccountID Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new AccountID(reader.GetString() ?? throw new NullReferenceException("Expected a string, got null"));
}
public override void Write(Utf8JsonWriter writer, AccountID value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ID);
}
}