Skip to content

Commit 61435a1

Browse files
committed
update to targets
1 parent e6d0bb1 commit 61435a1

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

src/stream-net-tests/IntegrationTests.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,6 +1746,72 @@ public async Task TestActivityPartialUpdateByForeignIDTime()
17461746
Assert.AreEqual("zyx", updatedAct.GetData<string>("custom_thing"));
17471747
}
17481748

1749+
[Test]
1750+
public async Task TestUpdateToTargets()
1751+
{
1752+
var fidTime = new Stream.ForeignIDTime(System.Guid.NewGuid().ToString(), DateTime.UtcNow);
1753+
1754+
var targets = new List<string>()
1755+
{
1756+
"flat:" + Guid.NewGuid().ToString(),
1757+
"user:" + Guid.NewGuid().ToString(),
1758+
};
1759+
1760+
var act = new Stream.Activity("upd", "test", "1")
1761+
{
1762+
ForeignId = fidTime.ForeignID,
1763+
Time = fidTime.Time,
1764+
To = targets
1765+
};
1766+
1767+
var insertedAct = await this._user1.AddActivity(act);
1768+
Assert.AreEqual(2, insertedAct.To.Count);
1769+
1770+
//add 1
1771+
var add = "user:" + Guid.NewGuid().ToString();
1772+
var updateResp = await this._user1.UpdateActivityToTargets(fidTime, new string[] { add });
1773+
Assert.AreEqual(insertedAct.Id, updateResp.Activity.Id);
1774+
Assert.AreEqual(1, updateResp.Added.Count);
1775+
Assert.AreEqual(add, updateResp.Added[0]);
1776+
Assert.AreEqual(3, updateResp.Activity.To.Count);
1777+
Assert.IsNotNull(updateResp.Activity.To.ToList().Find(t => t == add));
1778+
1779+
var updatedAct = (await this._user1.GetActivities(0, 1, FeedFilter.Where().IdLessThanEqual(insertedAct.Id))).FirstOrDefault();
1780+
Assert.NotNull(updatedAct);
1781+
Assert.AreEqual(3, updatedAct.To.Count);
1782+
Assert.IsNotNull(updatedAct.To.ToList().Find(t => t == add));
1783+
1784+
//remove 1
1785+
var remove = targets[0];
1786+
updateResp = await this._user1.UpdateActivityToTargets(fidTime, null, null, new string[] { remove });
1787+
Assert.AreEqual(insertedAct.Id, updateResp.Activity.Id);
1788+
Assert.AreEqual(1, updateResp.Removed.Count);
1789+
Assert.AreEqual(remove, updateResp.Removed[0]);
1790+
Assert.AreEqual(2, updateResp.Activity.To.Count);
1791+
Assert.IsNull(updateResp.Activity.To.ToList().Find(t => t == remove));
1792+
1793+
updatedAct = (await this._user1.GetActivities(0, 1, FeedFilter.Where().IdLessThanEqual(insertedAct.Id))).FirstOrDefault();
1794+
Assert.NotNull(updatedAct);
1795+
Assert.AreEqual(2, updatedAct.To.Count);
1796+
Assert.IsNull(updatedAct.To.ToList().Find(t => t == remove));
1797+
1798+
//new ones
1799+
var newOnes = new List<string>()
1800+
{
1801+
"flat:" + Guid.NewGuid().ToString(),
1802+
"user:" + Guid.NewGuid().ToString(),
1803+
};
1804+
updateResp = await this._user1.UpdateActivityToTargets(fidTime, null, newOnes);
1805+
Assert.AreEqual(insertedAct.Id, updateResp.Activity.Id);
1806+
Assert.AreEqual(2, updateResp.Activity.To.Count);
1807+
Assert.AreEqual(2, updateResp.Added.Count);
1808+
Assert.AreEqual(2, updateResp.Added.ToList().FindAll(t => newOnes.Contains(t)).Count);
1809+
updatedAct = (await this._user1.GetActivities(0, 1, FeedFilter.Where().IdLessThanEqual(insertedAct.Id))).FirstOrDefault();
1810+
Assert.NotNull(updatedAct);
1811+
Assert.AreEqual(2, updatedAct.To.Count);
1812+
Assert.AreEqual(2, updatedAct.To.ToList().FindAll(t => newOnes.Contains(t)).Count);
1813+
}
1814+
17491815
[Test]
17501816
public async Task TestBatchPartialUpdate()
17511817
{

src/stream-net/IStreamFeed.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,9 @@ public interface IStreamFeed
2828
Task UnfollowFeed(string targetFeedSlug, string targetUserId, bool keepHistory = false);
2929
Task UpdateActivities(IEnumerable<Activity> activities);
3030
Task UpdateActivity(Activity activity);
31+
Task<UpdateToTargetsResponse> UpdateActivityToTargets(ForeignIDTime foreignIDTime,
32+
IEnumerable<string> adds = null,
33+
IEnumerable<string> newTargets = null,
34+
IEnumerable<string> removed = null);
3135
}
3236
}

src/stream-net/StreamFeed.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,34 @@ public async Task<IEnumerable<Activity>> GetActivities(int offset = 0, int limit
179179
throw StreamException.FromResponse(response);
180180
}
181181

182+
public async Task<UpdateToTargetsResponse> UpdateActivityToTargets(ForeignIDTime foreignIDTime,
183+
IEnumerable<string> adds = null,
184+
IEnumerable<string> newTargets = null,
185+
IEnumerable<string> removed = null)
186+
{
187+
var payload = new Dictionary<string, object>()
188+
{
189+
{"foreign_id" , foreignIDTime.ForeignID},
190+
{"time" , foreignIDTime.Time.ToString("s", System.Globalization.CultureInfo.InvariantCulture)}
191+
};
192+
if (adds != null)
193+
payload["added_targets"] = adds.ToList();
194+
if (newTargets != null)
195+
payload["new_targets"] = newTargets.ToList();
196+
if (removed != null)
197+
payload["removed_targets"] = removed.ToList();
198+
199+
var endpoint = string.Format("feed_targets/{0}/{1}/activity_to_targets/", this._feedSlug, this._userId);
200+
var request = this._client.BuildJWTAppRequest(endpoint, HttpMethod.POST);
201+
request.SetJsonBody(JsonConvert.SerializeObject(payload));
202+
var response = await this._client.MakeRequest(request);
203+
204+
if (response.StatusCode == System.Net.HttpStatusCode.Created)
205+
return UpdateToTargetsResponse.FromJson(response.Content);
206+
207+
throw StreamException.FromResponse(response);
208+
}
209+
182210
internal async Task<StreamResponse<T>> GetWithOptions<T>(GetOptions options = null) where T : Activity
183211
{
184212
// build request
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using Newtonsoft.Json;
4+
using Newtonsoft.Json.Linq;
5+
6+
namespace Stream
7+
{
8+
public class UpdateToTargetsResponse
9+
{
10+
public Activity Activity { get; internal set; }
11+
public List<string> Added { get; internal set; }
12+
public List<string> Removed { get; internal set; }
13+
14+
public UpdateToTargetsResponse() { }
15+
16+
internal static UpdateToTargetsResponse FromJson(string json)
17+
{
18+
return UpdateToTargetsResponse.FromJson(JObject.Parse(json));
19+
}
20+
21+
internal static UpdateToTargetsResponse FromJson(JObject obj)
22+
{
23+
var result = new UpdateToTargetsResponse();
24+
foreach (var jsonProp in obj.Properties())
25+
{
26+
switch (jsonProp.Name)
27+
{
28+
case "added":
29+
result.Added = jsonProp.Value.ToObject<List<string>>();
30+
break;
31+
case "removed":
32+
result.Removed = jsonProp.Value.ToObject<List<string>>();
33+
break;
34+
case "activity":
35+
result.Activity = Activity.FromJson(jsonProp.Value.ToObject<JObject>());
36+
break;
37+
}
38+
}
39+
return result;
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)