Skip to content

Commit 95ab421

Browse files
authored
[FEEDS-1132]return ranking expression with score vars (#170)
* chore: return ranking expression with score vars * chore: fix test
1 parent 8a3d7b9 commit 95ab421

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

src/Models/GenericResponse.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using Newtonsoft.Json;
12
using System.Collections.Generic;
23

34
namespace Stream.Models
@@ -7,6 +8,10 @@ public class GenericGetResponse<T> : ResponseBase
78
{
89
/// <summary>Container for <typeparamref name="T"/> objects.</summary>
910
public List<T> Results { get; set; }
11+
12+
/// <summary>The ranking expression used for scoring activities.</summary>
13+
[JsonProperty("ranking_expr")]
14+
public string RankingExpression { get; set; }
1015
}
1116

1217
/// <summary>Base class for personalized read responses of <typeparamref name="T"/>.</summary>

src/Models/GetOptions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class GetOptions
2020
private string _user_id = null;
2121
private string _ranking_vars = null;
2222
private bool _score_vars = false;
23+
private bool _ranking_expr = false;
2324
private string _discard_actors = null;
2425
private string _discard_actors_sep = null;
2526
private string _moderation_template = null;
@@ -68,6 +69,12 @@ public GetOptions WithScoreVars()
6869
return this;
6970
}
7071

72+
public GetOptions WithRankingExpr()
73+
{
74+
_ranking_expr = true;
75+
return this;
76+
}
77+
7178
public GetOptions WithRankingVars(IDictionary<string, object> rankingVars)
7279
{
7380
_ranking_vars = StreamJsonConverter.SerializeObject(rankingVars);
@@ -149,6 +156,9 @@ internal void Apply(RestRequest request)
149156
if (_score_vars)
150157
request.AddQueryParameter("withScoreVars", "true");
151158

159+
if (_ranking_expr)
160+
request.AddQueryParameter("with_ranking_expr", "true");
161+
152162
if (!string.IsNullOrWhiteSpace(_discard_actors_sep))
153163
request.AddQueryParameter("discard_actors_sep", _discard_actors_sep);
154164

tests/ActivityTests/GetActivityTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,36 @@ public async Task TestScoreVars()
172172
Assert.IsNull(r2.Results[0].ScoreVars);
173173
}
174174

175+
[Test]
176+
public async Task TestRankingExpression()
177+
{
178+
var feed = this.RankedFeed;
179+
180+
var newActivity1 = new Activity("1", "test", "1")
181+
{
182+
ForeignId = "r-test-1",
183+
Time = DateTime.Parse("2000-08-16T16:32:32"),
184+
};
185+
186+
newActivity1.SetData("popularity", 123);
187+
var r1 = await feed.AddActivityAsync(newActivity1);
188+
189+
// Test with ranking expression flag
190+
var r2 = await feed.GetFlatActivitiesAsync(GetOptions.Default.WithLimit(1).WithRanking("popularity").WithRankingExpr());
191+
Assert.IsNotNull(r2.RankingExpression);
192+
Assert.IsNotEmpty(r2.RankingExpression);
193+
194+
// Test without ranking expression flag
195+
r2 = await feed.GetFlatActivitiesAsync(GetOptions.Default.WithLimit(1).WithRanking("popularity"));
196+
Assert.IsNull(r2.RankingExpression);
197+
198+
// Test with both score vars and ranking expression
199+
r2 = await feed.GetFlatActivitiesAsync(GetOptions.Default.WithLimit(1).WithRanking("popularity").WithScoreVars().WithRankingExpr());
200+
Assert.IsNotNull(r2.RankingExpression);
201+
Assert.IsNotEmpty(r2.RankingExpression);
202+
Assert.IsNotNull(r2.Results[0].ScoreVars);
203+
}
204+
175205
[Test]
176206
public async Task TestGetActivitiesByForeignIDAndTime()
177207
{

tests/TestBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void Setup()
2222
FlatFeed = Client.Feed("flat", System.Guid.NewGuid().ToString());
2323
AggregateFeed = Client.Feed("aggregate", System.Guid.NewGuid().ToString());
2424
NotificationFeed = Client.Feed("notification", System.Guid.NewGuid().ToString());
25-
RankedFeed = Client.Feed("ranked", System.Guid.NewGuid().ToString());
25+
RankedFeed = Client.Feed("user", System.Guid.NewGuid().ToString());
2626
}
2727
}
2828
}

0 commit comments

Comments
 (0)