Skip to content

Commit 507f1f8

Browse files
feat(pin): allow adding and removing pinned objects
1 parent 6d4ef53 commit 507f1f8

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

src/PinnedCollection .cs

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,30 @@ public void Add(PinnedObject obj)
4848
if (obj == null)
4949
throw new ArgumentNullException();
5050

51-
throw new NotImplementedException();
51+
var recursive = "recursive=" + (obj.Mode == PinMode.Recursive).ToString().ToLowerInvariant();
52+
ipfs.DoCommand("pin/add", obj.Id, recursive);
53+
pins = null;
54+
}
55+
56+
/// <summary>
57+
/// Pin an object.
58+
/// </summary>
59+
/// <param name="id">
60+
/// The string representation of the object's <see cref="MultiHash"/>.
61+
/// </param>
62+
/// <param name="recursive">
63+
/// True to also pin the object's links; False to just pin the object. Defaults to true.
64+
/// </param>
65+
/// <remarks>
66+
/// Equivalent to <c>ipfs pin add <i>id</i></c>.
67+
/// </remarks>
68+
public void Add(string id, bool recursive = true)
69+
{
70+
Add(new PinnedObject
71+
{
72+
Id = id,
73+
Mode = recursive ? PinMode.Recursive : PinMode.Direct
74+
});
5275
}
5376

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

94+
public bool Contains(string id)
95+
{
96+
return Pins.Any(pin => pin.Id == id);
97+
}
98+
7199
/// <inheritdoc />
72100
public void CopyTo(PinnedObject[] array, int index)
73101
{
@@ -90,17 +118,42 @@ public bool IsReadOnly
90118
}
91119

92120
/// <summary>
93-
/// Remove the trusted peer.
121+
/// Remove the pinned object.
94122
/// </summary>
95123
/// <remarks>
96-
/// Equivalent to <c>ipfs bootstrap rm <i>peer</i></c>.
124+
/// Equivalent to <c>ipfs pin rm <i>id</i></c>.
97125
/// </remarks>
98-
public bool Remove(PinnedObject peer)
126+
public bool Remove(PinnedObject obj)
99127
{
100-
if (peer == null)
128+
if (obj == null)
101129
throw new ArgumentNullException();
102130

103-
throw new NotImplementedException();
131+
var recursive = "recursive=" + (obj.Mode == PinMode.Recursive).ToString().ToLowerInvariant();
132+
ipfs.DoCommand("pin/rm", obj.Id, recursive);
133+
pins = null;
134+
135+
return true;
136+
}
137+
138+
/// <summary>
139+
/// Unpin an object.
140+
/// </summary>
141+
/// <param name="id">
142+
/// The string representation of the object's <see cref="MultiHash"/>.
143+
/// </param>
144+
/// <param name="recursive">
145+
/// True to also unpin the object's links; False to just unpin the object. Defaults to true.
146+
/// </param>
147+
/// <remarks>
148+
/// Equivalent to <c>ipfs pin rm <i>id</i></c>.
149+
/// </remarks>
150+
public bool Remove(string id, bool recursive = true)
151+
{
152+
return Remove(new PinnedObject
153+
{
154+
Id = id,
155+
Mode = recursive ? PinMode.Recursive : PinMode.Direct
156+
});
104157
}
105158

106159
/// <inheritdoc />

test/PinTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,20 @@ public void Pin_List()
1717
Assert.IsTrue(ipfs.PinnedObjects.Count > 0);
1818
}
1919

20+
[TestMethod]
21+
public void Pin_Add_Remove()
22+
{
23+
var ipfs = new IpfsClient();
24+
var result = ipfs.AddTextAsync("I am pinned").Result;
25+
var id = result.Hash;
26+
27+
ipfs.PinnedObjects.Add(id);
28+
Assert.IsTrue(ipfs.PinnedObjects.Contains(id));
29+
30+
ipfs.PinnedObjects.Remove(id);
31+
Assert.IsFalse(ipfs.PinnedObjects.Contains(id));
32+
}
33+
34+
2035
}
2136
}

0 commit comments

Comments
 (0)