From 06daf1757ca3d7cfb673f9b13d1fe315619b6ba7 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Wed, 9 Mar 2016 13:54:08 +0100 Subject: [PATCH] Patch up Matrix.Event.Typing * Add from_json() and to_json() methods --- src/matrix-event-typing.vala | 63 ++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/src/matrix-event-typing.vala b/src/matrix-event-typing.vala index ee72306..234a2a8 100644 --- a/src/matrix-event-typing.vala +++ b/src/matrix-event-typing.vala @@ -22,9 +22,14 @@ * Informs the client of the list of users currently typing. */ public class Matrix.Event.Typing : Matrix.Event.Base { - private List? _user_ids = null; - + /** + * The ID of the room this event belongs to. + */ public string? room_id { get; set; default = null; } + + /** + * The list of user IDs typing in this room, if any. + */ public List? user_ids { get { return _user_ids; @@ -36,4 +41,58 @@ public class Matrix.Event.Typing : Matrix.Event.Base { default = null; } + + private List? _user_ids = null; + + protected override void + from_json(Json.Node json_data) + throws Matrix.Error + { + var root = json_data.get_object(); + var content_root = root.get_member("content").get_object(); + Json.Node? node; + + if ((node = root.get_member("room_id")) != null) { + _room_id = node.get_string(); + } else if (Config.DEBUG) { + warning("room_id is missing from a m.typing event"); + } + + if ((node = content_root.get_member("user_ids")) != null) { + _user_ids = null; + + node.get_array().foreach_element((ary, idx, user_node) => { + _user_ids.prepend(user_node.get_string()); + }); + } else { + warning("content.user_ids is missing from a m.typing event"); + } + + base.from_json(json_data); + } + + protected override void + to_json(Json.Node json_data) + throws Matrix.Error + { + if (_room_id == null) { + throw new Matrix.Error.INCOMPLETE( + "Won't generate a m.typing event without room_id"); + } + + var root = json_data.get_object(); + var content_root = root.get_member("content").get_object(); + + root.set_string_member("room_id", _room_id); + + var users_ary = new Json.Array(); + var users_node = new Json.Node(Json.NodeType.ARRAY); + content_root.set_member("user_ids", users_node); + + foreach (var entry in _user_ids) { + users_ary.add_string_element(entry); + } + + base.to_json(json_data); + } }