internal async Task <StreamResponse <T> > GetEnriched <T>(GetOptions options = null) where T : EnrichedActivity { // build request options = options ?? GetOptions.Default; var request = _client.BuildEnrichedFeedRequest(this, "/", HttpMethod.GET); options.Apply(request); // make request var response = await _client.MakeRequest(request); if (response.StatusCode != System.Net.HttpStatusCode.OK) { throw StreamException.FromResponse(response); } // handle response var result = new StreamResponse <T>(); JObject obj = JObject.Parse(response.Content); foreach (var prop in obj.Properties()) { switch (prop.Name) { case "results": case "activities": { // get the results var array = prop.Value as JArray; result.Results = array.Select(a => EnrichedActivity.FromJson((JObject)a) as T).ToList(); break; } case "unseen": { result.Unseen = prop.Value.Value <long>(); break; } case "unread": { result.Unread = prop.Value.Value <long>(); break; } case "duration": { result.Duration = prop.Value.Value <String>(); break; } default: break; } } return(result); }
internal static EnrichedActivity GetActivity(string json) { JObject obj = JObject.Parse(json); foreach (var prop in obj.Properties()) { if (prop.Name == "activity") { return(EnrichedActivity.FromJson((JObject)prop.Value)); } } return(null); }
internal static EnrichedActivity FromJson(JObject obj) { EnrichedActivity activity = new EnrichedActivity(); EnrichedAggregatedActivity aggregateActivity = null; EnrichedNotificationActivity notificationActivity = null; if (obj.Properties().Any(p => p.Name == Field_Activities)) { if ((obj.Properties().Any(p => p.Name == Field_IsRead)) || (obj.Properties().Any(p => p.Name == Field_IsSeen))) { activity = aggregateActivity = notificationActivity = new EnrichedNotificationActivity(); } else { activity = aggregateActivity = new EnrichedAggregatedActivity(); } } obj.Properties().ForEach((prop) => { switch (prop.Name) { case Field_Id: activity.Id = prop.Value.Value <string>(); break; case Field_Actor: activity.Actor = EnrichableField.FromJSON(prop.Value); break; case Field_Verb: activity.Verb = EnrichableField.FromJSON(prop.Value); break; case Field_Object: activity.Object = EnrichableField.FromJSON(prop.Value); break; case Field_Target: activity.Target = EnrichableField.FromJSON(prop.Value); break; case Field_ForeignId: activity.ForeignId = EnrichableField.FromJSON(prop.Value); break; case Field_Time: activity.Time = prop.Value.Value <DateTime>(); break; case Field_To: { JArray array = prop.Value as JArray; if ((array != null) && (array.SafeCount() > 0)) { if (array.First.Type == JTokenType.Array) { // need to take the first from each array List <string> tos = new List <string>(); foreach (var child in array) { var str = child.ToObject <string[]>(); tos.Add(str[0]); } activity.To = tos; } else { activity.To = prop.Value.ToObject <string[]>().ToList(); } } else { activity.To = new List <string>(); } break; } case Field_Activities: { var activities = new List <EnrichedActivity>(); JArray array = prop.Value as JArray; if ((array != null) && (array.SafeCount() > 0)) { foreach (var child in array) { var childJO = child as JObject; if (childJO != null) { activities.Add(FromJson(childJO)); } } } if (aggregateActivity != null) { aggregateActivity.Activities = activities; } break; } case Field_ActorCount: { if (aggregateActivity != null) { aggregateActivity.ActorCount = prop.Value.Value <int>(); } break; } case Field_IsRead: { if (notificationActivity != null) { notificationActivity.IsRead = prop.Value.Value <bool>(); } break; } case Field_IsSeen: { if (notificationActivity != null) { notificationActivity.IsSeen = prop.Value.Value <bool>(); } break; } case Field_CreatedAt: { if (aggregateActivity != null) { aggregateActivity.CreatedAt = prop.Value.Value <DateTime>(); } break; } case Field_UpdatedAt: { if (aggregateActivity != null) { aggregateActivity.UpdatedAt = prop.Value.Value <DateTime>(); } break; } case Field_Group: { if (aggregateActivity != null) { aggregateActivity.Group = prop.Value.Value <string>(); } break; } case Field_OwnReactions: { activity.OwnReactions = prop.Value.ToObject <IDictionary <string, IEnumerable <Reaction> > >(); break; } case Field_LatestReactions: { activity.LatestReactions = prop.Value.ToObject <IDictionary <string, IEnumerable <Reaction> > >(); break; } case Field_ReactionCounts: { activity.ReactionCounts = prop.Value.ToObject <IDictionary <string, int> >(); break; } default: { // stash everything else as custom activity._data[prop.Name] = prop.Value; break; }; } }); return(activity); }