Skip to content

Commit 856da7a

Browse files
authored
Add follow stats (#52)
1 parent bdf2952 commit 856da7a

File tree

5 files changed

+77
-2
lines changed

5 files changed

+77
-2
lines changed

src/stream-net-tests/IntegrationTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using System.IO;
66
using System.Linq;
7+
using System.Text;
78
using System.Threading.Tasks;
89

910
namespace stream_net_tests
@@ -2503,5 +2504,25 @@ public async Task TestOG()
25032504
Assert.IsNotEmpty(og.Images);
25042505
Assert.IsNotEmpty(og.Images[0].Image);
25052506
}
2507+
2508+
[Test]
2509+
public async Task TestFollowStats()
2510+
{
2511+
var f1 = _client.Feed("user", System.Guid.NewGuid().ToString());
2512+
var f2 = _client.Feed("user", System.Guid.NewGuid().ToString());
2513+
await f1.FollowFeed(f2);
2514+
2515+
var stats = await f1.FollowStats(null, new string[] { "timeline" });
2516+
Assert.AreEqual(stats.Followers.Count, 0);
2517+
Assert.AreEqual(stats.Followers.Feed, f1.FeedId);
2518+
Assert.AreEqual(stats.Following.Count, 0);
2519+
Assert.AreEqual(stats.Following.Feed, f1.FeedId);
2520+
2521+
stats = await f1.FollowStats(null, new string[] { "user" });
2522+
Assert.AreEqual(stats.Followers.Count, 0);
2523+
Assert.AreEqual(stats.Followers.Feed, f1.FeedId);
2524+
Assert.AreEqual(stats.Following.Count, 1);
2525+
Assert.AreEqual(stats.Following.Feed, f1.FeedId);
2526+
}
25062527
}
25072528
}

src/stream-net/FollowStats.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Stream
4+
{
5+
6+
public class FollowStat
7+
{
8+
[JsonProperty("count")]
9+
public int Count { get; set; }
10+
11+
[JsonProperty("feed")]
12+
public string Feed { get; set; }
13+
}
14+
15+
public class FollowStats
16+
{
17+
18+
[JsonProperty("followers")]
19+
public FollowStat Followers { get; set; }
20+
21+
[JsonProperty("following")]
22+
public FollowStat Following { get; set; }
23+
}
24+
}
25+
26+

src/stream-net/IStreamFeed.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public interface IStreamFeed
2727
Task UnfollowFeed(string targetFeedSlug, string targetUserId, bool keepHistory = false);
2828
Task UpdateActivities(IEnumerable<Activity> activities);
2929
Task UpdateActivity(Activity activity);
30+
Task<FollowStats> FollowStats(string[] followersSlugs = null, string[] followingSlugs = null);
3031
Task<UpdateToTargetsResponse> UpdateActivityToTargets(ForeignIDTime foreignIDTime,
3132
IEnumerable<string> adds = null,
3233
IEnumerable<string> newTargets = null,

src/stream-net/StreamClientToken.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
22
using System;
33
using System.Collections.Generic;
44
using System.Security.Cryptography;

src/stream-net/StreamFeed.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
22
using Newtonsoft.Json.Linq;
33
using Stream.Rest;
44
using System;
@@ -424,5 +424,32 @@ private void ValidateFeedFollow(IStreamFeed feed)
424424
throw new ArgumentException("Cannot follow/unfollow myself");
425425
}
426426

427+
internal class FollowStatsResponse
428+
{
429+
public FollowStats Results { get; set; }
430+
}
431+
432+
public async Task<FollowStats> FollowStats(string[] followersSlugs = null, string[] followingSlugs = null)
433+
{
434+
var request = this._client.BuildAppRequest("stats/follow/", HttpMethod.GET);
435+
request.AddQueryParameter("followers", this.FeedId);
436+
request.AddQueryParameter("following", this.FeedId);
437+
438+
if (followersSlugs != null)
439+
{
440+
request.AddQueryParameter("followers_slugs", string.Join(",", followersSlugs));
441+
}
442+
if (followingSlugs != null)
443+
{
444+
request.AddQueryParameter("following_slugs", string.Join(",", followingSlugs));
445+
}
446+
447+
var response = await _client.MakeRequest(request);
448+
if (response.StatusCode != System.Net.HttpStatusCode.OK)
449+
throw StreamException.FromResponse(response);
450+
451+
return JsonConvert.DeserializeObject<FollowStatsResponse>(response.Content).Results;
452+
}
453+
427454
}
428455
}

0 commit comments

Comments
 (0)