From 3e6a021a0fda448a573b81e4141f8000563b3065 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Wed, 3 Feb 2016 11:31:57 +0100 Subject: [PATCH] Add MatrixRoomMemberEvent --- .gitignore | 1 + .../matrix-glib/matrix-glib-docs.xml | 9 +- .../matrix-glib/matrix-glib-sections.txt | 26 ++++ src/Makefile.am | 1 + src/matrix-event-types.c | 3 + src/matrix-room-member-event.vala | 119 ++++++++++++++++++ 6 files changed, 157 insertions(+), 2 deletions(-) create mode 100644 src/matrix-room-member-event.vala diff --git a/.gitignore b/.gitignore index 7ee3c75..2f1b476 100644 --- a/.gitignore +++ b/.gitignore @@ -68,3 +68,4 @@ Makefile.in /src/matrix-compacts.c /src/matrix-event.c /src/matrix-room-event.c +/src/matrix-room-member-event.c diff --git a/docs/reference/matrix-glib/matrix-glib-docs.xml b/docs/reference/matrix-glib/matrix-glib-docs.xml index fbb1ff6..93138c8 100644 --- a/docs/reference/matrix-glib/matrix-glib-docs.xml +++ b/docs/reference/matrix-glib/matrix-glib-docs.xml @@ -18,8 +18,13 @@ Matrix Client - - +
+ Types + + + +
+ diff --git a/docs/reference/matrix-glib/matrix-glib-sections.txt b/docs/reference/matrix-glib/matrix-glib-sections.txt index 269d579..949de12 100644 --- a/docs/reference/matrix-glib/matrix-glib-sections.txt +++ b/docs/reference/matrix-glib/matrix-glib-sections.txt @@ -1,3 +1,29 @@ +
+matrix-room-member-event +MatrixRoomMemberEvent +matrix_room_member_event_set_membership +matrix_room_member_event_get_membership +matrix_room_member_event_set_state_key +matrix_room_member_event_get_state_key +matrix_room_member_event_set_avatar_url +matrix_room_member_event_get_avatar_url +matrix_room_member_event_set_display_name +matrix_room_member_event_get_display_name + + +MATRIX_IS_ROOM_MEMBER_EVENT +MATRIX_IS_ROOM_MEMBER_EVENT_CLASS +MATRIX_ROOM_MEMBER_EVENT +MATRIX_ROOM_MEMBER_EVENT_CLASS +MATRIX_ROOM_MEMBER_EVENT_GET_CLASS +MATRIX_TYPE_ROOM_MEMBER_EVENT +MatrixRoomMemberEvent +MatrixRoomMemberEventPrivate +MatrixRoomMemberEventClass +matrix_room_member_event_get_type +matrix_room_member_event_construct +
+
matrix-event MatrixEvent diff --git a/src/Makefile.am b/src/Makefile.am index 1cf0a44..875af2f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -24,6 +24,7 @@ libmatrix_glib_0_0_la_VALA_SOURCES = \ matrix-compacts.vala \ matrix-event.vala \ matrix-room-event.vala \ + matrix-room-member-event.vala \ $(NULL) AM_CPPFLAGS += \ diff --git a/src/matrix-event-types.c b/src/matrix-event-types.c index 727cc70..a14dae8 100644 --- a/src/matrix-event-types.c +++ b/src/matrix-event-types.c @@ -152,6 +152,9 @@ G_DEFINE_CONSTRUCTOR(matrix_event_types_ctor); static void matrix_event_types_ctor(void) { + matrix_event_register_type("m.room.member", + MATRIX_TYPE_ROOM_MEMBER_EVENT, + NULL); } void diff --git a/src/matrix-room-member-event.vala b/src/matrix-room-member-event.vala new file mode 100644 index 0000000..5907e6c --- /dev/null +++ b/src/matrix-room-member-event.vala @@ -0,0 +1,119 @@ +/* + * This file is part of matrix-glib-sdk + * + * matrix-glib-sdk is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * matrix-glib-sdk is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with matrix-glib-sdk. If not, see + * . + */ + +/** + * Class for representing a room membership events + * + * The room membership event class. + */ +public class Matrix.RoomMemberEvent : Matrix.RoomEvent { + public string? state_key { get; set; } + public RoomMembership membership { + get; set; + default = RoomMembership.UNKNOWN; + } + public string? avatar_url { get; set; } + public string? display_name { get; set; } + + protected override void + from_json(Json.Node json_data) + throws Matrix.Error + { + Json.Object root = json_data.get_object(); + Json.Object content_root = root.get_member("content").get_object(); + Json.Node? node; + + if ((node = root.get_member("state_key")) != null) { + _state_key = node.get_string(); + } else { + GLib.warning("state_key is missing from the m.room.member event"); + } + + if ((node = root.get_member("room_id")) != null) { + _room_id = node.get_string(); + } else { + GLib.warning("room_id is missing from the m.room.member event"); + } + + if ((node = content_root.get_member("membership")) != null) { + Matrix.RoomMembership? mship = (Matrix.RoomMembership?)_g_enum_nick_to_value( + typeof(Matrix.RoomMembership), + node.get_string()); + + if (mship != null) { + _membership = mship; + } + } else { + GLib.warning("membership key is missing from the m.room.member event"); + } + + if ((node = content_root.get_member("avatar_url")) != null) { + _avatar_url = node.get_string(); + } + + if ((node = content_root.get_member("displayname")) != null) { + _display_name = node.get_string(); + } + + // Chain up + base.from_json(json_data); + } + + protected override void + to_json(Json.Node json_data) + throws Matrix.Error + { + Json.Object root, content_root; + string? mship; + + if (membership == RoomMembership.UNKNOWN) { + throw new Matrix.Error.UNKNOWN_VALUE( + "Unknown membership value cannot be added to a room member event"); + } + + root = json_data.get_object(); + + content_root = _json_object_node_ensure_field(json_data, + "content", + Json.NodeType.OBJECT) + .get_object(); + + root.set_string_member("state_key", state_key); + + if (room_id != null) { + root.set_string_member("room_id", room_id); + } + + mship = _g_enum_value_to_nick(typeof(Matrix.RoomMembership), + membership); + + if (mship != null) { + content_root.set_string_member("membership", mship); + } + + if (avatar_url != null) { + content_root.set_string_member("avatar_url", avatar_url); + } + + if (display_name != null) { + content_root.set_string_member("displayname", display_name); + } + + base.to_json(json_data); + } +}