Skip to content

Commit

Permalink
ResolvedUnfurledMediaItem + fix missing accessory on sections
Browse files Browse the repository at this point in the history
  • Loading branch information
Misha-133 committed Feb 18, 2025
1 parent 1f73870 commit 2d66598
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Discord;

public class ResolvedUnfurledMediaItem : UnfurledMediaItem
{
public string ProxyUrl { get; }

public int Height { get; }

public int Width { get; }

public string ContentType { get;}

public UnfurledMediaItemLoadingState LoadingState { get; }

internal ResolvedUnfurledMediaItem(string url, string proxyUrl, int height, int width, string contentType, UnfurledMediaItemLoadingState loadingState) : base(url)
{
ProxyUrl = proxyUrl;
Height = height;
Width = width;
ContentType = contentType;
LoadingState = loadingState;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Discord;

public readonly struct UnfurledMediaItem
public class UnfurledMediaItem
{
public string Url { get; }

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace Discord;

public enum UnfurledMediaItemLoadingState
{
/// <summary>
/// The state of the media item is unknown.
/// </summary>
Unknown = 0,

/// <summary>
/// The media item is currently loading.
/// </summary>
Loading = 1,

/// <summary>
/// The media item was successfully loaded.
/// </summary>
LoadingSuccess = 2,

/// <summary>
/// The media item was not found.
/// </summary>
LoadingNotFound = 3
}
3 changes: 2 additions & 1 deletion src/Discord.Net.Rest/API/Common/ThumbnailComponent.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Discord.Rest;
using Newtonsoft.Json;

namespace Discord.API;
Expand Down Expand Up @@ -25,7 +26,7 @@ public ThumbnailComponent(Discord.ThumbnailComponent component)
{
Type = component.Type;
Id = component.Id ?? Optional<int>.Unspecified;
Media = new UnfurledMediaItem { Url = component.Media.Url };
Media = component.Media.ToModel();
Description = component.Description;
IsSpoiler = component.IsSpoiler;
}
Expand Down
17 changes: 16 additions & 1 deletion src/Discord.Net.Rest/API/Common/UnfurledMediaItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,23 @@

namespace Discord.API;

public class UnfurledMediaItem
internal class UnfurledMediaItem
{
[JsonProperty("url")]
public string Url { get; set; }

[JsonProperty("proxy_url")]
public Optional<string> ProxyUrl { get; set; }

[JsonProperty("height")]
public Optional<int> Height { get; set; }

[JsonProperty("width")]
public Optional<int> Width { get; set; }

[JsonProperty("content_type")]
public Optional<string> ContentType { get; set; }

[JsonProperty("loading_state")]
public Optional<UnfurledMediaItemLoadingState> LoadingState { get; set; }
}
17 changes: 14 additions & 3 deletions src/Discord.Net.Rest/Extensions/MessageComponentExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,18 @@ internal static IMessageComponent ToEntity(this IMessageComponent component)
case ComponentType.Section:
{
var parsed = (API.SectionComponent)component;
return new SectionComponent(parsed.Id.ToNullable(), parsed.Components.Select(x => x.ToEntity()).ToImmutableArray(), parsed.Accessory.ToModel());
return new SectionComponent(parsed.Id.ToNullable(),
parsed.Components.Select(x => x.ToEntity()).ToImmutableArray(),
parsed.Accessory.ToEntity());
}

case ComponentType.Thumbnail:
{
var parsed = (API.ThumbnailComponent)component;
return new ThumbnailComponent(parsed.Id.ToNullable(), parsed.Media.ToEntity(), parsed.Description.GetValueOrDefault(null), parsed.IsSpoiler.ToNullable());
return new ThumbnailComponent(parsed.Id.ToNullable(),
parsed.Media.ToEntity(),
parsed.Description.GetValueOrDefault(null),
parsed.IsSpoiler.ToNullable());
}

case ComponentType.MediaGallery:
Expand Down Expand Up @@ -175,8 +180,14 @@ internal static IMessageComponent ToEntity(this IMessageComponent component)

internal static UnfurledMediaItem ToEntity(this API.UnfurledMediaItem mediaItem)
{
return new UnfurledMediaItem(mediaItem.Url);
return new ResolvedUnfurledMediaItem(mediaItem.Url,
mediaItem.ProxyUrl.Value,
mediaItem.Height.Value,
mediaItem.Width.Value,
mediaItem.ContentType.Value,
mediaItem.LoadingState.Value);
}

internal static API.UnfurledMediaItem ToModel(this UnfurledMediaItem mediaItem)
{
return new API.UnfurledMediaItem
Expand Down

0 comments on commit 2d66598

Please sign in to comment.