Skip to content

Commit 6dccff1

Browse files
committed
Add support for with_activity_data when retrieving reactions
1 parent e6d0bb1 commit 6dccff1

File tree

2 files changed

+91
-8
lines changed

2 files changed

+91
-8
lines changed

src/stream-net/FeedFilter.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Stream.Rest;
22
using System.Collections.Generic;
3+
using System.Linq;
34

45
namespace Stream
56
{
@@ -10,7 +11,8 @@ internal enum OpType
1011
id_gte,
1112
id_gt,
1213
id_lte,
13-
id_lt
14+
id_lt,
15+
with_activity_data
1416
}
1517

1618
internal class OpEntry
@@ -32,6 +34,12 @@ private FeedFilter()
3234
{
3335
}
3436

37+
public FeedFilter WithActivityData()
38+
{
39+
_ops.Add(new OpEntry(OpType.with_activity_data, "true"));
40+
return this;
41+
}
42+
3543
public FeedFilter IdGreaterThan(string id)
3644
{
3745
_ops.Add(new OpEntry(OpType.id_gt, id));
@@ -64,6 +72,14 @@ internal void Apply(RestRequest request)
6472
});
6573
}
6674

75+
internal bool IncludesActivityData
76+
{
77+
get
78+
{
79+
return _ops.Any(x => x.Type == OpType.with_activity_data);
80+
}
81+
}
82+
6783
#region starts
6884

6985
public static FeedFilter Where()

src/stream-net/Reactions.cs

Lines changed: 74 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@
33
using Stream.Rest;
44
using System;
55
using System.Collections.Generic;
6-
using System.Linq;
76
using System.Threading.Tasks;
87

98
namespace Stream
109
{
1110
using ReactionFilter = FeedFilter;
1211

12+
public class ReactionsWithActivity
13+
{
14+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "results")]
15+
public IEnumerable<Reaction> Reactions { get; internal set; }
16+
17+
public EnrichedActivity Activity { get; internal set; }
18+
}
19+
1320
public class Reaction
1421
{
1522
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "id")]
@@ -65,6 +72,13 @@ public ReactionFiltering WithFilter(ReactionFilter filter)
6572
return this;
6673
}
6774

75+
internal ReactionFiltering WithActivityData()
76+
{
77+
_filter = (_filter == null) ? ReactionFilter.Where().WithActivityData() : _filter.WithActivityData();
78+
79+
return this;
80+
}
81+
6882
internal void Apply(RestRequest request)
6983
{
7084
request.AddQueryParameter("limit", _limit.ToString());
@@ -74,6 +88,14 @@ internal void Apply(RestRequest request)
7488
_filter.Apply(request);
7589
}
7690

91+
internal bool IncludesActivityData
92+
{
93+
get
94+
{
95+
return _filter.IncludesActivityData;
96+
}
97+
}
98+
7799
public static ReactionFiltering Default
78100
{
79101
get
@@ -142,7 +164,21 @@ private class ReactionsFilterResponse
142164
{
143165
[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "results")]
144166
public IEnumerable<Reaction> Reactions { get; internal set; }
145-
}
167+
168+
internal static EnrichedActivity GetActivity(string json)
169+
{
170+
JObject obj = JObject.Parse(json);
171+
foreach (var prop in obj.Properties())
172+
{
173+
if (prop.Name == "activity")
174+
{
175+
return EnrichedActivity.FromJson((JObject)prop.Value);
176+
}
177+
}
178+
179+
return null;
180+
}
181+
}
146182

147183
internal Reactions(StreamClient client)
148184
{
@@ -193,20 +229,51 @@ public async Task<Reaction> Get(string reactionID)
193229

194230
public async Task<IEnumerable<Reaction>> Filter(ReactionFiltering filtering, ReactionPagination pagination)
195231
{
196-
var urlPath = pagination.GetPath();
197-
var request = this._client.BuildJWTAppRequest($"reaction/{urlPath}", HttpMethod.GET);
198-
filtering.Apply(request);
232+
//var urlPath = pagination.GetPath();
233+
//var request = this._client.BuildJWTAppRequest($"reaction/{urlPath}", HttpMethod.GET);
234+
//filtering.Apply(request);
199235

200-
var response = await this._client.MakeRequest(request);
236+
//var response = await this._client.MakeRequest(request);
237+
var response = await FilterHelper(filtering, pagination);
201238

202239
if (response.StatusCode == System.Net.HttpStatusCode.OK)
203-
{
240+
{
204241
return JsonConvert.DeserializeObject<ReactionsFilterResponse>(response.Content).Reactions;
205242
}
206243

207244
throw StreamException.FromResponse(response);
208245
}
209246

247+
public async Task<ReactionsWithActivity> FilterWithActivityData(ReactionFiltering filtering, ReactionPagination pagination)
248+
{
249+
var response = await FilterHelper(filtering.WithActivityData(), pagination);
250+
251+
if (response.StatusCode == System.Net.HttpStatusCode.OK)
252+
{
253+
var reactions = JsonConvert.DeserializeObject<ReactionsFilterResponse>(response.Content).Reactions;
254+
var activity = ReactionsFilterResponse.GetActivity(response.Content);
255+
256+
return new ReactionsWithActivity
257+
{
258+
Reactions = reactions,
259+
Activity = activity
260+
};
261+
}
262+
263+
throw StreamException.FromResponse(response);
264+
}
265+
266+
private async Task<RestResponse> FilterHelper(ReactionFiltering filtering, ReactionPagination pagination)
267+
{
268+
var urlPath = pagination.GetPath();
269+
var request = this._client.BuildJWTAppRequest($"reaction/{urlPath}", HttpMethod.GET);
270+
filtering.Apply(request);
271+
272+
var response = await this._client.MakeRequest(request);
273+
274+
return response;
275+
}
276+
210277
public async Task<Reaction> Update(string reactionID, IDictionary<string, object> data = null, IEnumerable<string> targetFeeds = null)
211278
{
212279
var r = new Reaction()

0 commit comments

Comments
 (0)