diff --git a/.gitignore b/.gitignore index cfd952c..9882c01 100644 --- a/.gitignore +++ b/.gitignore @@ -72,3 +72,4 @@ Makefile.in /src/matrix-event-room-power-levels.c /src/matrix-event-room-avatar.c /src/matrix-event-room-message-feedback.c +/src/matrix-event-room-guest-access.c diff --git a/src/Makefile.am b/src/Makefile.am index c88c232..fd62d74 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -42,6 +42,7 @@ libmatrix_glib_0_0_la_VALA_SOURCES = \ matrix-event-room-power-levels.vala \ matrix-event-room-avatar.vala \ matrix-event-room-message-feedback.vala \ + matrix-event-room-guest-access.vala \ $(NULL) AM_CPPFLAGS += \ diff --git a/src/matrix-event-room-guest-access.vala b/src/matrix-event-room-guest-access.vala new file mode 100644 index 0000000..5dd4b67 --- /dev/null +++ b/src/matrix-event-room-guest-access.vala @@ -0,0 +1,81 @@ +/* + * 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.guest_access event + * + * This event controls whether guest users are allowed to join + * rooms. If this event is absent, servers should act as if it is + * present and has the guest_access value `forbidden`. + */ + +/** + * Class to hold a m.room.guest_access event + */ +public class Matrix.Event.RoomGuestAccess : Matrix.Event.State { + /** + * Whether guests can join the room. + */ + public GuestAccess? guest_access { 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("guest_access")) != null) { + GuestAccess? rules = (GuestAccess?)_g_enum_nick_to_value( + typeof(GuestAccess), node.get_string()); + + if (rules != null) { + _guest_access = rules; + } else { + _guest_access = GuestAccess.UNKNOWN; + + warning("Unknown value '%s' in a m.room.guest_access event", + node.get_string()); + } + } else { + warning("content.guest_access is missing from a m.room.guest_access event"); + } + + base.from_json(json_data); + } + + protected override void + to_json(Json.Node json_data) + throws Matrix.Error + { + if ((_guest_access == null) || (_guest_access == GuestAccess.UNKNOWN)) { + throw new Matrix.Error.INCOMPLETE( + "Won't generate a m.room.guest_access event with an unset content.guest_access key"); + } + + var content_root = json_data.get_object() + .get_member("content").get_object(); + + content_root.set_string_member( + "guest_access", + _g_enum_value_to_nick(typeof(GuestAccess), _guest_access)); + + base.to_json(json_data); + } +} diff --git a/src/matrix-event-types.c b/src/matrix-event-types.c index 5bb259e..22168b7 100644 --- a/src/matrix-event-types.c +++ b/src/matrix-event-types.c @@ -200,6 +200,9 @@ matrix_event_types_ctor(void) matrix_event_register_type("m.room.message.feedback", MATRIX_EVENT_TYPE_ROOM_MESSAGE_FEEDBACK, NULL); + matrix_event_register_type("m.room.guest_access", + MATRIX_EVENT_TYPE_ROOM_GUEST_ACCESS, + NULL); } void diff --git a/src/matrix-types.vala b/src/matrix-types.vala index 267462c..d8f6e8c 100644 --- a/src/matrix-types.vala +++ b/src/matrix-types.vala @@ -270,6 +270,12 @@ namespace Matrix { WORLD_READABLE; } + public enum GuestAccess { + UNKNOWN, /// represents a value unknown to this library + CAN_JOIN, /// guest users are allowed to access the room + FORBIDDEN; /// guest users are not allowed to access the room + } + public struct ImageInfo { int? size; int? height;