Skip to content

Commit

Permalink
Add a new type StatusParameters
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardobragaxz committed Apr 2, 2024
1 parent 1874401 commit f592485
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 8 deletions.
48 changes: 48 additions & 0 deletions Mastonet.Entities/Status.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,51 @@ public class Status
[JsonPropertyName("pinned")]
public bool? Pinned { get; set; }
}

public class StatusParameters
{
/// <summary>
/// The text of the status
/// </summary>
public string Status { get; set; } = string.Empty;

/// <summary>
/// Either "direct", "private", "unlisted" or "public"
/// </summary>
public Visibility? Visibility { get; set; }

/// <summary>
/// Local ID of the status you want to reply to
/// </summary>
public string? ReplyStatusId { get; set; }

/// <summary>
/// Array of media IDs to attach to the status (maximum 4)
/// </summary>
public IEnumerable<string>? MediaIds { get; set; }

/// <summary>
/// Set this to mark the media of the status as NSFW
/// </summary>
public bool Sensitive { get; set; }

/// <summary>
/// Text to be shown as a warning before the actual content
/// </summary>
public string? SpoilerText { get; set; }

/// <summary>
/// DateTime to schedule posting of status
/// </summary>
public DateTime? ScheduledAt { get; set; }

/// <summary>
/// Override language code of the toot (ISO 639-2)
/// </summary>
public string? Language { get; set; }

/// <summary>
/// Nested parameters to attach a poll to the status
/// </summary>
public PollParameters? PollParameters { get; set; }
}
84 changes: 76 additions & 8 deletions Mastonet/MastodonClient.Status.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public Task<MastodonList<Account>> GetFavouritedBy(string statusId, ArrayOptions
/// <returns>Returns Status</returns>
public Task<Status> PublishStatus(string status, Visibility? visibility = null, string? replyStatusId = null,
IEnumerable<string>? 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()))
{
Expand Down Expand Up @@ -141,18 +141,86 @@ public Task<Status> PublishStatus(string status, Visibility? visibility = null,
data.Add(new KeyValuePair<string, string>("language", language));
}

if (poll != null)
if (pollParameters != null)
{
data.AddRange(poll.Options.Select(option => new KeyValuePair<string, string>("poll[options][]", option)));
data.Add(new KeyValuePair<string, string>("poll[expires_in]", poll.ExpiresIn.TotalSeconds.ToString()));
if (poll.Multiple.HasValue)
data.AddRange(pollParameters.Options.Select(option => new KeyValuePair<string, string>("poll[options][]", option)));
data.Add(new KeyValuePair<string, string>("poll[expires_in]", pollParameters.ExpiresIn.TotalSeconds.ToString()));
if (pollParameters.Multiple.HasValue)
{
data.Add(new KeyValuePair<string, string>("poll[multiple]", poll.Multiple.Value.ToString().ToLowerInvariant()));
data.Add(new KeyValuePair<string, string>("poll[multiple]", pollParameters.Multiple.Value.ToString().ToLowerInvariant()));
}

if (poll.HideTotals.HasValue)
if (pollParameters.HideTotals.HasValue)
{
data.Add(new KeyValuePair<string, string>("poll[hide_totals]", pollParameters.HideTotals.Value.ToString().ToLowerInvariant()));
}
}

return Post<Status>("/api/v1/statuses", data);
}

public Task<Status> 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<KeyValuePair<string, string>>()
{
new KeyValuePair<string, string>("status", statusParameters.Status),
};

if (!string.IsNullOrEmpty(statusParameters.ReplyStatusId))
{
data.Add(new KeyValuePair<string, string>("in_reply_to_id", statusParameters.ReplyStatusId!));
}

if (statusParameters.MediaIds != null)
{
foreach (var mediaId in statusParameters.MediaIds)
{
data.Add(new KeyValuePair<string, string>("media_ids[]", mediaId));
}
}

if (statusParameters.Sensitive)
{
data.Add(new KeyValuePair<string, string>("sensitive", "true"));
}

if (statusParameters.SpoilerText != null)
{
data.Add(new KeyValuePair<string, string>("spoiler_text", statusParameters.SpoilerText));
}

if (statusParameters.Visibility.HasValue)
{
data.Add(new KeyValuePair<string, string>("visibility", statusParameters.Visibility.Value.ToString().ToLowerInvariant()));
}

if (statusParameters.ScheduledAt.HasValue)
{
data.Add(new KeyValuePair<string, string>("scheduled_at", statusParameters.ScheduledAt.Value.ToString("o")));
}

if (statusParameters.Language != null)
{
data.Add(new KeyValuePair<string, string>("language", statusParameters.Language));
}

if (statusParameters.PollParameters != null)
{
data.AddRange(statusParameters.PollParameters.Options.Select(option => new KeyValuePair<string, string>("poll[options][]", option)));
data.Add(new KeyValuePair<string, string>("poll[expires_in]", statusParameters.PollParameters.ExpiresIn.TotalSeconds.ToString()));
if (statusParameters.PollParameters.Multiple.HasValue)
{
data.Add(new KeyValuePair<string, string>("poll[multiple]", statusParameters.PollParameters.Multiple.Value.ToString().ToLowerInvariant()));
}

if (statusParameters.PollParameters.HideTotals.HasValue)
{
data.Add(new KeyValuePair<string, string>("poll[hide_totals]", poll.HideTotals.Value.ToString().ToLowerInvariant()));
data.Add(new KeyValuePair<string, string>("poll[hide_totals]", statusParameters.PollParameters.HideTotals.Value.ToString().ToLowerInvariant()));
}
}

Expand Down

0 comments on commit f592485

Please sign in to comment.