Skip to content

Commit

Permalink
Add replies to Post from CBOR
Browse files Browse the repository at this point in the history
  • Loading branch information
drasticactions committed Feb 14, 2024
1 parent e3ee374 commit 59cbff8
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 1 deletion.
6 changes: 6 additions & 0 deletions samples/FishyFlip.Firehose/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ async Task HandleMessageAsync(SubscribeRepoMessage message)
// The path contains the post action and path, we need the path, so we split to get it.
var url = $"https://bsky.app/profile/{did}/post/{message.Commit.Ops![0]!.Path!.Split("/").Last()}";
Console.WriteLine($"Post URL: {url}, from {repo.Handle}");

if (post.Reply is not null)
{
Console.WriteLine($"Reply Root: {post.Reply.Root.Uri}");
Console.WriteLine($"Reply Parent: {post.Reply.Parent.Uri}");
}
}
}
}
2 changes: 1 addition & 1 deletion src/FishyFlip/ATWebSocketProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ void HandleProgressStatus(CarProgressStatusEvent e)
var blockObj = CBORObject.Read(blockStream);
if (blockObj["$type"] is not null)
{
message.Record = ATRecord.FromCBORObject(blockObj);
message.Record = ATRecord.FromCBORObject(blockObj, this.logger);
}
else if (blockObj["sig"] is not null)
{
Expand Down
6 changes: 6 additions & 0 deletions src/FishyFlip/Models/ATRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public ATRecord()
/// <returns>An AT Record if the conversion is successful; otherwise, null.</returns>
public static ATRecord? FromCBORObject(CBORObject blockObj, ILogger? logger = default)
{
#if DEBUG
var rawObj = blockObj.ToJSONString();
logger?.LogDebug($"Raw Object: {rawObj}");
#endif

if (blockObj["$type"] is not null)
{
switch (blockObj["$type"].AsString())
Expand All @@ -63,6 +68,7 @@ public ATRecord()
case Constants.ActorTypes.Profile:
return new Profile(blockObj);
default:
logger?.LogDebug($"Unknown type: {blockObj["$type"].AsString()}");
return null;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/FishyFlip/Models/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public Post(CBORObject obj)
this.Text = obj["text"].AsString();
this.Langs = obj["langs"]?.Values.Select(n => n.AsString()).ToArray();
this.Facets = obj["facets"]?.Values.Select(n => new Facet(n)).ToArray();
this.Reply = obj["reply"]?.IsNull ?? false ? null : obj["reply"].ToReply();
}

/// <summary>
Expand Down
24 changes: 24 additions & 0 deletions src/FishyFlip/Tools/Cbor/CborExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,28 @@ public static Embed ToEmbed(this CBORObject obj)

return new UnknownEmbed(type);
}

/// <summary>
/// Cast CBOR to Reply.
/// </summary>
/// <param name="obj">CBORObject.</param>
/// <returns>Reply.</returns>
public static Reply? ToReply(this CBORObject? obj)
{
if (obj is null)
{
return null;
}

var root = obj["root"];
var parent = obj["parent"];
if (root.IsNull || parent.IsNull)
{
return null;
}

var rootRef = new ReplyRef(root["cid"].ToCid()!, ATUri.Create(root["uri"].AsString()));
var parentRef = new ReplyRef(parent["cid"].ToCid()!, ATUri.Create(parent["uri"].AsString()));
return new Reply(rootRef, parentRef);
}
}

0 comments on commit 59cbff8

Please sign in to comment.