public MatrixEventContent GetContent(JObject jobj, JsonSerializer serializer, string type) { Type T; if (!contentTypes.TryGetValue(type, out T)) { //Console.WriteLine ("Unknown Event:" + type); return(null); } try { if (T == typeof(MatrixMRoomMessage)) { MatrixMRoomMessage message = new MatrixMRoomMessage(); serializer.Populate(jobj.CreateReader(), message); T = MessageContentType(message.msgtype); } MatrixEventContent content = (MatrixEventContent)Activator.CreateInstance(T); content.mxContent = jobj; if (type == "m.receipt") { ((MatrixMReceipt)content).ParseJObject(jobj); } else { serializer.Populate(jobj.CreateReader(), content); } return(content); } catch (Exception ex) { throw new Exception($"Failed to get content for {type}"); } }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { // Load JObject from stream JObject jObject = JObject.Load(reader); if (objectType == typeof(MatrixEvent)) { // Populate the event itself MatrixEvent ev = new MatrixEvent(); serializer.Populate(jObject.CreateReader(), ev); JToken redact; if (jObject ["content"].HasValues) { ev.content = GetContent(jObject ["content"] as JObject, serializer, ev.type); } else if (((JObject)jObject["unsigned"]).TryGetValue("redacted_because", out redact)) { //TODO: Parse Redacted } return(ev); } if (objectType == typeof(MatrixEventContent)) { // Populate var ev = new MatrixEventContent(); ev.mxContent = jObject; return(ev); } return(null); }
public MatrixEventContent GetContent(JToken jobj, JsonSerializer serializer, string type) { Type T; if (contentTypes.TryGetValue(type, out T)) { if (T == typeof(MatrixMRoomMessage)) { MatrixMRoomMessage message = new MatrixMRoomMessage(); serializer.Populate(jobj.CreateReader(), message); T = MessageContentType(message.msgtype); } MatrixEventContent content = (MatrixEventContent)Activator.CreateInstance(T); if (type == "m.receipt") { ((MatrixMReceipt)content).ParseJObject((JObject)jobj); } else { serializer.Populate(jobj.CreateReader(), content); } return(content); } else { #if DEBUG var payload = jobj.ToString(); Console.WriteLine("Unknown Event:" + type); #endif return(new MatrixEventContent()); } }
public static MatrixEvent MockEvent( MatrixEventContent content, string stateKey = null, int age = 0) { MatrixEvent ev = new MatrixEvent(); ev.content = content; if (stateKey != null) { ev.state_key = stateKey; } ev.age = age; return(ev); }