Port MatrixEventRoomJoinRules to C

This commit is contained in:
Gergely Polonkai 2017-11-21 10:51:44 +01:00
parent 69fda9f728
commit fe8664fb69
7 changed files with 317 additions and 91 deletions

1
.gitignore vendored
View File

@ -53,7 +53,6 @@ Makefile.in
/src/matrix-http-client.c /src/matrix-http-client.c
/src/namespace-info.vala /src/namespace-info.vala
/src/namespace-info.c /src/namespace-info.c
/src/matrix-event-room-join-rules.c
/src/matrix-event-room-canonical-alias.c /src/matrix-event-room-canonical-alias.c
/src/matrix-event-room-create.c /src/matrix-event-room-create.c
/src/matrix-event-room-power-levels.c /src/matrix-event-room-power-levels.c

View File

@ -21,7 +21,6 @@ libmatrix_glib_0_0_la_VALA_SOURCES = \
matrix-client.vala \ matrix-client.vala \
matrix-http-api.vala \ matrix-http-api.vala \
matrix-http-client.vala \ matrix-http-client.vala \
matrix-event-room-join-rules.vala \
matrix-event-room-canonical-alias.vala \ matrix-event-room-canonical-alias.vala \
matrix-event-room-create.vala \ matrix-event-room-create.vala \
matrix-event-room-power-levels.vala \ matrix-event-room-power-levels.vala \
@ -110,6 +109,7 @@ INST_H_SRC_FILES = \
matrix-event-room-name.h \ matrix-event-room-name.h \
matrix-event-room-message.h \ matrix-event-room-message.h \
matrix-event-room-history-visibility.h \ matrix-event-room-history-visibility.h \
matrix-event-room-join-rules.h \
matrix-event-typing.h \ matrix-event-typing.h \
matrix-event-receipt.h \ matrix-event-receipt.h \
utils.h \ utils.h \
@ -159,6 +159,7 @@ libmatrix_glib_0_0_la_SOURCES = \
matrix-event-room-name.c \ matrix-event-room-name.c \
matrix-event-room-message.c \ matrix-event-room-message.c \
matrix-event-room-history-visibility.c \ matrix-event-room-history-visibility.c \
matrix-event-room-join-rules.c \
matrix-profile.c \ matrix-profile.c \
matrix-room.c \ matrix-room.c \
utils.c \ utils.c \

View File

@ -0,0 +1,262 @@
/*
* 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
* <http://www.gnu.org/licenses/>.
*/
#include "matrix-event-room-join-rules.h"
#include "matrix-enumtypes.h"
#include "utils.h"
/**
* SECTION:matrix-event-room-join-rules
* @short_description: event to hold the join rules for a room
*
* This is the default event handler for `m.room.join_rules` events.
*
* A room may be `public` meaning anyone can join the room without any prior action.
* Alternatively, it can be `invite` meaning that a user who wishes to join the room must
* first receive an invite to the room from someone already inside of the room. Currently,
* `knock` and `private` are reserved keywords which are not implemented.
*/
enum {
PROP_0,
PROP_JOIN_RULES,
NUM_PROPERTIES
};
static GParamSpec *matrix_event_room_join_rules_properties[NUM_PROPERTIES];
typedef struct {
MatrixJoinRules _join_rules;
} MatrixEventRoomJoinRulesPrivate;
/**
* MatrixEventRoomJoinRules:
*/
G_DEFINE_TYPE_WITH_PRIVATE(MatrixEventRoomJoinRules, matrix_event_room_join_rules, MATRIX_EVENT_TYPE_STATE);
static void
matrix_event_room_join_rules_real_from_json(MatrixEventBase *matrix_event_base, JsonNode *json_data, GError **error)
{
MatrixEventRoomJoinRulesPrivate *priv;
JsonObject *root;
JsonObject *content_root;
JsonNode *content_node;
JsonNode *node;
g_return_if_fail(json_data != NULL);
priv = matrix_event_room_join_rules_get_instance_private(MATRIX_EVENT_ROOM_JOIN_RULES(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);
#ifdef 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.join_rules is non-empty");
}
}
#endif
if ((node = json_object_get_member(content_root, "join_rule")) != NULL) {
GError *inner_error = NULL;
MatrixJoinRules join_rules = _matrix_g_enum_nick_to_value(MATRIX_TYPE_JOIN_RULES, json_node_get_string(node), &inner_error);
if (inner_error != NULL) {
priv->_join_rules = MATRIX_JOIN_RULES_UNKNOWN;
#ifdef DEBUG
g_warning("Unknown value %s in a m.room.join_rules event", json_node_get_string(node));
#endif
} else {
priv->_join_rules = join_rules;
}
} else {
priv->_join_rules = MATRIX_JOIN_RULES_UNKNOWN;
g_warning("content.join_rules is missing from a m.room.join_rules event.");
}
MATRIX_EVENT_BASE_CLASS(matrix_event_room_join_rules_parent_class)->from_json(matrix_event_base, json_data, error);
}
static void
matrix_event_room_join_rules_real_to_json(MatrixEventBase *matrix_event_base, JsonNode *json_data, GError **error)
{
MatrixEventRoomJoinRulesPrivate *priv;
JsonObject *root;
JsonObject *content_root;
JsonNode *content_node;
const gchar *state_key;
gchar *join_rules;
g_return_if_fail(json_data != NULL);
priv = matrix_event_room_join_rules_get_instance_private(MATRIX_EVENT_ROOM_JOIN_RULES(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.join_rules event with a non-empty state_key");
return;
}
if (priv->_join_rules == MATRIX_JOIN_RULES_UNKNOWN) {
g_set_error(error, MATRIX_ERROR, MATRIX_ERROR_UNKNOWN_VALUE,
"Won't send a m.room.join_rules event with an unknown rule");
return;
}
join_rules = _matrix_g_enum_to_string(MATRIX_TYPE_JOIN_RULES, priv->_join_rules, '_');
json_object_set_string_member(content_root, "join_rule", join_rules);
g_free(join_rules);
MATRIX_EVENT_BASE_CLASS(matrix_event_room_join_rules_parent_class)->to_json(matrix_event_base, json_data, error);
}
/**
* matrix_event_room_join_rules_new:
*
* Create a new #MatrixEventRoomJoinRules object.
*
* Returns: (transfer full): a new #MatrixEventRoomJoinRules object
*/
MatrixEventRoomJoinRules *
matrix_event_room_join_rules_new(void) {
return (MatrixEventRoomJoinRules *)matrix_event_state_construct(MATRIX_EVENT_TYPE_ROOM_JOIN_RULES);
}
/**
* matrix_event_room_join_rules_get_join_rules:
* @event: a #MatrixEventRoomJoinRules
*
* Get the join rules for the room in @event.
*
* Returns: a #MatrixJoinRules value
*/
MatrixJoinRules
matrix_event_room_join_rules_get_join_rules(MatrixEventRoomJoinRules *matrix_event_room_join_rules)
{
MatrixEventRoomJoinRulesPrivate *priv;
g_return_val_if_fail(matrix_event_room_join_rules != NULL, 0);
priv = matrix_event_room_join_rules_get_instance_private(matrix_event_room_join_rules);
return priv->_join_rules;
}
/**
* matrix_event_room_join_rules_set_join_rules:
* @event: a #MatrixEventRoomJoinRules
* @join_rules: a #MatrixJoinRules value
*
* Set the join rules for the room in @event.
*/
void
matrix_event_room_join_rules_set_join_rules(MatrixEventRoomJoinRules *matrix_event_room_join_rules, MatrixJoinRules join_rules)
{
MatrixEventRoomJoinRulesPrivate *priv;
g_return_if_fail(matrix_event_room_join_rules != NULL);
priv = matrix_event_room_join_rules_get_instance_private(matrix_event_room_join_rules);
if (priv->_join_rules != join_rules) {
priv->_join_rules = join_rules;
g_object_notify_by_pspec((GObject *)matrix_event_room_join_rules, matrix_event_room_join_rules_properties[PROP_JOIN_RULES]);
}
}
static void
matrix_event_room_join_rules_finalize(GObject *gobject)
{
G_OBJECT_CLASS(matrix_event_room_join_rules_parent_class)->finalize(gobject);
}
static void
matrix_event_room_join_rules_get_property(GObject *gobject, guint property_id, GValue *value, GParamSpec *pspec)
{
MatrixEventRoomJoinRules *matrix_event_room_join_rules = MATRIX_EVENT_ROOM_JOIN_RULES(gobject);
switch (property_id) {
case PROP_JOIN_RULES:
g_value_set_enum(value, matrix_event_room_join_rules_get_join_rules(matrix_event_room_join_rules));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec);
break;
}
}
static void
matrix_event_room_join_rules_set_property(GObject *gobject, guint property_id, const GValue *value, GParamSpec *pspec)
{
MatrixEventRoomJoinRules *matrix_event_room_join_rules = MATRIX_EVENT_ROOM_JOIN_RULES(gobject);
switch (property_id) {
case PROP_JOIN_RULES:
matrix_event_room_join_rules_set_join_rules(matrix_event_room_join_rules, g_value_get_enum(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec);
break;
}
}
static void
matrix_event_room_join_rules_class_init(MatrixEventRoomJoinRulesClass *klass)
{
((MatrixEventBaseClass *)klass)->from_json = matrix_event_room_join_rules_real_from_json;
((MatrixEventBaseClass *)klass)->to_json = matrix_event_room_join_rules_real_to_json;
G_OBJECT_CLASS(klass)->get_property = matrix_event_room_join_rules_get_property;
G_OBJECT_CLASS(klass)->set_property = matrix_event_room_join_rules_set_property;
G_OBJECT_CLASS(klass)->finalize = matrix_event_room_join_rules_finalize;
/**
* MatrixEventRoomJoinRules:join-rules:
*
* The join rules.
*/
matrix_event_room_join_rules_properties[PROP_JOIN_RULES] = g_param_spec_enum(
"join-rules", "join-rules", "join-rules",
MATRIX_TYPE_JOIN_RULES, MATRIX_JOIN_RULES_UNKNOWN,
G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE);
g_object_class_install_property(G_OBJECT_CLASS(klass), PROP_JOIN_RULES, matrix_event_room_join_rules_properties[PROP_JOIN_RULES]);
}
static void
matrix_event_room_join_rules_init(MatrixEventRoomJoinRules *matrix_event_room_join_rules)
{
MatrixEventRoomJoinRulesPrivate *priv = matrix_event_room_join_rules_get_instance_private(matrix_event_room_join_rules);
priv->_join_rules = MATRIX_JOIN_RULES_UNKNOWN;
}

View File

@ -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
* <http://www.gnu.org/licenses/>.
*/
#ifndef __MATRIX_GLIB_SDK_EVENT_ROOM_JOIN_RULES_H__
# define __MATRIX_GLIB_SDK_EVENT_ROOM_JOIN_RULES_H__
# include <glib-object.h>
# include "matrix-event-state-base.h"
# include "matrix-types.h"
G_BEGIN_DECLS
#define MATRIX_EVENT_TYPE_ROOM_JOIN_RULES (matrix_event_room_join_rules_get_type ())
G_DECLARE_DERIVABLE_TYPE(MatrixEventRoomJoinRules, matrix_event_room_join_rules, MATRIX_EVENT, ROOM_JOIN_RULES, MatrixEventState)
struct _MatrixEventRoomJoinRulesClass {
MatrixEventStateClass parent_class;
};
MatrixEventRoomJoinRules *matrix_event_room_join_rules_new (void);
MatrixJoinRules matrix_event_room_join_rules_get_join_rules (MatrixEventRoomJoinRules *event);
void matrix_event_room_join_rules_set_join_rules (MatrixEventRoomJoinRules *event, MatrixJoinRules join_rules);
G_END_DECLS
#endif /* __MATRIX_GLIB_SDK_EVENT_ROOM_JOIN_RULES_H__ */

View File

@ -1,89 +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
* <http://www.gnu.org/licenses/>.
*/
/**
* Class to hold a m.room.join_rules event.
*
* A room may be public meaning anyone can join the room without any
* prior action. Alternatively, it can be invite meaning that a user
* who wishes to join the room must first receive an invite to the
* room from someone already inside of the room. Currently, knock and
* private are reserved keywords which are not implemented.
*/
public class Matrix.Event.RoomJoinRules : Matrix.Event.State {
public JoinRules join_rules { get; set; default = JoinRules.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.join_rules is non-empty");
}
}
if ((node = content_root.get_member("join_rule")) != null) {
try {
_join_rules = (JoinRules)_g_enum_nick_to_value(
typeof(JoinRules), node.get_string());
} catch (Matrix.Error e) {
_join_rules = Matrix.JoinRules.UNKNOWN;
if (Config.DEBUG) {
warning("Unknown value %s in a m.room.join_rules event",
node.get_string());
}
}
} else {
_join_rules = Matrix.JoinRules.UNKNOWN;
warning("content.join_rules is missing from a m.room.join_rules event.");
}
base.from_json(json_data);
}
protected override void
to_json(Json.Node json_data)
throws Matrix.Error
{
var content_root = json_data.get_object().
get_member("content").get_object();
if (_state_key != "") {
throw new Matrix.Error.INCOMPLETE(
"Won't generate a m.room.join_rules event with a non-empty state_key");
}
if (_join_rules == Matrix.JoinRules.UNKNOWN) {
throw new Matrix.Error.INCOMPLETE(
"Won't send a m.room.join_rules event with an unknown rule");
}
content_root.set_string_member(
"join_rule",
_g_enum_value_to_nick(typeof(Matrix.JoinRules), _join_rules));
base.to_json(json_data);
}
}

View File

@ -29,6 +29,7 @@
#include "matrix-event-room-name.h" #include "matrix-event-room-name.h"
#include "matrix-event-room-message.h" #include "matrix-event-room-message.h"
#include "matrix-event-room-history-visibility.h" #include "matrix-event-room-history-visibility.h"
#include "matrix-event-room-join-rules.h"
#include "matrix-message-text.h" #include "matrix-message-text.h"
#include "matrix-message-location.h" #include "matrix-message-location.h"

View File

@ -729,6 +729,17 @@ namespace Matrix {
protected override void to_json(Json.Node json_data) protected override void to_json(Json.Node json_data)
throws Matrix.Error; throws Matrix.Error;
} }
[CCode (cheader_filename = "matrix-event-room-join-rules.h")]
public class RoomJoinRules : State {
public Matrix.JoinRules join_rules { get; set; default = Matrix.JoinRules.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")] [CCode (gir_namespace = "MatrixMessage", gir_version = "0.0")]