From cba50735497879b169d8d4c655dd8a35565544d0 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Wed, 22 Nov 2017 10:37:06 +0100 Subject: [PATCH] Port MatrixEventRoomMessageFeedback to C --- .gitignore | 1 - src/Makefile.am | 3 +- src/matrix-event-room-message-feedback.c | 312 ++++++++++++++++++++ src/matrix-event-room-message-feedback.h | 42 +++ src/matrix-event-room-message-feedback.vala | 84 ------ src/matrix-event-types.c | 1 + vapi/c-api.vapi | 12 + 7 files changed, 369 insertions(+), 86 deletions(-) create mode 100644 src/matrix-event-room-message-feedback.c create mode 100644 src/matrix-event-room-message-feedback.h delete mode 100644 src/matrix-event-room-message-feedback.vala diff --git a/.gitignore b/.gitignore index bda09ba..31be807 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-message-feedback.c /src/matrix-event-room-guest-access.c /src/matrix-event-room-redaction.c /src/matrix-event-room-third-party-invite.c diff --git a/src/Makefile.am b/src/Makefile.am index 8bd5806..80bb6c6 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-message-feedback.vala \ matrix-event-room-guest-access.vala \ matrix-event-room-redaction.vala \ matrix-event-room-third-party-invite.vala \ @@ -110,6 +109,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-message-feedback.h \ matrix-event-typing.h \ matrix-event-receipt.h \ utils.h \ @@ -166,6 +166,7 @@ libmatrix_glib_0_0_la_SOURCES = \ matrix-event-room-canonical-alias.c \ matrix-event-room-create.c \ matrix-event-room-power-levels.c \ + matrix-event-room-message-feedback.c \ matrix-profile.c \ matrix-room.c \ utils.c \ diff --git a/src/matrix-event-room-message-feedback.c b/src/matrix-event-room-message-feedback.c new file mode 100644 index 0000000..2bf7b85 --- /dev/null +++ b/src/matrix-event-room-message-feedback.c @@ -0,0 +1,312 @@ +/* + * 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-message-feedback.h" +#include "matrix-types.h" + +/** + * SECTION:matrix-event-room-message-feedback + * @short_description: event to hold message feedbacks + * + * This is the default event handler for `m.room.message.feedback` events. + * + * Usage of this event is discouraged in favour of the receipts module. Most clients will not + * recognise this event. + * + * Feedback events are events sent to acknowledge a message in some way. There are two + * supported acknowledgements: `delivered` (sent when the event has been received) and `read` + * (sent when the event has been observed by the end-user). The `target_event_id` should + * reference the `m.room.message` event being acknowledged. + */ +enum { + PROP_0, + PROP_FEEDBACK_TYPE, + PROP_TARGET_EVENT_ID, + NUM_PROPERTIES +}; + +static GParamSpec* matrix_event_room_message_feedback_properties[NUM_PROPERTIES]; + +typedef struct { + gchar* _feedback_type; + gchar* _target_event_id; +} MatrixEventRoomMessageFeedbackPrivate; + +/** + * MatrixEventRoomMessageFeedback: + */ +G_DEFINE_TYPE_WITH_PRIVATE(MatrixEventRoomMessageFeedback, matrix_event_room_message_feedback, MATRIX_EVENT_TYPE_ROOM); + +static void +matrix_event_room_message_feedback_real_from_json(MatrixEventBase *matrix_event_base, JsonNode *json_data, GError **error) +{ + MatrixEventRoomMessageFeedbackPrivate *priv; + JsonObject *root; + JsonObject *content_root; + JsonNode *content_node; + JsonNode *node; + + g_return_if_fail(json_data != NULL); + + priv = matrix_event_room_message_feedback_get_instance_private(MATRIX_EVENT_ROOM_MESSAGE_FEEDBACK(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, "type")) != NULL) { + g_free(priv->_feedback_type); + priv->_feedback_type = g_strdup(json_node_get_string(node)); + } else { + g_warning("content.type is missing from a m.room.message.feedback event"); + } + + if ((node = json_object_get_member(content_root, "target_event_id")) != NULL) { + g_free(priv->_target_event_id); + priv->_target_event_id = g_strdup(json_node_get_string(node)); + } else { + g_warning("content.target_event_id is missing from a m.room.message.feedback event"); + } + + MATRIX_EVENT_BASE_CLASS(matrix_event_room_message_feedback_parent_class)->from_json(matrix_event_base, json_data, error); +} + +static void +matrix_event_room_message_feedback_real_to_json(MatrixEventBase *matrix_event_base, JsonNode *json_data, GError **error) +{ + MatrixEventRoomMessageFeedbackPrivate *priv; + JsonObject *root; + JsonObject *content_root; + JsonNode *content_node; + + g_return_if_fail(json_data != NULL); + + priv = matrix_event_room_message_feedback_get_instance_private(MATRIX_EVENT_ROOM_MESSAGE_FEEDBACK(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 ((priv->_feedback_type == NULL) || (priv->_target_event_id == NULL)) { + g_set_error(error, MATRIX_ERROR, MATRIX_ERROR_INCOMPLETE, + "Won't generate a m.room.message.feedback without all fields set"); + + return; + } + + json_object_set_string_member(content_root, "type", priv->_feedback_type); + json_object_set_string_member(content_root, "target_event_id", priv->_target_event_id); + + MATRIX_EVENT_BASE_CLASS(matrix_event_room_message_feedback_parent_class)->to_json(matrix_event_base, json_data, error); +} + +/** + * matrix_event_room_message_feedback_new: + * + * Create a new #MatrixEventRoomMessageFeedback object. + * + * Returns: (transfer full): a new #MatrixEventRoomMessageFeedback object + */ +MatrixEventRoomMessageFeedback * +matrix_event_room_message_feedback_new(void) +{ + return (MatrixEventRoomMessageFeedback *)matrix_event_room_construct(MATRIX_EVENT_TYPE_ROOM_MESSAGE_FEEDBACK); +} + +/** + * matrix_event_room_message_feedback_get_feedback_type: + * @event: a #MatrixEventRoomMessageFeedback + * + * Get the feedback type from @event. + * + * The returned value is owned by @event and should not be freed. + * + * Returns: (transfer none) (nullable): the feedback type + */ +const gchar * +matrix_event_room_message_feedback_get_feedback_type(MatrixEventRoomMessageFeedback *matrix_event_room_message_feedback) +{ + MatrixEventRoomMessageFeedbackPrivate *priv; + + g_return_val_if_fail(matrix_event_room_message_feedback != NULL, NULL); + + priv = matrix_event_room_message_feedback_get_instance_private(matrix_event_room_message_feedback); + + return priv->_feedback_type; +} + +/** + * matrix_event_room_message_feedback_set_feedback_type: + * @event: a #MatrixEventRoomMessageFeedback + * @feedback_type: (transfer none) (nullable): a feedback type + * + * Set the feedback type in @event. + */ +void +matrix_event_room_message_feedback_set_feedback_type(MatrixEventRoomMessageFeedback *matrix_event_room_message_feedback, const gchar *feedback_type) +{ + MatrixEventRoomMessageFeedbackPrivate *priv; + + g_return_if_fail(matrix_event_room_message_feedback != NULL); + + priv = matrix_event_room_message_feedback_get_instance_private(matrix_event_room_message_feedback); + + if (g_strcmp0(feedback_type, priv->_feedback_type) != 0) { + g_free(priv->_feedback_type); + priv->_feedback_type = g_strdup(feedback_type); + + g_object_notify_by_pspec((GObject *)matrix_event_room_message_feedback, matrix_event_room_message_feedback_properties[PROP_FEEDBACK_TYPE]); + } +} + +/** + * matrix_event_room_message_feedback_get_target_event_id: + * @event: a #MatrixEventRoomMessageFeedback + * + * Get the target event’s ID from @event. + * + * The returned value is owned by @event and should not be freed. + * + * Returns: (transfer none) (nullable): the ID of the target event + */ +const gchar * +matrix_event_room_message_feedback_get_target_event_id(MatrixEventRoomMessageFeedback *matrix_event_room_message_feedback) +{ + MatrixEventRoomMessageFeedbackPrivate *priv; + + g_return_val_if_fail(matrix_event_room_message_feedback != NULL, NULL); + + priv = matrix_event_room_message_feedback_get_instance_private(matrix_event_room_message_feedback); + return priv->_target_event_id; +} + +/** + * matrix_event_room_message_feedback_set_target_event_id: + * @event: a #MatrixEventRoomMessageFeedback + * @target_event_id: the ID of the target event + * + * Set the ID of the target event in @event. + */ +void +matrix_event_room_message_feedback_set_target_event_id(MatrixEventRoomMessageFeedback *matrix_event_room_message_feedback, const gchar *target_event_id) +{ + MatrixEventRoomMessageFeedbackPrivate *priv; + + g_return_if_fail(matrix_event_room_message_feedback != NULL); + + priv = matrix_event_room_message_feedback_get_instance_private(matrix_event_room_message_feedback); + + if (g_strcmp0(target_event_id, priv->_target_event_id) != 0) { + g_free(priv->_target_event_id); + priv->_target_event_id = g_strdup(target_event_id); + + g_object_notify_by_pspec((GObject *)matrix_event_room_message_feedback, matrix_event_room_message_feedback_properties[PROP_TARGET_EVENT_ID]); + } +} + +static void +matrix_event_room_message_feedback_finalize(GObject *gobject) +{ + MatrixEventRoomMessageFeedbackPrivate *priv = matrix_event_room_message_feedback_get_instance_private(MATRIX_EVENT_ROOM_MESSAGE_FEEDBACK(gobject)); + + g_free(priv->_feedback_type); + g_free(priv->_target_event_id); + + G_OBJECT_CLASS(matrix_event_room_message_feedback_parent_class)->finalize(gobject); +} + +static void +matrix_event_room_message_feedback_get_property(GObject *gobject, guint property_id, GValue *value, GParamSpec *pspec) +{ + MatrixEventRoomMessageFeedback *matrix_event_room_message_feedback = MATRIX_EVENT_ROOM_MESSAGE_FEEDBACK(gobject); + + switch (property_id) { + case PROP_FEEDBACK_TYPE: + g_value_set_string(value, matrix_event_room_message_feedback_get_feedback_type(matrix_event_room_message_feedback)); + + break; + case PROP_TARGET_EVENT_ID: + g_value_set_string(value, matrix_event_room_message_feedback_get_target_event_id(matrix_event_room_message_feedback)); + + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); + + break; + } +} + +static void +matrix_event_room_message_feedback_set_property(GObject *gobject, guint property_id, const GValue *value, GParamSpec *pspec) +{ + MatrixEventRoomMessageFeedback *matrix_event_room_message_feedback = MATRIX_EVENT_ROOM_MESSAGE_FEEDBACK(gobject); + + switch (property_id) { + case PROP_FEEDBACK_TYPE: + matrix_event_room_message_feedback_set_feedback_type(matrix_event_room_message_feedback, g_value_get_string(value)); + + break; + case PROP_TARGET_EVENT_ID: + matrix_event_room_message_feedback_set_target_event_id(matrix_event_room_message_feedback, g_value_get_string(value)); + + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); + break; + } +} + +static void +matrix_event_room_message_feedback_class_init(MatrixEventRoomMessageFeedbackClass *klass) +{ + ((MatrixEventBaseClass *)klass)->from_json = matrix_event_room_message_feedback_real_from_json; + ((MatrixEventBaseClass *)klass)->to_json = matrix_event_room_message_feedback_real_to_json; + G_OBJECT_CLASS(klass)->get_property = matrix_event_room_message_feedback_get_property; + G_OBJECT_CLASS(klass)->set_property = matrix_event_room_message_feedback_set_property; + G_OBJECT_CLASS(klass)->finalize = matrix_event_room_message_feedback_finalize; + + /** + * MatrixEventRoomMessageFeedback:feedback-type: + * + * The type of the feedback. As the use of this event type is discouraged, this SDK doesn’t + * implement this as an actual enum, but the only recognised values are `received` and `read`. + */ + matrix_event_room_message_feedback_properties[PROP_FEEDBACK_TYPE] = g_param_spec_string( + "feedback-type", "feedback-type", "feedback-type", + NULL, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); + g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_FEEDBACK_TYPE, matrix_event_room_message_feedback_properties[PROP_FEEDBACK_TYPE]); + + /** + * MatrixEventRoomMessageFeedback:target-event-id: + * + * The event that this feedback is related to. + */ + matrix_event_room_message_feedback_properties[PROP_TARGET_EVENT_ID] = g_param_spec_string( + "target-event-id", "target-event-id", "target-event-id", + NULL, + G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE); + g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_TARGET_EVENT_ID, matrix_event_room_message_feedback_properties[PROP_TARGET_EVENT_ID]); +} + +static void +matrix_event_room_message_feedback_init(MatrixEventRoomMessageFeedback *matrix_event_room_message_feedback) +{ + MatrixEventRoomMessageFeedbackPrivate *priv = matrix_event_room_message_feedback_get_instance_private(matrix_event_room_message_feedback); + + priv->_feedback_type = NULL; + priv->_target_event_id = NULL; +} diff --git a/src/matrix-event-room-message-feedback.h b/src/matrix-event-room-message-feedback.h new file mode 100644 index 0000000..82a290a --- /dev/null +++ b/src/matrix-event-room-message-feedback.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_MESSAGE_FEEDBACK_H__ +# define __MATRIX_GLIB_SDK_EVENT_ROOM_MESSAGE_FEEDBACK_H__ + +# include +# include "matrix-event-room-base.h" + +G_BEGIN_DECLS + +#define MATRIX_EVENT_TYPE_ROOM_MESSAGE_FEEDBACK (matrix_event_room_message_feedback_get_type ()) +G_DECLARE_DERIVABLE_TYPE(MatrixEventRoomMessageFeedback, matrix_event_room_message_feedback, MATRIX_EVENT, ROOM_MESSAGE_FEEDBACK, MatrixEventRoom) + +struct _MatrixEventRoomMessageFeedbackClass { + MatrixEventRoomClass parent_class; +}; + +MatrixEventRoomMessageFeedback *matrix_event_room_message_feedback_new(void); +const gchar *matrix_event_room_message_feedback_get_feedback_type(MatrixEventRoomMessageFeedback *event); +void matrix_event_room_message_feedback_set_feedback_type(MatrixEventRoomMessageFeedback *event, const gchar *feedback_type); +const gchar *matrix_event_room_message_feedback_get_target_event_id(MatrixEventRoomMessageFeedback *event); +void matrix_event_room_message_feedback_set_target_event_id(MatrixEventRoomMessageFeedback *event, const gchar *target_event_id); + +G_END_DECLS + +#endif /* __MATRIX_GLIB_SDK_EVENT_ROOM_MESSAGE_FEEDBACK_H__ */ diff --git a/src/matrix-event-room-message-feedback.vala b/src/matrix-event-room-message-feedback.vala deleted file mode 100644 index 6c38713..0000000 --- a/src/matrix-event-room-message-feedback.vala +++ /dev/null @@ -1,84 +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.message.feedback event. - * - * Usage of this event is discouraged in favour of the receipts - * module. Most clients will not recognise this event. - * - * Feedback events are events sent to acknowledge a message in some - * way. There are two supported acknowledgements: `delivered` (sent - * when the event has been received) and `read` (sent when the event - * has been observed by the end-user). The `target_event_id` should - * reference the `m.room.message` event being acknowledged. - */ -public class Matrix.Event.RoomMessageFeedback : Matrix.Event.Room { - /** - * The type of the feedback. As the use of this event type is - * discouraged, Matrix GLib SDK doesn’t implement this as an - * actual enum. - */ - public string? feedback_type { get; set; default = null; } - - /** - * The event that this feedback is related to. - */ - public string? target_event_id { 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("type")) != null) { - _feedback_type = node.get_string(); - } else { - warning("content.type is missing from a m.room.message.feedback event"); - } - - if ((node = content_root.get_member("target_event_id")) != null) { - _target_event_id = node.get_string(); - } else { - warning("content.target_event_id is missing from a m.room.message.feedback event"); - } - - base.from_json(json_data); - } - - protected override void - to_json(Json.Node json_data) - throws Matrix.Error - { - if ((_feedback_type == null) || (_target_event_id == null)) { - throw new Matrix.Error.INCOMPLETE( - "Won't generate a m.room.message.feedback without all fields set"); - } - - var content_root = json_data.get_object() - .get_member("content").get_object(); - - content_root.set_string_member("type", _feedback_type); - content_root.set_string_member("target_event_id", _target_event_id); - - base.to_json(json_data); - } -} diff --git a/src/matrix-event-types.c b/src/matrix-event-types.c index acf16af..c98f017 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-message-feedback.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 51387c3..f7cef2b 100644 --- a/vapi/c-api.vapi +++ b/vapi/c-api.vapi @@ -821,6 +821,18 @@ namespace Matrix { public void set_event_level(string event_type, int level); } + + [CCode (cheader_filename = "matrix-event-room-message-feedback.h")] + public class RoomMessageFeedback : Room { + public string? feedback_type { get; set; default = null; } + public string? target_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")]