Uwaa/Pleroma/Models/Account.cs

113 lines
3.5 KiB
C#
Raw Permalink Normal View History

2024-12-13 13:25:20 +01:00
namespace Uwaa.Pleroma;
2024-11-22 09:55:08 +01:00
2024-12-21 20:01:57 +01:00
public class Account : ASObject
2024-11-22 09:55:08 +01:00
{
[JsonPropertyName("bot")]
public bool Bot { get; set; }
2025-01-16 19:38:50 +01:00
[JsonPropertyName("created_at")]
public DateTimeOffset CreatedAt { get; set; }
2024-11-22 09:55:08 +01:00
[JsonPropertyName("display_name")]
public string DisplayName { get; set; } = null!;
2024-12-18 10:56:02 +01:00
[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("locked")]
public bool Locked { get; set; }
[JsonPropertyName("note")]
public string Bio { get; set; } = null!;
2024-11-22 09:55:08 +01:00
[JsonPropertyName("statuses_count")]
public uint StatusesCount { get; set; }
[JsonPropertyName("url")]
public string URL { get; set; } = null!;
2024-12-18 10:56:02 +01:00
[JsonPropertyName("username")]
public string Username { get; set; } = null!;
2024-11-22 09:55:08 +01:00
[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)..];
}
}
2024-12-30 02:14:55 +01:00
/// <summary>
/// If true, the user opts-out of automated interactions. Bots must not follow these users, nor reply, favorite, boost, or store posts from these users.
/// </summary>
public bool NoBot => Bio != null && Bio.Contains("#nobot", StringComparison.OrdinalIgnoreCase);
2024-12-26 00:34:13 +01:00
[JsonConstructor()]
2024-12-26 00:09:26 +01:00
internal Account()
{
}
public override string ToString() => $"@{Webfinger}";
2024-11-22 09:55:08 +01:00
}
2024-12-18 10:56:02 +01:00
[JsonConverter(typeof(AccountIDConverter))]
public readonly struct AccountID(string id)
{
public static implicit operator AccountID(string id) => new AccountID(id);
2025-01-04 15:54:45 +01:00
public static implicit operator AccountID?(string? id) => id == null ? new AccountID?() : new AccountID(id);
2024-12-18 10:56:02 +01:00
public static implicit operator AccountID(Account account) => new AccountID(account.ID);
2025-01-04 15:54:45 +01:00
public static implicit operator AccountID?(Account? account) => account == null ? new AccountID?() : new AccountID(account.ID);
2024-12-18 10:56:02 +01:00
public static implicit operator AccountID(Relationship relationship) => new AccountID(relationship.ID);
2025-01-04 15:54:59 +01:00
public static implicit operator AccountID(AccountInfo userInfo) => new AccountID(userInfo.ID);
2024-12-18 10:56:02 +01:00
public readonly string ID = id;
public override string ToString() => ID;
2024-12-21 20:01:57 +01:00
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();
2024-12-18 10:56:02 +01:00
}
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);
}
}