From 2ec736fa3e4937e08b8998f7e37dc2f1d9a27474 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Wed, 22 Nov 2017 11:00:54 +0100 Subject: [PATCH] Port MatrixEventRoomGuestAccess to C --- .gitignore | 1 - src/Makefile.am | 3 +- src/matrix-event-room-guest-access.c | 255 ++++++++++++++++++++++++ src/matrix-event-room-guest-access.h | 41 ++++ src/matrix-event-room-guest-access.vala | 92 --------- src/matrix-event-types.c | 1 + vapi/c-api.vapi | 11 + 7 files changed, 310 insertions(+), 94 deletions(-) create mode 100644 src/matrix-event-room-guest-access.c create mode 100644 src/matrix-event-room-guest-access.h delete mode 100644 src/matrix-event-room-guest-access.vala diff --git a/.gitignore b/.gitignore index 31be807..fa57828 100644 --- a/.gitignore +++ b/.gitignore @@ -53,7 +53,6 @@ Makefile.in /src/matrix-http-client.c /src/namespace-info.vala /src/namespace-info.c -/src/matrix-event-room-guest-access.c /src/matrix-event-room-redaction.c /src/matrix-event-room-third-party-invite.c /src/matrix-event-call-candidates.c diff --git a/src/Makefile.am b/src/Makefile.am index 80bb6c6..8fc75e8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -21,7 +21,6 @@ libmatrix_glib_0_0_la_VALA_SOURCES = \ matrix-client.vala \ matrix-http-api.vala \ matrix-http-client.vala \ - matrix-event-room-guest-access.vala \ matrix-event-room-redaction.vala \ matrix-event-room-third-party-invite.vala \ matrix-event-call-candidates.vala \ @@ -109,6 +108,7 @@ INST_H_SRC_FILES = \ matrix-event-room-canonical-alias.h \ matrix-event-room-create.h \ matrix-event-room-power-levels.h \ + matrix-event-room-guest-access.h \ matrix-event-room-message-feedback.h \ matrix-event-typing.h \ matrix-event-receipt.h \ @@ -167,6 +167,7 @@ libmatrix_glib_0_0_la_SOURCES = \ matrix-event-room-create.c \ matrix-event-room-power-levels.c \ matrix-event-room-message-feedback.c \ + matrix-event-room-guest-access.c \ matrix-profile.c \ matrix-room.c \ utils.c \ diff --git a/src/matrix-event-room-guest-access.c b/src/matrix-event-room-guest-access.c new file mode 100644 index 0000000..48c60ae --- /dev/null +++ b/src/matrix-event-room-guest-access.c @@ -0,0 +1,255 @@ +/* + * 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 + * . + */ + +#include "matrix-event-room-guest-access.h" +#include "utils.h" +#include "matrix-enumtypes.h" + +/** + * SECTION:matrix-event-room-guest-access + * @short_description: event describing guest access to a room + * + * This is the default event handler for `m.room.guest_access` events. + * + * 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 set to `forbidden`. + */ +enum { + PROP_0, + PROP_GUEST_ACCESS, + NUM_PROPERTIES +}; + +static GParamSpec *matrix_event_room_guest_access_properties[NUM_PROPERTIES]; + +typedef struct { + MatrixGuestAccess _guest_access; +} MatrixEventRoomGuestAccessPrivate; + +/** + * MatrixEventRoomGuestAccess: + */ +G_DEFINE_TYPE_WITH_PRIVATE(MatrixEventRoomGuestAccess, matrix_event_room_guest_access, MATRIX_EVENT_TYPE_STATE); + +static void +matrix_event_room_guest_access_real_from_json(MatrixEventBase *matrix_event_base, JsonNode *json_data, GError **error) +{ + MatrixEventRoomGuestAccessPrivate *priv; + JsonObject *root; + JsonObject *content_root; + JsonNode *content_node; + JsonNode *node; + + g_return_if_fail(json_data != NULL); + + priv = matrix_event_room_guest_access_get_instance_private(MATRIX_EVENT_ROOM_GUEST_ACCESS(matrix_event_base)); + root = json_node_get_object(json_data); + content_node = json_object_get_member(root, "content"); + content_root = json_node_get_object(content_node); + +#if DEBUG + if ((node = json_object_get_member(root, "state_key")) != NULL) { + const gchar *state_key = json_node_get_string(node); + + if ((state_key == NULL) || (*state_key == 0)) { + g_warning("state_key of a m.room.guest_access is non-empty"); + } + } +#endif + + if ((node = json_object_get_member(content_root, "guest_access")) != NULL) { + GError *inner_error = NULL; + MatrixGuestAccess guest_access = _matrix_g_enum_nick_to_value(MATRIX_TYPE_GUEST_ACCESS, json_node_get_string(node), &inner_error); + + if (inner_error != NULL) { + priv->_guest_access = MATRIX_GUEST_ACCESS_UNKNOWN; + +#if DEBUG + g_warning("Unknown value '%s' in a m.room.guest_access event", json_node_get_string(node)); +#endif + } else { + priv->_guest_access = guest_access; + } + } else { + g_warning("content.guest_access is missing from a m.room.guest_access event"); + } + + MATRIX_EVENT_BASE_CLASS(matrix_event_room_guest_access_parent_class)->from_json(matrix_event_base, json_data, error); +} + +static void +matrix_event_room_guest_access_real_to_json(MatrixEventBase *matrix_event_base, JsonNode *json_data, GError **error) +{ + MatrixEventRoomGuestAccessPrivate *priv; + JsonObject *root; + JsonObject *content_root; + JsonNode *content_node; + const gchar *state_key; + gchar *guest_access; + + g_return_if_fail(json_data != NULL); + + priv = matrix_event_room_guest_access_get_instance_private(MATRIX_EVENT_ROOM_GUEST_ACCESS(matrix_event_base)); + root = json_node_get_object(json_data); + content_node = json_object_get_member(root, "content"); + content_root = json_node_get_object(content_node); + + state_key = matrix_event_state_get_state_key(MATRIX_EVENT_STATE(matrix_event_base)); + + if ((state_key == NULL) || (*state_key == 0)) { + g_set_error(error, MATRIX_ERROR, MATRIX_ERROR_INCOMPLETE, + "Won't generate a m.room.guest_access event with a non-empty state key"); + + return; + } + + if (priv->_guest_access == MATRIX_GUEST_ACCESS_UNKNOWN) { + g_set_error(error, MATRIX_ERROR, MATRIX_ERROR_UNKNOWN_VALUE, + "Won't generate a m.room.guest_access event with an unknown content.guest_access key"); + + return; + } + + guest_access = _matrix_g_enum_to_string(MATRIX_TYPE_GUEST_ACCESS, priv->_guest_access, '_'); + json_object_set_string_member(content_root, "guest_access", guest_access); + g_free(guest_access); + + MATRIX_EVENT_BASE_CLASS(matrix_event_room_guest_access_parent_class)->to_json(matrix_event_base, json_data, error); +} + +/** + * matrix_event_room_guest_access_new: + * + * Create a new #MatrixEventRoomGuestAccess object. + * + * Returns: (transfer full): a new #MatrixEventRoomGuestAccess object + */ +MatrixEventRoomGuestAccess * +matrix_event_room_guest_access_new(void) +{ + return (MatrixEventRoomGuestAccess *)matrix_event_state_construct(MATRIX_EVENT_TYPE_ROOM_GUEST_ACCESS); +} + +/** + * matrix_event_room_guest_access_get_guest_access: + * @event: a #MatrixEventRoomGuestAccess + * + * Get the guest access status of the room from @event. + * + * Returns: the guest access status + */ +MatrixGuestAccess +matrix_event_room_guest_access_get_guest_access(MatrixEventRoomGuestAccess *matrix_event_room_guest_access) +{ + MatrixEventRoomGuestAccessPrivate *priv; + + g_return_val_if_fail(matrix_event_room_guest_access != NULL, 0); + + priv = matrix_event_room_guest_access_get_instance_private(matrix_event_room_guest_access); + + return priv->_guest_access; +} + +/** + * matrix_event_room_guest_access_set_guest_access: + * @event: a #MatrixEventRoomGuestAccess + * @guest_access: a #MatrixGuestAccess value + * + * Set the guest access status for the room in @event. + */ +void +matrix_event_room_guest_access_set_guest_access(MatrixEventRoomGuestAccess *matrix_event_room_guest_access, MatrixGuestAccess guest_access) +{ + MatrixEventRoomGuestAccessPrivate *priv; + + g_return_if_fail(matrix_event_room_guest_access != NULL); + + priv = matrix_event_room_guest_access_get_instance_private(matrix_event_room_guest_access); + + if (priv->_guest_access != guest_access) { + priv->_guest_access = guest_access; + + g_object_notify_by_pspec((GObject *)matrix_event_room_guest_access, matrix_event_room_guest_access_properties[PROP_GUEST_ACCESS]); + } +} + +static void +matrix_event_room_guest_access_finalize (GObject *gobject) +{ + G_OBJECT_CLASS(matrix_event_room_guest_access_parent_class)->finalize(gobject); +} + +static void +matrix_event_room_guest_access_get_property(GObject *gobject, guint property_id, GValue *value, GParamSpec *pspec) +{ + MatrixEventRoomGuestAccess *matrix_event_room_guest_access = MATRIX_EVENT_ROOM_GUEST_ACCESS(gobject); + + switch (property_id) { + case PROP_GUEST_ACCESS: + g_value_set_enum(value, matrix_event_room_guest_access_get_guest_access(matrix_event_room_guest_access)); + + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); + + break; + } +} + +static void +matrix_event_room_guest_access_set_property(GObject *gobject, guint property_id, const GValue *value, GParamSpec *pspec) { + MatrixEventRoomGuestAccess *matrix_event_room_guest_access = MATRIX_EVENT_ROOM_GUEST_ACCESS(gobject); + + switch (property_id) { + case PROP_GUEST_ACCESS: + matrix_event_room_guest_access_set_guest_access(matrix_event_room_guest_access, g_value_get_enum(value)); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); + break; + } +} + +static void +matrix_event_room_guest_access_class_init(MatrixEventRoomGuestAccessClass *klass) +{ + ((MatrixEventBaseClass *)klass)->from_json = matrix_event_room_guest_access_real_from_json; + ((MatrixEventBaseClass *)klass)->to_json = matrix_event_room_guest_access_real_to_json; + G_OBJECT_CLASS(klass)->get_property = matrix_event_room_guest_access_get_property; + G_OBJECT_CLASS(klass)->set_property = matrix_event_room_guest_access_set_property; + G_OBJECT_CLASS(klass)->finalize = matrix_event_room_guest_access_finalize; + + /** + * MatrixEventRoomGuestAccess:guest-access: + * + * Whether guests can join the room. + */ + matrix_event_room_guest_access_properties[PROP_GUEST_ACCESS] = g_param_spec_enum( + "guest-access", "guest-access", "guest-access", + MATRIX_TYPE_GUEST_ACCESS, MATRIX_GUEST_ACCESS_UNKNOWN, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); + g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_GUEST_ACCESS, matrix_event_room_guest_access_properties[PROP_GUEST_ACCESS]); +} + +static void +matrix_event_room_guest_access_init(MatrixEventRoomGuestAccess *matrix_event_room_guest_access) +{ + MatrixEventRoomGuestAccessPrivate *priv = matrix_event_room_guest_access_get_instance_private(matrix_event_room_guest_access); + + priv->_guest_access = MATRIX_GUEST_ACCESS_UNKNOWN; +} diff --git a/src/matrix-event-room-guest-access.h b/src/matrix-event-room-guest-access.h new file mode 100644 index 0000000..cefe2a8 --- /dev/null +++ b/src/matrix-event-room-guest-access.h @@ -0,0 +1,41 @@ +/* + * 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 + * . + */ + +#ifndef __MATRIX_GLIB_SDK_EVENT_ROOM_GUEST_ACCESS_H__ +# define __MATRIX_GLIB_SDK_EVENT_ROOM_GUEST_ACCESS_H__ + +# include +# include "matrix-event-state-base.h" +# include "matrix-types.h" + +G_BEGIN_DECLS + +#define MATRIX_EVENT_TYPE_ROOM_GUEST_ACCESS (matrix_event_room_guest_access_get_type ()) +G_DECLARE_DERIVABLE_TYPE(MatrixEventRoomGuestAccess, matrix_event_room_guest_access, MATRIX_EVENT, ROOM_GUEST_ACCESS, MatrixEventState) + +struct _MatrixEventRoomGuestAccessClass { + MatrixEventStateClass parent_class; +}; + +MatrixEventRoomGuestAccess *matrix_event_room_guest_access_new(void); +MatrixGuestAccess matrix_event_room_guest_access_get_guest_access(MatrixEventRoomGuestAccess *event); +void matrix_event_room_guest_access_set_guest_access(MatrixEventRoomGuestAccess *event, MatrixGuestAccess guest_access); + +G_END_DECLS + +#endif /* __MATRIX_GLIB_SDK_EVENT_ROOM_GUEST_ACCESS_H__ */ diff --git a/src/matrix-event-room-guest-access.vala b/src/matrix-event-room-guest-access.vala deleted file mode 100644 index c178838..0000000 --- a/src/matrix-event-room-guest-access.vala +++ /dev/null @@ -1,92 +0,0 @@ -/* - * 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 = GuestAccess.UNKNOWN; } - - 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 = null; - - if (Config.DEBUG && ((node = root.get_member("state_key")) != null)) { - if (node.get_string() != "") { - warning("state_key of a m.room.guest_access is non-empty"); - } - } - - if ((node = content_root.get_member("guest_access")) != null) { - try { - _guest_access = (GuestAccess)_g_enum_nick_to_value( - typeof(GuestAccess), node.get_string()); - } catch (Matrix.Error e) { - _guest_access = GuestAccess.UNKNOWN; - - if (Config.DEBUG) { - 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 (_state_key != "") { - throw new Matrix.Error.INCOMPLETE( - "Won't generate a m.room.guest_access event with a non-empty state key"); - } - - if ((_guest_access == GuestAccess.UNKNOWN)) { - throw new Matrix.Error.INCOMPLETE( - "Won't generate a m.room.guest_access event with an unknown 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 c98f017..cf9261a 100644 --- a/src/matrix-event-types.c +++ b/src/matrix-event-types.c @@ -33,6 +33,7 @@ #include "matrix-event-room-canonical-alias.h" #include "matrix-event-room-create.h" #include "matrix-event-room-power-levels.h" +#include "matrix-event-room-guest-access.h" #include "matrix-event-room-message-feedback.h" #include "matrix-event-call-answer.h" #include "matrix-event-call-hangup.h" diff --git a/vapi/c-api.vapi b/vapi/c-api.vapi index f7cef2b..e6f96db 100644 --- a/vapi/c-api.vapi +++ b/vapi/c-api.vapi @@ -833,6 +833,17 @@ namespace Matrix { protected override void to_json(Json.Node json_data) throws Matrix.Error; } + + [CCode (cheader_filename = "matrix-event-room-guest-access.h")] + public class RoomGuestAccess : State { + public Matrix.GuestAccess guest_access { get; set; default = Matrix.GuestAccess.UNKNOWN; } + + protected override void from_json(Json.Node json_data) + throws Matrix.Error; + + protected override void to_json(Json.Node json_data) + throws Matrix.Error; + } } [CCode (gir_namespace = "MatrixMessage", gir_version = "0.0")]