Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
drasticactions committed Jan 15, 2024
1 parent 010df34 commit 303a0da
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 41 deletions.
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
<ItemGroup>
<PackageVersion Include="CommunityToolkit.Mvvm" Version="8.2.0" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageVersion Include="StyleCop.Analyzers" Version="1.2.0-beta.435" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.1" />
<PackageVersion Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
<PackageVersion Include="Sharprompt" Version="2.4.5" />
<PackageVersion Include="System.IdentityModel.Tokens.Jwt" Version="7.2.0" />
<PackageVersion Include="MimeTypes" Version="2.4.1" />
Expand Down
3 changes: 1 addition & 2 deletions src/FishyFlip.Tests/AnonymousTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public async Task GetBlocksAsyncTest()
var postCid1 = Cid.Decode("bafyreibby2anauk6ef2ntmeyebeb3yosncathvohhjrb7jmxfpyljyeq2e");
var postCid2 = Cid.Decode("bafyreiausj2iabpfs2mbmp2qtaszd2jokmsogto7z6zrz3pkncx3emyx4m");

var oncardecoded = new OnCarDecoded((e) => {
var oncardecoded = new OnCarDecoded((e) => {
});

var result = await this.proto.Sync.GetBlocksAsync(atDid, new[] { postCid1, postCid2 }, oncardecoded);
Expand Down
92 changes: 60 additions & 32 deletions src/FishyFlip/ATProtoRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,16 @@ public Task<Result<RecordRef>> CreateBlockAsync(
return
this.Client
.Post<CreateBlockRecord, RecordRef>(
Constants.Urls.ATProtoRepo.CreateRecord, this.Options.JsonSerializerOptions, record,
cancellationToken, this.Options.Logger);
Constants.Urls.ATProtoRepo.CreateRecord,
this.Options.JsonSerializerOptions,
record,
cancellationToken,
this.Options.Logger);
}

public async Task<Result<PostRecord?>> GetPostAsync(ATIdentifier repo, string rkey, Cid? cid = null, CancellationToken cancellationToken = default)
=> await this.GetRecordAsync<PostRecord>(Constants.FeedType.Post, repo, rkey, cid, cancellationToken);

public async Task<Result<ActorRecord?>> GetActorAsync(ATIdentifier repo, Cid? cid = null, CancellationToken cancellationToken = default)
=> await this.GetRecordAsync<ActorRecord>(Constants.ActorTypes.Profile, repo, "self", cid, cancellationToken);

Expand All @@ -112,8 +115,11 @@ public Task<Result<UploadBlobResponse>> UploadBlobAsync(StreamContent content, C
return
this.Client
.Post<UploadBlobResponse>(
Constants.Urls.ATProtoRepo.UploadBlob, this.Options.JsonSerializerOptions, content,
cancellationToken, this.Options.Logger);
Constants.Urls.ATProtoRepo.UploadBlob,
this.Options.JsonSerializerOptions,
content,
cancellationToken,
this.Options.Logger);
}

public async Task<Result<T?>> GetRecordAsync<T>(string collection, ATIdentifier repo, string rkey, Cid? cid = null, CancellationToken cancellationToken = default)
Expand All @@ -128,44 +134,42 @@ public Task<Result<UploadBlobResponse>> UploadBlobAsync(StreamContent content, C
return await this.Client.Get<T>(url, this.Options.JsonSerializerOptions, cancellationToken, this.Options.Logger);
}

public async Task<Result<DescribeRepo>> DescribeRepoAsync(ATIdentifier identifier, CancellationToken cancellationToken = default)
public async Task<Result<DescribeRepo?>> DescribeRepoAsync(ATIdentifier identifier, CancellationToken cancellationToken = default)
{
string url = $"{Constants.Urls.ATProtoRepo.DescribeRepo}?repo={identifier}";
return await this.Client.Get<DescribeRepo?>(url, this.Options.JsonSerializerOptions, cancellationToken, this.Options.Logger);
}

private Task<Result<Y>> CreateRecord<X, Y>(X record, CancellationToken cancellationToken = default)
{
return
this.Client
.Post<X, Y>(
Constants.Urls.ATProtoRepo.CreateRecord, this.Options.JsonSerializerOptions, record,
cancellationToken, this.Options.Logger);
}

private Task<Result<Y>> PutRecord<X, Y>(X record, CancellationToken cancellationToken = default)
{
return
this.Client
.Post<X, Y>(
Constants.Urls.ATProtoRepo.PutRecord, this.Options.JsonSerializerOptions, record,
cancellationToken, this.Options.Logger);
}

public Task<Result<Success>> DeleteFollowAsync(ATIdentifier repo, string rkey,
Cid? swapRecord = null, Cid? swapCommit = null, CancellationToken cancellationToken = default)
public Task<Result<Success>> DeleteFollowAsync(
ATIdentifier repo,
string rkey,
Cid? swapRecord = null,
Cid? swapCommit = null,
CancellationToken cancellationToken = default)
=> this.DeleteRecordAsync(Constants.GraphTypes.Follow, repo, rkey, swapRecord, swapCommit, cancellationToken);

public Task<Result<Success>> DeleteBlockAsync(ATIdentifier repo, string rkey,
Cid? swapRecord = null, Cid? swapCommit = null, CancellationToken cancellationToken = default)
public Task<Result<Success>> DeleteBlockAsync(
ATIdentifier repo,
string rkey,
Cid? swapRecord = null,
Cid? swapCommit = null,
CancellationToken cancellationToken = default)
=> this.DeleteRecordAsync(Constants.GraphTypes.Block, repo, rkey, swapRecord, swapCommit, cancellationToken);

public Task<Result<Success>> DeleteLikeAsync(ATIdentifier repo, string rkey,
Cid? swapRecord = null, Cid? swapCommit = null, CancellationToken cancellationToken = default)
public Task<Result<Success>> DeleteLikeAsync(
ATIdentifier repo,
string rkey,
Cid? swapRecord = null,
Cid? swapCommit = null,
CancellationToken cancellationToken = default)
=> this.DeleteRecordAsync(Constants.FeedType.Like, repo, rkey, swapRecord, swapCommit, cancellationToken);

public Task<Result<Success>> DeletePostAsync(ATIdentifier repo, string rkey,
Cid? swapRecord = null, Cid? swapCommit = null, CancellationToken cancellationToken = default)
public Task<Result<Success>> DeletePostAsync(
ATIdentifier repo,
string rkey,
Cid? swapRecord = null,
Cid? swapCommit = null,
CancellationToken cancellationToken = default)
=> this.DeleteRecordAsync(Constants.FeedType.Post, repo, rkey, swapRecord, swapCommit, cancellationToken);

public async Task<Result<Success>> DeleteRecordAsync(string collection, ATIdentifier repo, string rkey, Cid? swapRecord = null, Cid? swapCommit = null, CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -222,4 +226,28 @@ public async Task<Result<Success>> DeleteRecordAsync(string collection, ATIdenti

return await this.Client.Get<ListRecords>(url, this.Options.JsonSerializerOptions, cancellationToken, this.Options.Logger);
}

private Task<Result<T2>> PutRecord<T, T2>(T record, CancellationToken cancellationToken = default)
{
return
this.Client
.Post<T, T2>(
Constants.Urls.ATProtoRepo.PutRecord,
this.Options.JsonSerializerOptions,
record,
cancellationToken,
this.Options.Logger);
}

private Task<Result<T2>> CreateRecord<T, T2>(T record, CancellationToken cancellationToken = default)
{
return
this.Client
.Post<T, T2>(
Constants.Urls.ATProtoRepo.CreateRecord,
this.Options.JsonSerializerOptions,
record,
cancellationToken,
this.Options.Logger);
}
}
3 changes: 1 addition & 2 deletions src/FishyFlip/ATWebSocketProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public sealed class ATWebSocketProtocol : IDisposable
{
private const int ReceiveBufferSize = 32768;
private ClientWebSocket client;
private CancellationToken? token;
private bool disposedValue;
private ILogger? logger;
private Uri instanceUri;
Expand All @@ -20,7 +19,7 @@ public sealed class ATWebSocketProtocol : IDisposable
/// <summary>
/// Initializes a new instance of the <see cref="ATWebSocketProtocol"/> class.
/// </summary>
/// <param name="options"><see cref="ATProtocolOptions"/>.</param>
/// <param name="protocol"><see cref="ATProtocolOptions"/>.</param>
internal ATWebSocketProtocol(ATProtocol protocol)
{
this.protocol = protocol;
Expand Down
35 changes: 32 additions & 3 deletions src/FishyFlip/Models/AtDid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@

namespace FishyFlip.Models;

/// <summary>
/// ATDid.
/// </summary>
public class ATDid : ATIdentifier
{
/// <summary>
/// Initializes a new instance of the <see cref="ATDid"/> class.
/// </summary>
/// <param name="handler">Actor Handler.</param>
[JsonConstructor]
protected ATDid(string Handler)
protected ATDid(string handler)
{
this.Handler = Handler;
this.Handler = handler;
}

/// <summary>
/// Gets the Actor Handler.
/// </summary>
public string Handler { get; }

public static ATDid? Create(string uri)
/// <summary>
/// Create a new ATDid.
/// </summary>
/// <param name="uri">Uri.</param>
/// <returns>ATDid.</returns>
/// <exception cref="ArgumentNullException">Thrown if URI is null or empty.</exception>
public static new ATDid? Create(string uri)
{
if (string.IsNullOrEmpty(uri))
{
Expand All @@ -30,6 +46,12 @@ protected ATDid(string Handler)
return new ATDid(uri);
}

/// <summary>
/// Validate if a given string is a valid ATDid.
/// </summary>
/// <param name="uri">Uri.</param>
/// <returns>Bool.</returns>
/// <exception cref="ArgumentNullException">Thrown if uri is null or empty.</exception>
public static bool IsValid(string uri)
{
if (string.IsNullOrEmpty(uri))
Expand All @@ -40,11 +62,18 @@ public static bool IsValid(string uri)
return DIDValidator.EnsureValidDid(uri);
}

/// <inheritdoc/>
public override string ToString()
{
return this.Handler.ToString();
}

/// <summary>
/// Create a new ATDid.
/// </summary>
/// <param name="uri">ATUri.</param>
/// <returns>ATDid.</returns>
/// <exception cref="ArgumentNullException">Thrown if uri is null or empty.</exception>
internal static ATDid? Create(ATUri uri)
{
if (uri == null)
Expand Down

0 comments on commit 303a0da

Please sign in to comment.