Skip to content

Commit a8e27b3

Browse files
committed
chore: return ranking expression with score vars
1 parent 8a3d7b9 commit a8e27b3

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
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
{

0 commit comments

Comments
 (0)