A native .NET C# library for developing Matrix clients.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.4 KiB

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
namespace MatrixDotNetLib
{
public class MatrixSyncRooms
{
// once the JSON from "join" is parsed, this is set to its contents as an object
public Dictionary<string,MatrixSyncRoom> Joined { get; private set;}
public Dictionary<string, MatrixSyncRoom> Invited { get; private set; }
public Dictionary<string, MatrixSyncRoom> Left { get; private set; }
public JObject Join
{
get { return null; }
set
{
// takes JSON children and parses them into objects
// we have to do this bc the entity names under "join" are dynamic
// each entity holds a well-defined object though
// create dict for Joined
Joined = new Dictionary<string, MatrixSyncRoom>();
// these AREN'T an array
ParseRooms(value, "Joined");
}
}
public JObject Invite
{
get { return null; }
set
{
// create dict for Joined
Invited = new Dictionary<string, MatrixSyncRoom>();
// these AREN'T an array
ParseRooms(value, "Invited");
}
}
public JObject Leave
{
get { return null; }
set
{
// create dict for Joined
Left = new Dictionary<string, MatrixSyncRoom>();
// these AREN'T an array
ParseRooms(value, "Left");
}
}
public void ParseRooms(JObject roomsJson, string valType)
{
// get the children of this object
JEnumerable<JToken> kids = roomsJson.Children();
Dictionary<string,MatrixSyncRoom> roomList = new Dictionary<string,MatrixSyncRoom>();
foreach (JProperty kid in kids)
{
MatrixSyncRoom msr = JsonConvert.DeserializeObject<MatrixSyncRoom>(kid.Value.ToString());
roomList.Add(kid.Name, msr);
}
// get the target
this.GetType().GetProperty(valType).SetValue(this,roomList);
}
}
}
//// simplest implementation
//// might not work for UWP
//// sauce: https://stackoverflow.com/questions/5527316/how-to-set-the-content-of-an-httpwebrequest-in-c
//HttpContent requestContent = new StringContent(requestJson, Encoding.UTF8, "application/json");
//HttpClient client = new HttpClient();
//client.BaseAddress = new Uri(loginUrl);
//client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//HttpResponseMessage responseMessage = client.PostAsync(loginUrl, requestContent).Result;
//string responseString = responseMessage.Content.ReadAsStringAsync().Result;
//if(responseString.Contains("errcode"))
//{
// // deserialize into error object
// MatrixError error = JsonConvert.DeserializeObject<MatrixError>(responseString);
// // convert error object to a string
// string errMsg = error.ErrorCode + ": " + error.ErrorMessage;
// // throw exception (can be caught and handled gracefully)
// throw new Exception(errMsg);
//}
//else
//{
// MatrixLoginResponse response = JsonConvert.DeserializeObject<MatrixLoginResponse>(responseString);
// return response;
//}