Skip to content

Commit

Permalink
Add RecordWithMediaViewEmbed.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
drasticactions committed Oct 21, 2023
1 parent f45c5d5 commit 765c6e6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/FishyFlip/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ public static class EmbedTypes
public const string RecordView = "app.bsky.embed.record#view";
public const string Record = "app.bsky.embed.record";
public const string RecordWithMedia = "app.bsky.embed.recordWithMedia";
public const string RecordWithMediaView = "app.bsky.embed.recordWithMedia#view";
}

public static class FeedType
Expand Down
25 changes: 25 additions & 0 deletions src/FishyFlip/Models/RecordWithMediaViewEmbed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FishyFlip.Models;

/// <summary>
/// Record with Media Embed.
/// </summary>
public class RecordWithMediaViewEmbed : Embed
{
[JsonConstructor]
public RecordWithMediaViewEmbed(RecordViewEmbed? record, ImageViewEmbed? images)
{
this.Record = record;
this.Images = images;
this.Type = Constants.EmbedTypes.RecordWithMedia;
}

public RecordViewEmbed? Record { get; }

public ImageViewEmbed? Images { get; }
}
24 changes: 24 additions & 0 deletions src/FishyFlip/Tools/Json/EmbedConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,30 @@ public class EmbedConverter : JsonConverter<Embed>
}

break;
case Constants.EmbedTypes.RecordWithMediaView:
RecordViewEmbed? record1 = null;
ImageViewEmbed? media1 = null;

if (doc.RootElement.TryGetProperty("record", out var recordVal2))
{
record1 = JsonSerializer.Deserialize<RecordViewEmbed>(recordVal2.GetRawText(), options);
}

if (doc.RootElement.TryGetProperty("media", out var mediaVal2))
{
if (mediaVal2.TryGetProperty("$type", out var mediaType2))
{
var mediaText = mediaType2.GetString()?.Trim() ?? string.Empty;
switch (mediaText)
{
case Constants.EmbedTypes.ImageView:
media1 = JsonSerializer.Deserialize<ImageViewEmbed>(mediaVal2.GetRawText(), options);
break;
}
}
}

return new RecordWithMediaViewEmbed(record1, media1);
case Constants.EmbedTypes.RecordWithMedia:
RecordEmbed? record = null;
ImagesEmbed? media = null;
Expand Down

0 comments on commit 765c6e6

Please sign in to comment.