From f4eb461431f5c43d33fe6a7755a37b066974e533 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Wed, 22 Nov 2017 11:24:53 +0100 Subject: [PATCH] Port MatrixEventRoomRedaction to C --- .gitignore | 1 - src/Makefile.am | 3 +- src/matrix-event-room-redaction.c | 278 +++++++++++++++++++++++++++ src/matrix-event-room-redaction.h | 42 ++++ src/matrix-event-room-redaction.vala | 85 -------- src/matrix-event-types.c | 1 + vapi/c-api.vapi | 12 ++ 7 files changed, 335 insertions(+), 87 deletions(-) create mode 100644 src/matrix-event-room-redaction.c create mode 100644 src/matrix-event-room-redaction.h delete mode 100644 src/matrix-event-room-redaction.vala diff --git a/.gitignore b/.gitignore index fa57828..e5abac9 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-redaction.c /src/matrix-event-room-third-party-invite.c /src/matrix-event-call-candidates.c /src/matrix-glib-0.0.pc diff --git a/src/Makefile.am b/src/Makefile.am index 8fc75e8..133a760 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-redaction.vala \ matrix-event-room-third-party-invite.vala \ matrix-event-call-candidates.vala \ $(NULL) @@ -110,6 +109,7 @@ INST_H_SRC_FILES = \ matrix-event-room-power-levels.h \ matrix-event-room-guest-access.h \ matrix-event-room-message-feedback.h \ + matrix-event-room-redaction.h \ matrix-event-typing.h \ matrix-event-receipt.h \ utils.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-redaction.c \ matrix-event-room-guest-access.c \ matrix-profile.c \ matrix-room.c \ diff --git a/src/matrix-event-room-redaction.c b/src/matrix-event-room-redaction.c new file mode 100644 index 0000000..6531d7e --- /dev/null +++ b/src/matrix-event-room-redaction.c @@ -0,0 +1,278 @@ +/* + * 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-redaction.h" +#include "matrix-types.h" + +/** + * SECTION:matrix-event-room-redaction + * @short_description: event to designate event redactions + * + * This is the default event handler for `m.room.redaction` events. + * + * Events can be redacted by either room or server admins. Redacting an event means that all + * keys not required by the protocol are stripped off, allowing admins to remove offensive or + * illegal content that may have been attached to any event. This cannot be undone, allowing + * server owners to physically delete the offending data. There is also a concept of a + * moderator hiding a message event, which can be undone, but cannot be applied to state events. + * The event that has been redacted is specified in the `redacts` event level key. + */ +enum { + PROP_0, + PROP_REASON, + PROP_REDACTED_EVENT_ID, + NUM_PROPERTIES +}; + +static GParamSpec *matrix_event_room_redaction_properties[NUM_PROPERTIES]; + +typedef struct { + gchar* _reason; + gchar* _redacted_event_id; +} MatrixEventRoomRedactionPrivate; + +/** + * MatrixEventRoomRedaction: + */ +G_DEFINE_TYPE_WITH_PRIVATE(MatrixEventRoomRedaction, matrix_event_room_redaction, MATRIX_EVENT_TYPE_ROOM); + +static void +matrix_event_room_redaction_real_from_json(MatrixEventBase *matrix_event_base, JsonNode *json_data, GError **error) +{ + MatrixEventRoomRedactionPrivate *priv; + JsonObject *root; + JsonObject *content_root; + JsonNode *content_node; + JsonNode *node; + + g_return_if_fail (json_data != NULL); + + priv = matrix_event_room_redaction_get_instance_private(MATRIX_EVENT_ROOM_REDACTION(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 ((node = json_object_get_member(content_root, "reason")) != NULL) { + g_free(priv->_reason); + priv->_reason = g_strdup(json_node_get_string(node)); + } + + if ((node = json_object_get_member(root, "redacts")) != NULL) { + g_free(priv->_redacted_event_id); + priv->_redacted_event_id = g_strdup(json_node_get_string(node)); + } else { + g_warning("redacts is missing from a m.room.redaction event"); + } + + MATRIX_EVENT_BASE_CLASS(matrix_event_room_redaction_parent_class)->from_json(matrix_event_base, json_data, error); +} + +static void +matrix_event_room_redaction_real_to_json(MatrixEventBase *matrix_event_base, JsonNode *json_data, GError **error) +{ + MatrixEventRoomRedactionPrivate *priv; + JsonObject *root; + JsonObject *content_root; + JsonNode *content_node; + + g_return_if_fail (json_data != NULL); + + priv = matrix_event_room_redaction_get_instance_private(MATRIX_EVENT_ROOM_REDACTION(matrix_event_base)); + root = json_node_get_object(json_data); + + if (priv->_redacted_event_id == NULL) { + g_set_error(error, MATRIX_ERROR, MATRIX_ERROR_INCOMPLETE, + "Won't generate a m.room.redaction event without /* TODO: */he redacts field"); + + return; + } + + json_object_set_string_member(root, "redacts", priv->_redacted_event_id); + + if (priv->_reason != NULL) { + content_node = json_object_get_member(root, "content"); + content_root = json_node_get_object(content_node); + + json_object_set_string_member(content_root, "reason", priv->_reason); + } + + MATRIX_EVENT_BASE_CLASS(matrix_event_room_redaction_parent_class)->to_json(matrix_event_base, json_data, error); +} + +/** + * matrix_event_room_redaction_new: + * + * Create a new #MatrixEventRoomRedaction object. + * + * Returns: (transfer full): a new #MatrixEventRoomRedaction object + */ +MatrixEventRoomRedaction * +matrix_event_room_redaction_new(void) +{ + return (MatrixEventRoomRedaction *)matrix_event_room_construct(MATRIX_EVENT_TYPE_ROOM_REDACTION); +} + +const gchar * +matrix_event_room_redaction_get_reason(MatrixEventRoomRedaction *matrix_event_room_redaction) +{ + MatrixEventRoomRedactionPrivate *priv; + + g_return_val_if_fail(matrix_event_room_redaction != NULL, NULL); + + priv = matrix_event_room_redaction_get_instance_private(matrix_event_room_redaction); + + return priv->_reason; +} + +void +matrix_event_room_redaction_set_reason(MatrixEventRoomRedaction *matrix_event_room_redaction, const gchar *reason) +{ + MatrixEventRoomRedactionPrivate *priv; + + g_return_if_fail(matrix_event_room_redaction != NULL); + + priv = matrix_event_room_redaction_get_instance_private(matrix_event_room_redaction); + + if (g_strcmp0(reason, priv->_reason) != 0) { + g_free(priv->_reason); + priv->_reason = g_strdup(reason); + + g_object_notify_by_pspec((GObject *)matrix_event_room_redaction, matrix_event_room_redaction_properties[PROP_REASON]); + } +} + +const gchar * +matrix_event_room_redaction_get_redacted_event_id(MatrixEventRoomRedaction *matrix_event_room_redaction) +{ + MatrixEventRoomRedactionPrivate *priv; + + g_return_val_if_fail(matrix_event_room_redaction != NULL, NULL); + + priv = matrix_event_room_redaction_get_instance_private(matrix_event_room_redaction); + + return priv->_redacted_event_id; +} + +void +matrix_event_room_redaction_set_redacted_event_id(MatrixEventRoomRedaction *matrix_event_room_redaction, const gchar *redacted_event_id) +{ + MatrixEventRoomRedactionPrivate *priv; + + g_return_if_fail(matrix_event_room_redaction != NULL); + + priv = matrix_event_room_redaction_get_instance_private(matrix_event_room_redaction); + + if (g_strcmp0(redacted_event_id, priv->_redacted_event_id) != 0) { + g_free(priv->_redacted_event_id); + priv->_redacted_event_id = g_strdup(redacted_event_id); + + g_object_notify_by_pspec((GObject *)matrix_event_room_redaction, matrix_event_room_redaction_properties[PROP_REDACTED_EVENT_ID]); + } +} + +static void +matrix_event_room_redaction_finalize(GObject *gobject) +{ + MatrixEventRoomRedactionPrivate *priv = matrix_event_room_redaction_get_instance_private(MATRIX_EVENT_ROOM_REDACTION(gobject)); + + g_free(priv->_reason); + g_free(priv->_redacted_event_id); + + G_OBJECT_CLASS(matrix_event_room_redaction_parent_class)->finalize(gobject); +} + +static void +matrix_event_room_redaction_get_property(GObject *gobject, guint property_id, GValue *value, GParamSpec *pspec) +{ + MatrixEventRoomRedaction *matrix_event_room_redaction = MATRIX_EVENT_ROOM_REDACTION(gobject); + + switch (property_id) { + case PROP_REASON: + g_value_set_string(value, matrix_event_room_redaction_get_reason(matrix_event_room_redaction)); + + break; + case PROP_REDACTED_EVENT_ID: + g_value_set_string(value, matrix_event_room_redaction_get_redacted_event_id(matrix_event_room_redaction)); + + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); + break; + } +} + +static void +matrix_event_room_redaction_set_property(GObject *gobject, guint property_id, const GValue *value, GParamSpec *pspec) +{ + MatrixEventRoomRedaction *matrix_event_room_redaction = MATRIX_EVENT_ROOM_REDACTION(gobject); + + switch (property_id) { + case PROP_REASON: + matrix_event_room_redaction_set_reason(matrix_event_room_redaction, g_value_get_string(value)); + + break; + case PROP_REDACTED_EVENT_ID: + matrix_event_room_redaction_set_redacted_event_id(matrix_event_room_redaction, g_value_get_string(value)); + + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); + + break; + } +} + +static void +matrix_event_room_redaction_class_init(MatrixEventRoomRedactionClass *klass) +{ + ((MatrixEventBaseClass *)klass)->from_json = matrix_event_room_redaction_real_from_json; + ((MatrixEventBaseClass *)klass)->to_json = matrix_event_room_redaction_real_to_json; + G_OBJECT_CLASS(klass)->get_property = matrix_event_room_redaction_get_property; + G_OBJECT_CLASS(klass)->set_property = matrix_event_room_redaction_set_property; + G_OBJECT_CLASS(klass)->finalize = matrix_event_room_redaction_finalize; + + /** + * MatrixEventRoomRedaction:reason: + * + * The reason for the redaction, if any. + */ + matrix_event_room_redaction_properties[PROP_REASON] = g_param_spec_string( + "reason", "reason", "reason", + NULL, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); + g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_REASON, matrix_event_room_redaction_properties[PROP_REASON]); + + /** + * MatrixEventRoomRedaction:redacted-event-id: + * + * The event ID that was redacted. + */ + matrix_event_room_redaction_properties[PROP_REDACTED_EVENT_ID] = g_param_spec_string( + "redacted-event-id", "redacted-event-id", "redacted-event-id", + NULL, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); + g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_REDACTED_EVENT_ID, matrix_event_room_redaction_properties[PROP_REDACTED_EVENT_ID]); +} + +static void +matrix_event_room_redaction_init(MatrixEventRoomRedaction *matrix_event_room_redaction) { + MatrixEventRoomRedactionPrivate *priv = matrix_event_room_redaction_get_instance_private(matrix_event_room_redaction); + + priv->_reason = NULL; + priv->_redacted_event_id = NULL; +} diff --git a/src/matrix-event-room-redaction.h b/src/matrix-event-room-redaction.h new file mode 100644 index 0000000..20ac7c2 --- /dev/null +++ b/src/matrix-event-room-redaction.h @@ -0,0 +1,42 @@ +/* + * 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_REDACTION_H__ +# define __MATRIX_GLIB_SDK_EVENT_ROOM_REDACTION_H__ + +# include +# include "matrix-event-room-base.h" + +G_BEGIN_DECLS + +#define MATRIX_EVENT_TYPE_ROOM_REDACTION (matrix_event_room_redaction_get_type ()) +G_DECLARE_DERIVABLE_TYPE(MatrixEventRoomRedaction, matrix_event_room_redaction, MATRIX_EVENT, ROOM_REDACTION, MatrixEventRoom) + +struct _MatrixEventRoomRedactionClass { + MatrixEventRoomClass parent_class; +}; + +MatrixEventRoomRedaction* matrix_event_room_redaction_new (void); +const gchar* matrix_event_room_redaction_get_reason (MatrixEventRoomRedaction* self); +void matrix_event_room_redaction_set_reason (MatrixEventRoomRedaction* self, const gchar* value); +const gchar* matrix_event_room_redaction_get_redacted_event_id (MatrixEventRoomRedaction* self); +void matrix_event_room_redaction_set_redacted_event_id (MatrixEventRoomRedaction* self, const gchar* value); + +G_END_DECLS + +#endif /* __MATRIX_GLIB_SDK_EVENT_ROOM_REDACTION_H__ */ diff --git a/src/matrix-event-room-redaction.vala b/src/matrix-event-room-redaction.vala deleted file mode 100644 index dff18ca..0000000 --- a/src/matrix-event-room-redaction.vala +++ /dev/null @@ -1,85 +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.redaction event - * - * Events can be redacted by either room or server admins. Redacting - * an event means that all keys not required by the protocol are - * stripped off, allowing admins to remove offensive or illegal - * content that may have been attached to any event. This cannot be - * undone, allowing server owners to physically delete the offending - * data. There is also a concept of a moderator hiding a message - * event, which can be undone, but cannot be applied to state - * events. The event that has been redacted is specified in the - * redacts event level key. - */ -public class Matrix.Event.RoomRedaction : Matrix.Event.Room { - /** - * The reason for the redaction, if any. - */ - public string? reason { get; set; default = null; } - - /** - * The event ID that was redacted. - */ - public string? redacted_event_id { get; set; default = null; } - - 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; - - if ((node = content_root.get_member("reason")) != null) { - _reason = node.get_string(); - } - - if ((node = root.get_member("redacts")) != null) { - _redacted_event_id = node.get_string(); - } else { - warning("redacts is missing from a m.room.redaction event"); - } - - base.from_json(json_data); - } - - protected override void - to_json(Json.Node json_data) - throws Matrix.Error - { - if (_redacted_event_id == null) { - throw new Matrix.Error.INCOMPLETE( - "Won't generate a m.room.redaction event without the redacts field"); - } - - var root = json_data.get_object(); - - root.set_string_member("redacts", _redacted_event_id); - - if (_reason != null) { - var content_root = root.get_member("content").get_object(); - - content_root.set_string_member("reason", _reason); - } - - base.to_json(json_data); - } -} diff --git a/src/matrix-event-types.c b/src/matrix-event-types.c index cf9261a..b629c98 100644 --- a/src/matrix-event-types.c +++ b/src/matrix-event-types.c @@ -35,6 +35,7 @@ #include "matrix-event-room-power-levels.h" #include "matrix-event-room-guest-access.h" #include "matrix-event-room-message-feedback.h" +#include "matrix-event-room-redaction.h" #include "matrix-event-call-answer.h" #include "matrix-event-call-hangup.h" #include "matrix-event-call-invite.h" diff --git a/vapi/c-api.vapi b/vapi/c-api.vapi index e6f96db..76933d1 100644 --- a/vapi/c-api.vapi +++ b/vapi/c-api.vapi @@ -844,6 +844,18 @@ namespace Matrix { protected override void to_json(Json.Node json_data) throws Matrix.Error; } + + [CCode (cheader_filename = "matrix-event-room-redaction.h")] + public class RoomRedaction : Room { + public string? reason { get; set; default = null; } + public string? redacted_event_id { get; set; default = null; } + + 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")]