Skip to content

Commit 7e0b00c

Browse files
authored
[PBE-1129] Add support for ranking and score variables (#121)
* chore: add support for ranking vars (string) * chore: changed parameter to dictionary * chore: added ranking test * chore: change feed in test * chore: change ranking test * chore: fix test * chore: ignore test * chore: added scorevars * chore: added score vars test * chore: ignoring test
1 parent 9c11f5c commit 7e0b00c

File tree

5 files changed

+78
-1
lines changed

5 files changed

+78
-1
lines changed

src/Models/Activity.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class Activity : ActivityBase
2121
public string Object { get; set; }
2222
public string Target { get; set; }
2323
public string Origin { get; set; }
24+
public Dictionary<string, object> ScoreVars { get; set; }
2425

2526
public Activity(string actor, string verb, string @object)
2627
{

src/Models/GetOptions.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Stream.Rest;
2+
using Stream.Utils;
23
using System.Collections.Generic;
34

45
namespace Stream.Models
@@ -17,6 +18,8 @@ public class GetOptions
1718
private string _endpoint = null;
1819
private string _feed_slug = null;
1920
private string _user_id = null;
21+
private string _ranking_vars = null;
22+
private bool _score_vars = false;
2023

2124
private IDictionary<string, string> _custom = null;
2225

@@ -56,6 +59,18 @@ public GetOptions WithRanking(string rankingSlug)
5659
return this;
5760
}
5861

62+
public GetOptions WithScoreVars()
63+
{
64+
_score_vars = true;
65+
return this;
66+
}
67+
68+
public GetOptions WithRankingVars(IDictionary<string, object> rankingVars)
69+
{
70+
_ranking_vars = StreamJsonConverter.SerializeObject(rankingVars);
71+
return this;
72+
}
73+
5974
public GetOptions WithSession(string session)
6075
{
6176
_session = session;
@@ -111,6 +126,12 @@ internal void Apply(RestRequest request)
111126
if (!string.IsNullOrWhiteSpace(_user_id))
112127
request.AddQueryParameter("user_id", _user_id);
113128

129+
if (!string.IsNullOrWhiteSpace(_ranking_vars))
130+
request.AddQueryParameter("ranking_vars", _ranking_vars);
131+
132+
if (_score_vars)
133+
request.AddQueryParameter("withScoreVars", "true");
134+
114135
if (_custom != null)
115136
{
116137
foreach (KeyValuePair<string, string> kvp in _custom)

src/StreamFeed.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ internal StreamFeed(StreamClient client, string feedSlug, string userId)
4343
public async Task<Activity> AddActivityAsync(Activity activity)
4444
{
4545
var request = _client.BuildFeedRequest(this, "/", HttpMethod.Post);
46-
request.SetJsonBody(StreamJsonConverter.SerializeObject(activity));
46+
var body = StreamJsonConverter.SerializeObject(activity);
47+
request.SetJsonBody(body);
4748

4849
var response = await _client.MakeRequestAsync(request);
4950

tests/ActivityTests/GetActivityTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,58 @@ public async Task TestGetActivitiesByID()
9999
});
100100
}
101101

102+
[Test]
103+
[Ignore("Test database has no ranked method at the moment")]
104+
public async Task TestRankingVars()
105+
{
106+
var newActivity1 = new Activity("1", "test", "1")
107+
{
108+
ForeignId = "r-test-1",
109+
Time = DateTime.Parse("2000-08-16T16:32:32"),
110+
};
111+
112+
newActivity1.SetData("popularity", 123);
113+
114+
var response = await this.UserFeed.AddActivityAsync(newActivity1);
115+
116+
var newActivity2 = new Activity("1", "test", "2")
117+
{
118+
ForeignId = "r-test-2",
119+
Time = DateTime.Parse("2000-08-17T16:32:32"),
120+
};
121+
122+
response = await this.UserFeed.AddActivityAsync(newActivity2);
123+
124+
var ranking_vars = new Dictionary<string, object> { { "popularity", 666 } };
125+
var r2 = await this.UserFeed.GetFlatActivitiesAsync(GetOptions.Default.WithLimit(2).WithRanking("popular").WithRankingVars(ranking_vars));
126+
Assert.NotNull(r2);
127+
Assert.AreEqual(2, r2.Results.Count);
128+
Assert.AreEqual(r2.Results[0].Score, 11.090528);
129+
Assert.AreEqual(r2.Results[1].Score, 0.99999917);
130+
}
131+
132+
[Test]
133+
[Ignore("Test database has no ranked method at the moment")]
134+
public async Task TestScoreVars()
135+
{
136+
var feed = this.RankedFeed;
137+
138+
var newActivity1 = new Activity("1", "test", "1")
139+
{
140+
ForeignId = "r-test-1",
141+
Time = DateTime.Parse("2000-08-16T16:32:32"),
142+
};
143+
144+
newActivity1.SetData("popularity", 123);
145+
var r1 = await feed.AddActivityAsync(newActivity1);
146+
147+
var r2 = await feed.GetFlatActivitiesAsync(GetOptions.Default.WithLimit(1).WithRanking("popularity").WithScoreVars());
148+
Assert.IsNotNull(r2.Results[0].ScoreVars);
149+
150+
r2 = await feed.GetFlatActivitiesAsync(GetOptions.Default.WithLimit(1).WithRanking("popularity"));
151+
Assert.IsNull(r2.Results[0].ScoreVars);
152+
}
153+
102154
[Test]
103155
public async Task TestGetActivitiesByForeignIDAndTime()
104156
{

tests/TestBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public abstract class TestBase
88
protected IStreamClient Client { get; private set; }
99
protected IStreamFeed UserFeed { get; private set; }
1010
protected IStreamFeed UserFeed2 { get; private set; }
11+
protected IStreamFeed RankedFeed { get; private set; }
1112
protected IStreamFeed FlatFeed { get; private set; }
1213
protected IStreamFeed AggregateFeed { get; private set; }
1314
protected IStreamFeed NotificationFeed { get; private set; }
@@ -21,6 +22,7 @@ public void Setup()
2122
FlatFeed = Client.Feed("flat", System.Guid.NewGuid().ToString());
2223
AggregateFeed = Client.Feed("aggregate", System.Guid.NewGuid().ToString());
2324
NotificationFeed = Client.Feed("notification", System.Guid.NewGuid().ToString());
25+
RankedFeed = Client.Feed("ranked", System.Guid.NewGuid().ToString());
2426
}
2527
}
2628
}

0 commit comments

Comments
 (0)