Skip to content

Commit

Permalink
Create ListItem
Browse files Browse the repository at this point in the history
  • Loading branch information
drasticactions committed Jan 15, 2024
1 parent 3c8d033 commit 13e589c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 26 deletions.
37 changes: 25 additions & 12 deletions src/FishyFlip.Tests/AuthorizedTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,8 +334,24 @@ public async Task CreateAndRemoveListTest()
var createList = (await this.proto.Repo.CreateCurateListAsync(randomName, "Test List", DateTime.UtcNow)).HandleResult();
Assert.True(createList!.Cid is not null);
Assert.True(createList!.Uri is not null);
var repo = this.proto.SessionManager!.Session!.Did;
var removeList = (await this.proto.Repo.DeleteListAsync(repo, createList.Uri.Rkey)).HandleResult();
var removeList = (await this.proto.Repo.DeleteListAsync(createList.Uri.Rkey)).HandleResult();
Assert.True(removeList is not null);
}

[Fact]
public async Task CreateAndRemoveListItemTest()
{
var randomName = Guid.NewGuid().ToString();
var createList = (await this.proto.Repo.CreateCurateListAsync(randomName, "Test List", DateTime.UtcNow)).HandleResult();
Assert.True(createList!.Cid is not null);
Assert.True(createList!.Uri is not null);
var follow1 = ATDid.Create("did:plc:up76ybimufzledmmhbv25wse");
var follow = (await this.proto.Repo.CreateListItemAsync(follow1, createList.Uri)).HandleResult();
Assert.True(follow!.Cid is not null);
Assert.True(follow!.Uri is not null);
var removeListItem = (await this.proto.Repo.DeleteListItemAsync(follow.Uri.Rkey)).HandleResult();
Assert.True(removeListItem is not null);
var removeList = (await this.proto.Repo.DeleteListAsync(createList.Uri.Rkey)).HandleResult();
Assert.True(removeList is not null);
}

Expand All @@ -347,7 +363,7 @@ public async Task CreateAndRemovePostTest()
Assert.True(create!.Cid is not null);
Assert.True(create!.Uri is not null);
var repo = this.proto.SessionManager!.Session!.Did;
var remove = (await this.proto.Repo.DeletePostAsync(repo, create.Uri.Rkey)).HandleResult();
var remove = (await this.proto.Repo.DeletePostAsync(create.Uri.Rkey)).HandleResult();
Assert.True(remove is not null);
}

Expand All @@ -364,10 +380,10 @@ public async Task CreateAndRemoveRepostTest()
Assert.True(repost!.Uri is not null);

var repo = this.proto.SessionManager!.Session!.Did;
var removeRepost = (await this.proto.Repo.DeleteRepostAsync(repo, repost.Uri.Rkey)).HandleResult();
var removeRepost = (await this.proto.Repo.DeleteRepostAsync(repost.Uri.Rkey)).HandleResult();
Assert.True(removeRepost is not null);

var remove = (await this.proto.Repo.DeletePostAsync(repo, create.Uri.Rkey)).HandleResult();
var remove = (await this.proto.Repo.DeletePostAsync(create.Uri.Rkey)).HandleResult();
Assert.True(remove is not null);
}

Expand All @@ -383,11 +399,10 @@ public async Task CreateAndRemoveLikeTest()
Assert.True(like!.Cid is not null);
Assert.True(like!.Uri is not null);

var repo = this.proto.SessionManager!.Session!.Did;
var removeLike = (await this.proto.Repo.DeleteLikeAsync(repo, like.Uri.Rkey)).HandleResult();
var removeLike = (await this.proto.Repo.DeleteLikeAsync(like.Uri.Rkey)).HandleResult();
Assert.True(removeLike is not null);

var remove = (await this.proto.Repo.DeletePostAsync(repo, create.Uri.Rkey)).HandleResult();
var remove = (await this.proto.Repo.DeletePostAsync(create.Uri.Rkey)).HandleResult();
Assert.True(remove is not null);
}

Expand All @@ -399,8 +414,7 @@ public async Task CreateAndRemoveFollowTest()
Assert.True(follow!.Cid is not null);
Assert.True(follow!.Uri is not null);

var repo = this.proto.SessionManager!.Session!.Did;
var remove = (await this.proto.Repo.DeleteFollowAsync(repo, follow.Uri.Rkey)).HandleResult();
var remove = (await this.proto.Repo.DeleteFollowAsync(follow.Uri.Rkey)).HandleResult();
Assert.True(remove is not null);
}

Expand All @@ -412,8 +426,7 @@ public async Task CreateAndRemoveBlockTest()
Assert.True(follow!.Cid is not null);
Assert.True(follow!.Uri is not null);

var repo = this.proto.SessionManager!.Session!.Did;
var remove = (await this.proto.Repo.DeleteBlockAsync(repo, follow.Uri.Rkey)).HandleResult();
var remove = (await this.proto.Repo.DeleteBlockAsync(follow.Uri.Rkey)).HandleResult();
Assert.True(remove is not null);
}

Expand Down
43 changes: 29 additions & 14 deletions src/FishyFlip/ATProtoRepo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ public Task<Result<RecordRef>> CreateFollowAsync(
return this.CreateRecord<CreateFollowRecord, RecordRef>(record, cancellationToken);
}

public Task<Result<RecordRef>> CreateListItemAsync(
ATDid subject,
ATUri list,
DateTime? createdAt = null,
CancellationToken cancellationToken = default)
{
CreateListItemRecord record = new(
Constants.GraphTypes.ListItem,
this.proto.SessionManager!.Session!.Did.ToString()!,
new ListItemRecord(subject, list, createdAt ?? DateTime.UtcNow));

return this.CreateRecord<CreateListItemRecord, RecordRef>(record, cancellationToken);
}

public Task<Result<CreatePostResponse>> CreatePostAsync(
string text,
Facet[]? facets = null,
Expand Down Expand Up @@ -166,52 +180,53 @@ public Task<Result<UploadBlobResponse>> UploadBlobAsync(StreamContent content, C
}

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);
=> this.DeleteRecordAsync(Constants.GraphTypes.Follow, rkey, swapRecord, swapCommit, cancellationToken);

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);
=> this.DeleteRecordAsync(Constants.GraphTypes.Block, rkey, swapRecord, swapCommit, cancellationToken);

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);
=> this.DeleteRecordAsync(Constants.FeedType.Like, rkey, swapRecord, swapCommit, cancellationToken);

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);
=> this.DeleteRecordAsync(Constants.FeedType.Post, rkey, swapRecord, swapCommit, cancellationToken);

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

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

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

[Obsolete("Use ListFollowsAsync instead")]
public Task<Result<ListRecords?>> ListFollowAsync(ATIdentifier repo, int limit = 50, string? cursor = default, bool? reverse = default, CancellationToken cancellationToken = default)
Expand Down Expand Up @@ -281,11 +296,11 @@ private Task<Result<T2>> CreateRecord<T, T2>(T record, CancellationToken cancell
this.Options.Logger);
}

private async Task<Result<Success>> DeleteRecordAsync(string collection, ATIdentifier repo, string rkey, Cid? swapRecord = null, Cid? swapCommit = null, CancellationToken cancellationToken = default)
private async Task<Result<Success>> DeleteRecordAsync(string collection, string rkey, Cid? swapRecord = null, Cid? swapCommit = null, CancellationToken cancellationToken = default)
{
DeleteRecord record = new(
collection,
repo.ToString()!,
this.proto.SessionManager!.Session!.Did.ToString()!,
rkey,
swapRecord,
swapCommit);
Expand Down
7 changes: 7 additions & 0 deletions src/FishyFlip/Models/Internal/CreateListItemRecord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// <copyright file="CreateListItemRecord.cs" company="Drastic Actions">
// Copyright (c) Drastic Actions. All rights reserved.
// </copyright>

namespace FishyFlip.Models.Internal;

public record CreateListItemRecord(string Collection, string Repo, ListItemRecord Record);
23 changes: 23 additions & 0 deletions src/FishyFlip/Models/ListItemRecord.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// <copyright file="ListItemRecord.cs" company="Drastic Actions">
// Copyright (c) Drastic Actions. All rights reserved.
// </copyright>

using FishyFlip;

public class ListItemRecord : ATRecord
{
[JsonConstructor]
public ListItemRecord(ATDid subject, ATUri list, DateTime? createdAt)
{
this.Subject = subject;
this.List = list;
this.CreatedAt = createdAt ?? DateTime.Now;
this.Type = Constants.GraphTypes.ListItem;
}

public ATDid Subject { get; }

public ATUri List { get; }

public DateTime CreatedAt { get; }
}

0 comments on commit 13e589c

Please sign in to comment.