From 39fd66946e9143c32f4495ef7afaa65f22a54428 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Tue, 8 Mar 2016 14:13:02 +0100 Subject: [PATCH] Add event handler for m.room.create --- .gitignore | 1 + src/Makefile.am | 1 + src/matrix-event-room-create.vala | 78 +++++++++++++++++++++++++++++++ src/matrix-event-types.c | 3 ++ 4 files changed, 83 insertions(+) create mode 100644 src/matrix-event-room-create.vala diff --git a/.gitignore b/.gitignore index b9e3bb5..a86560f 100644 --- a/.gitignore +++ b/.gitignore @@ -68,3 +68,4 @@ Makefile.in /src/matrix-event-room-name.c /src/matrix-event-tag.c /src/matrix-event-room-canonical-alias.c +/src/matrix-event-room-create.c diff --git a/src/Makefile.am b/src/Makefile.am index c760d20..a1e7e5f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -38,6 +38,7 @@ libmatrix_glib_0_0_la_VALA_SOURCES = \ matrix-event-room-name.vala \ matrix-event-tag.vala \ matrix-event-room-canonical-alias.vala \ + matrix-event-room-create.vala \ $(NULL) AM_CPPFLAGS += \ diff --git a/src/matrix-event-room-create.vala b/src/matrix-event-room-create.vala new file mode 100644 index 0000000..64c479b --- /dev/null +++ b/src/matrix-event-room-create.vala @@ -0,0 +1,78 @@ +/* + * 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 to hold a m.room.create event + * + * This is the first event in a room and cannot be changed. It acts as + * the root of all other events. + */ +public class Matrix.Event.RoomCreate : Matrix.Event.State { + /** + * The user_id of the room creator. This is set by the homeserver. + */ + public string? creator { get; set; default = null; } + + /** + * Whether users on other servers can join this room. Defaults to + * true if key does not exist. + */ + public bool? federate { get; set; default = null; } + + protected override void + from_json(Json.Node json_data) + throws Matrix.Error + { + var content_root = json_data.get_object() + .get_member("content").get_object(); + Json.Node? node; + + if ((node = content_root.get_member("creator")) != null) { + _creator = node.get_string(); + } else { + warning("content.creator is missing from a m.room.create event"); + } + + if ((node = content_root.get_member("m.federate")) != null) { + _federate = node.get_boolean(); + } + + base.from_json(json_data); + } + + protected override void + to_json(Json.Node json_data) + throws Matrix.Error + { + if (_creator == null) { + throw new Matrix.Error.INCOMPLETE( + "Won't send a m.room.create event without a creator key"); + } + + var content_root = json_data.get_object() + .get_member("content").get_object(); + + content_root.set_string_member("creator", _creator); + + if (_federate != null) { + content_root.set_boolean_member("m.federate", _federate); + } + + base.to_json(json_data); + } +} diff --git a/src/matrix-event-types.c b/src/matrix-event-types.c index 865e78d..800fedb 100644 --- a/src/matrix-event-types.c +++ b/src/matrix-event-types.c @@ -188,6 +188,9 @@ matrix_event_types_ctor(void) matrix_event_register_type("m.room.canonical_alias", MATRIX_EVENT_TYPE_ROOM_CANONICAL_ALIAS, NULL); + matrix_event_register_type("m.room.create", + MATRIX_EVENT_TYPE_ROOM_CREATE, + NULL); } void