Skip to content

Commit

Permalink
feat(pin): allow adding and removing pinned objects
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Dec 19, 2016
1 parent 6d4ef53 commit 507f1f8
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
65 changes: 59 additions & 6 deletions src/PinnedCollection .cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,30 @@ public void Add(PinnedObject obj)
if (obj == null)
throw new ArgumentNullException();

throw new NotImplementedException();
var recursive = "recursive=" + (obj.Mode == PinMode.Recursive).ToString().ToLowerInvariant();
ipfs.DoCommand("pin/add", obj.Id, recursive);
pins = null;
}

/// <summary>
/// Pin an object.
/// </summary>
/// <param name="id">
/// The string representation of the object's <see cref="MultiHash"/>.
/// </param>
/// <param name="recursive">
/// True to also pin the object's links; False to just pin the object. Defaults to true.
/// </param>
/// <remarks>
/// Equivalent to <c>ipfs pin add <i>id</i></c>.
/// </remarks>
public void Add(string id, bool recursive = true)
{
Add(new PinnedObject
{
Id = id,
Mode = recursive ? PinMode.Recursive : PinMode.Direct
});
}

/// <summary>
Expand All @@ -68,6 +91,11 @@ public bool Contains(PinnedObject item)
return Pins.Contains(item);
}

public bool Contains(string id)
{
return Pins.Any(pin => pin.Id == id);
}

/// <inheritdoc />
public void CopyTo(PinnedObject[] array, int index)
{
Expand All @@ -90,17 +118,42 @@ public bool IsReadOnly
}

/// <summary>
/// Remove the trusted peer.
/// Remove the pinned object.
/// </summary>
/// <remarks>
/// Equivalent to <c>ipfs bootstrap rm <i>peer</i></c>.
/// Equivalent to <c>ipfs pin rm <i>id</i></c>.
/// </remarks>
public bool Remove(PinnedObject peer)
public bool Remove(PinnedObject obj)
{
if (peer == null)
if (obj == null)
throw new ArgumentNullException();

throw new NotImplementedException();
var recursive = "recursive=" + (obj.Mode == PinMode.Recursive).ToString().ToLowerInvariant();
ipfs.DoCommand("pin/rm", obj.Id, recursive);
pins = null;

return true;
}

/// <summary>
/// Unpin an object.
/// </summary>
/// <param name="id">
/// The string representation of the object's <see cref="MultiHash"/>.
/// </param>
/// <param name="recursive">
/// True to also unpin the object's links; False to just unpin the object. Defaults to true.
/// </param>
/// <remarks>
/// Equivalent to <c>ipfs pin rm <i>id</i></c>.
/// </remarks>
public bool Remove(string id, bool recursive = true)
{
return Remove(new PinnedObject
{
Id = id,
Mode = recursive ? PinMode.Recursive : PinMode.Direct
});
}

/// <inheritdoc />
Expand Down
15 changes: 15 additions & 0 deletions test/PinTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,20 @@ public void Pin_List()
Assert.IsTrue(ipfs.PinnedObjects.Count > 0);
}

[TestMethod]
public void Pin_Add_Remove()
{
var ipfs = new IpfsClient();
var result = ipfs.AddTextAsync("I am pinned").Result;
var id = result.Hash;

ipfs.PinnedObjects.Add(id);
Assert.IsTrue(ipfs.PinnedObjects.Contains(id));

ipfs.PinnedObjects.Remove(id);
Assert.IsFalse(ipfs.PinnedObjects.Contains(id));
}


}
}

0 comments on commit 507f1f8

Please sign in to comment.