diff --git a/src/matrix-message-base.vala b/src/matrix-message-base.vala index 7e58d72..1cd2d7c 100644 --- a/src/matrix-message-base.vala +++ b/src/matrix-message-base.vala @@ -81,6 +81,9 @@ public abstract class Matrix.Message.Base : Object, Initable { initialize_from_json(Json.Node json_data) throws Matrix.Error { + var gen = new Json.Generator(); + gen.set_root(json_data); + if (json_data.get_node_type() != Json.NodeType.OBJECT) { throw new Matrix.Error.INVALID_FORMAT( "The message is not valid"); @@ -132,6 +135,12 @@ public abstract class Matrix.Message.Base : Object, Initable { } else if (Config.DEBUG) { warning("msgtype is not present in a message"); } + + if ((node = root.get_member("body")) != null) { + _body = node.get_string(); + } else if (Config.DEBUG) { + warning("body is not presente in a message"); + } } /** @@ -149,6 +158,11 @@ public abstract class Matrix.Message.Base : Object, Initable { "Won't generate a message with an empty msgtype"); } + if (_body == null) { + throw new Matrix.Error.INCOMPLETE( + "Won't generate a message without body"); + } + root.set_string_member("msgtype", _message_type); } } diff --git a/src/matrix-message-emote.vala b/src/matrix-message-emote.vala index d8fa0bb..15e3e31 100644 --- a/src/matrix-message-emote.vala +++ b/src/matrix-message-emote.vala @@ -25,4 +25,18 @@ * sender. This message could also be represented in a different * colour to distinguish it from regular `m.text` messages. */ -public class Matrix.Message.Emote : Matrix.Message.Base {} +public class Matrix.Message.Emote : Matrix.Message.Base { + public override void + from_json(Json.Node json_data) + throws Matrix.Error + { + base.from_json(json_data); + } + + public override void + to_json(Json.Node json_data) + throws Matrix.Error + { + base.to_json(json_data); + } +} diff --git a/src/matrix-message-notice.vala b/src/matrix-message-notice.vala index 011754b..47b4b38 100644 --- a/src/matrix-message-notice.vala +++ b/src/matrix-message-notice.vala @@ -28,4 +28,18 @@ * infinite-loop situations where two automated clients continuously * exchange messages, as each responds to the other. */ -public class Matrix.Message.Notice : Matrix.Message.Base {} +public class Matrix.Message.Notice : Matrix.Message.Base { + public override void + from_json(Json.Node json_data) + throws Matrix.Error + { + base.from_json(json_data); + } + + public override void + to_json(Json.Node json_data) + throws Matrix.Error + { + base.to_json(json_data); + } +} diff --git a/src/matrix-message-text.vala b/src/matrix-message-text.vala index b6dd377..91ae5f4 100644 --- a/src/matrix-message-text.vala +++ b/src/matrix-message-text.vala @@ -21,4 +21,18 @@ * * Handle plain text messages. */ -public class Matrix.Message.Text : Matrix.Message.Base {} +public class Matrix.Message.Text : Matrix.Message.Base { + public override void + from_json(Json.Node json_data) + throws Matrix.Error + { + base.from_json(json_data); + } + + public override void + to_json(Json.Node json_data) + throws Matrix.Error + { + base.to_json(json_data); + } +}