From f592485750877cbed3a199778c97ddb39898be02 Mon Sep 17 00:00:00 2001 From: Eduardo Braga Date: Tue, 2 Apr 2024 12:08:47 -0300 Subject: [PATCH] Add a new type StatusParameters --- Mastonet.Entities/Status.cs | 48 ++++++++++++++++++ Mastonet/MastodonClient.Status.cs | 84 ++++++++++++++++++++++++++++--- 2 files changed, 124 insertions(+), 8 deletions(-) diff --git a/Mastonet.Entities/Status.cs b/Mastonet.Entities/Status.cs index 9ca3624..77045cc 100644 --- a/Mastonet.Entities/Status.cs +++ b/Mastonet.Entities/Status.cs @@ -198,3 +198,51 @@ public class Status [JsonPropertyName("pinned")] public bool? Pinned { get; set; } } + +public class StatusParameters +{ + /// + /// The text of the status + /// + public string Status { get; set; } = string.Empty; + + /// + /// Either "direct", "private", "unlisted" or "public" + /// + public Visibility? Visibility { get; set; } + + /// + /// Local ID of the status you want to reply to + /// + public string? ReplyStatusId { get; set; } + + /// + /// Array of media IDs to attach to the status (maximum 4) + /// + public IEnumerable? MediaIds { get; set; } + + /// + /// Set this to mark the media of the status as NSFW + /// + public bool Sensitive { get; set; } + + /// + /// Text to be shown as a warning before the actual content + /// + public string? SpoilerText { get; set; } + + /// + /// DateTime to schedule posting of status + /// + public DateTime? ScheduledAt { get; set; } + + /// + /// Override language code of the toot (ISO 639-2) + /// + public string? Language { get; set; } + + /// + /// Nested parameters to attach a poll to the status + /// + public PollParameters? PollParameters { get; set; } +} diff --git a/Mastonet/MastodonClient.Status.cs b/Mastonet/MastodonClient.Status.cs index f730c39..8f3ea06 100644 --- a/Mastonet/MastodonClient.Status.cs +++ b/Mastonet/MastodonClient.Status.cs @@ -91,7 +91,7 @@ public Task> GetFavouritedBy(string statusId, ArrayOptions /// Returns Status public Task PublishStatus(string status, Visibility? visibility = null, string? replyStatusId = null, IEnumerable? mediaIds = null, bool sensitive = false, string? spoilerText = null, - DateTime? scheduledAt = null, string? language = null, PollParameters? poll = null) + DateTime? scheduledAt = null, string? language = null, PollParameters? pollParameters = null) { if (string.IsNullOrEmpty(status) && (mediaIds == null || !mediaIds.Any())) { @@ -141,18 +141,86 @@ public Task PublishStatus(string status, Visibility? visibility = null, data.Add(new KeyValuePair("language", language)); } - if (poll != null) + if (pollParameters != null) { - data.AddRange(poll.Options.Select(option => new KeyValuePair("poll[options][]", option))); - data.Add(new KeyValuePair("poll[expires_in]", poll.ExpiresIn.TotalSeconds.ToString())); - if (poll.Multiple.HasValue) + data.AddRange(pollParameters.Options.Select(option => new KeyValuePair("poll[options][]", option))); + data.Add(new KeyValuePair("poll[expires_in]", pollParameters.ExpiresIn.TotalSeconds.ToString())); + if (pollParameters.Multiple.HasValue) { - data.Add(new KeyValuePair("poll[multiple]", poll.Multiple.Value.ToString().ToLowerInvariant())); + data.Add(new KeyValuePair("poll[multiple]", pollParameters.Multiple.Value.ToString().ToLowerInvariant())); } - if (poll.HideTotals.HasValue) + if (pollParameters.HideTotals.HasValue) + { + data.Add(new KeyValuePair("poll[hide_totals]", pollParameters.HideTotals.Value.ToString().ToLowerInvariant())); + } + } + + return Post("/api/v1/statuses", data); + } + + public Task PublishStatus(StatusParameters statusParameters) + { + if (string.IsNullOrEmpty(statusParameters.Status) && (statusParameters.MediaIds == null || !statusParameters.MediaIds.Any())) + { + throw new ArgumentException("A status must have either text (status) or media (mediaIds)", nameof(statusParameters.Status)); + } + + var data = new List>() + { + new KeyValuePair("status", statusParameters.Status), + }; + + if (!string.IsNullOrEmpty(statusParameters.ReplyStatusId)) + { + data.Add(new KeyValuePair("in_reply_to_id", statusParameters.ReplyStatusId!)); + } + + if (statusParameters.MediaIds != null) + { + foreach (var mediaId in statusParameters.MediaIds) + { + data.Add(new KeyValuePair("media_ids[]", mediaId)); + } + } + + if (statusParameters.Sensitive) + { + data.Add(new KeyValuePair("sensitive", "true")); + } + + if (statusParameters.SpoilerText != null) + { + data.Add(new KeyValuePair("spoiler_text", statusParameters.SpoilerText)); + } + + if (statusParameters.Visibility.HasValue) + { + data.Add(new KeyValuePair("visibility", statusParameters.Visibility.Value.ToString().ToLowerInvariant())); + } + + if (statusParameters.ScheduledAt.HasValue) + { + data.Add(new KeyValuePair("scheduled_at", statusParameters.ScheduledAt.Value.ToString("o"))); + } + + if (statusParameters.Language != null) + { + data.Add(new KeyValuePair("language", statusParameters.Language)); + } + + if (statusParameters.PollParameters != null) + { + data.AddRange(statusParameters.PollParameters.Options.Select(option => new KeyValuePair("poll[options][]", option))); + data.Add(new KeyValuePair("poll[expires_in]", statusParameters.PollParameters.ExpiresIn.TotalSeconds.ToString())); + if (statusParameters.PollParameters.Multiple.HasValue) + { + data.Add(new KeyValuePair("poll[multiple]", statusParameters.PollParameters.Multiple.Value.ToString().ToLowerInvariant())); + } + + if (statusParameters.PollParameters.HideTotals.HasValue) { - data.Add(new KeyValuePair("poll[hide_totals]", poll.HideTotals.Value.ToString().ToLowerInvariant())); + data.Add(new KeyValuePair("poll[hide_totals]", statusParameters.PollParameters.HideTotals.Value.ToString().ToLowerInvariant())); } }