Uwaa/Pleroma/Models/Status.cs

212 lines
6.1 KiB
C#
Raw 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 Status : ASObject
2024-11-22 09:55:08 +01:00
{
2024-12-11 03:49:46 +01:00
/// <summary>
/// HTML-encoded status content
/// </summary>
2024-11-22 09:55:08 +01:00
[JsonPropertyName("content")]
public string HtmlContent { get; set; } = null!;
2024-12-11 03:49:46 +01:00
/// <summary>
/// The account that authored this status
/// </summary>
2024-11-22 09:55:08 +01:00
[JsonPropertyName("account")]
public Account Account { get; set; } = null!;
2024-12-11 03:49:46 +01:00
/// <summary>
/// The date when this status was created
/// </summary>
2024-11-22 09:55:08 +01:00
[JsonPropertyName("created_at")]
public DateTime CreatedAt { get; set; }
2024-12-18 10:56:02 +01:00
/// <summary>
/// Have you favourited this status?
/// </summary>
[JsonPropertyName("favourited")]
public bool Favourited { get; set; }
/// <summary>
/// How many favourites this status has received
/// </summary>
[JsonPropertyName("favourites_count")]
public int FavouriteCount { get; set; }
2024-11-22 09:55:08 +01:00
2024-12-11 03:49:46 +01:00
/// <summary>
/// ID of the account being replied to
/// </summary>
2024-11-22 09:55:08 +01:00
[JsonPropertyName("in_reply_to_account_id")]
public string? ReplyToAccount { get; set; } = null;
2024-12-11 03:49:46 +01:00
/// <summary>
/// ID of the status being replied
/// </summary>
2024-11-22 09:55:08 +01:00
[JsonPropertyName("in_reply_to_id")]
2024-12-18 10:56:02 +01:00
public string? ReplyTo { get; set; } = null;
2024-11-22 09:55:08 +01:00
2024-12-11 03:49:46 +01:00
/// <summary>
/// Primary language of this status
/// </summary>
2024-11-22 09:55:08 +01:00
[JsonPropertyName("language")]
public string? Language { get; set; }
2024-12-18 10:56:02 +01:00
/// <summary>
/// Media that is attached to this status
/// </summary>
[JsonPropertyName("media_attachments")]
public Attachment[] Attachments { get; set; } = Array.Empty<Attachment>();
/// <summary>
/// Mentions of users within the status content
/// </summary>
[JsonPropertyName("mentions")]
public Mention[] Mentions { get; set; } = Array.Empty<Mention>();
2024-12-11 03:49:46 +01:00
/// <summary>
/// Have you pinned this status? Only appears if the status is pinnable.
/// </summary>
2024-11-22 09:55:08 +01:00
[JsonPropertyName("pinned")]
public bool Pinned { get; set; }
2024-12-18 10:56:02 +01:00
/// <summary>
/// Additional pleroma-specific data.
/// </summary>
[JsonPropertyName("pleroma")]
public PleromaStatusData Pleroma { get; set; } = null!;
/// <summary>
/// The status being reblogged
/// </summary>
[JsonPropertyName("reblog")]
public Status? Reblog { get; set; }
/// <summary>
/// Have you boosted this status?
/// </summary>
[JsonPropertyName("reblogged")]
public bool Reblogged { get; set; }
/// <summary>
/// How many boosts this status has received
/// </summary>
[JsonPropertyName("reblogs_count")]
public int ReblogCount { get; set; }
/// <summary>
/// How many replies this status has received
/// </summary>
[JsonPropertyName("replies_count")]
public int ReplyCount { get; set; }
/// <summary>
/// Is this status marked as sensitive content?
/// </summary>
[JsonPropertyName("sensitive")]
public bool Sensitive { get; set; }
/// <summary>
/// Subject or summary line, below which status content is collapsed until expanded
/// </summary>
[JsonPropertyName("spoiler_text")]
public string SpoilerText { get; set; } = null!;
/// <summary>
/// Hashtags included in the status.
/// </summary>
[JsonPropertyName("tags")]
public Hashtag[] Tags { get; set; } = Array.Empty<Hashtag>();
/// <summary>
/// URI of the status used for federation
/// </summary>
[JsonPropertyName("uri")]
public string URI { get; set; } = null!;
/// <summary>
/// A link to the status's HTML representation
/// </summary>
[JsonPropertyName("url")]
public string? URL { get; set; }
2024-12-11 03:49:46 +01:00
/// <summary>
/// Visibility of this status
/// </summary>
[JsonPropertyName("visibility")]
public StatusVisibility Visibility { get; set; }
/// <summary>
/// Plain text content of that status.
/// </summary>
2024-11-22 09:55:08 +01:00
[JsonIgnore]
2024-11-30 14:14:31 +01:00
public string Content => Pleroma?.Content?.Plain ?? HtmlContent;
2024-11-22 09:55:08 +01:00
2024-12-18 10:56:02 +01:00
/// <summary>
/// Returns true if a status mentions or replies to a user.
/// </summary>
2024-12-21 20:01:57 +01:00
/// <param name="user">The ID of the user.</param>
public bool IsInteracting(AccountID user)
2024-11-22 09:55:08 +01:00
{
2024-12-21 20:01:57 +01:00
return ReplyToAccount == user.ID || Mentions.Any(m => m.ID == user.ID);
2024-11-22 09:55:08 +01:00
}
public override string ToString() => $"{Account?.Username ?? "unknown"}: \"{Content}\"";
}
2024-12-18 10:56:02 +01:00
/// <summary>
/// Additional pleroma-specific data for a <see cref="Status"/>.
/// </summary>
public class PleromaStatusData
{
[JsonPropertyName("content")]
2024-12-21 20:01:57 +01:00
public PleromaStatusContent Content { get; set; } = null!;
2024-12-18 10:56:02 +01:00
[JsonPropertyName("local")]
public bool Local { get; set; }
2024-12-21 20:01:57 +01:00
[JsonPropertyName("quote_id")]
public string? QuoteID { get; set; }
2024-12-18 10:56:02 +01:00
}
2024-12-21 20:01:57 +01:00
public class PleromaStatusContent
2024-12-18 10:56:02 +01:00
{
[JsonPropertyName("text/plain")]
public string Plain { get; set; } = null!;
}
[JsonConverter(typeof(StatusIDConverter))]
2024-12-21 20:01:57 +01:00
public readonly struct StatusID(string id) : IEquatable<StatusID>
2024-12-18 10:56:02 +01:00
{
public static implicit operator StatusID(string id) => new StatusID(id);
public static implicit operator StatusID(Status status) => new StatusID(status.ID);
public readonly string ID = id;
public override string ToString() => ID;
2024-12-21 20:01:57 +01:00
public static bool operator ==(StatusID left, StatusID right) => left.Equals(right);
public static bool operator !=(StatusID left, StatusID right) => !(left == right);
public override bool Equals(object? obj) => (obj is StatusID id && Equals(id)) || (obj is Status status && Equals(status));
public bool Equals(StatusID other) => ID == other.ID;
public override int GetHashCode() => ID.GetHashCode();
2024-12-18 10:56:02 +01:00
}
class StatusIDConverter : JsonConverter<StatusID>
{
public override bool CanConvert(Type type) => type.IsAssignableTo(typeof(StatusID));
public override StatusID Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new StatusID(reader.GetString() ?? throw new NullReferenceException("Expected a string, got null"));
}
public override void Write(Utf8JsonWriter writer, StatusID value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ID);
}
}