From 37b174246219fccf9fc3814f6ea90b73865c11cb Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 11 Jan 2016 16:28:58 +0100 Subject: [PATCH] Add create-room call to test client --- src/matrix-http-api.c | 137 +++++++++++++++++++++++++++++++++++++++++- src/test-client.c | 24 +++----- 2 files changed, 145 insertions(+), 16 deletions(-) diff --git a/src/matrix-http-api.c b/src/matrix-http-api.c index c3f0a68..3972b06 100644 --- a/src/matrix-http-api.c +++ b/src/matrix-http-api.c @@ -17,6 +17,7 @@ */ #include "matrix-http-api.h" +#include "matrix-enumtypes.h" #include #include @@ -636,7 +637,7 @@ _send(MatrixHTTPAPI *api, request); } -void +static void i_login(MatrixAPI *api, MatrixAPICallback callback, gpointer user_data, @@ -657,6 +658,139 @@ i_login(MatrixAPI *api, error); } +static void +add_state_event(MatrixAPIStateEvent *event, JsonBuilder *builder) +{ + JsonNode *node = matrix_api_state_event_get_json_node(event); + + json_builder_add_value(builder, node); + json_node_free(node); +} + +static void +add_string(gchar *str, JsonBuilder *builder) +{ + json_builder_add_string_value(builder, str); +} + +static void +i_create_room(MatrixAPI *api, + MatrixAPICallback callback, + gpointer user_data, + MatrixAPIRoomPreset preset, + const gchar *room_name, + const gchar *room_alias, + const gchar *topic, + MatrixAPIRoomVisibility visibility, + JsonNode *creation_content, + GList *initial_state, + GList *invitees, + GError **error) +{ + JsonNode *body; + JsonObject *root_object; + JsonBuilder *builder; + + builder = json_builder_new(); + json_builder_begin_object(builder); + + if (creation_content) { + json_builder_set_member_name(builder, "creation_content"); + json_builder_add_value(builder, creation_content); + } + + if (initial_state) { + json_builder_set_member_name(builder, "initial_state"); + json_builder_begin_array(builder); + g_list_foreach(initial_state, (GFunc)add_state_event, builder); + json_builder_end_array(builder); + } + + if (invitees) { + json_builder_set_member_name(builder, "invite"); + json_builder_begin_array(builder); + g_list_foreach(invitees, (GFunc)add_string, builder); + json_builder_end_array(builder); + } + + if (room_name) { + json_builder_set_member_name(builder, "name"); + json_builder_add_string_value(builder, room_name); + } + + if (preset != MATRIX_API_ROOM_PRESET_NONE) { + GEnumClass *_enum_class = g_type_class_ref(MATRIX_TYPE_API_ROOM_PRESET); + GEnumValue *enum_value; + + if ((enum_value = g_enum_get_value( + G_ENUM_CLASS(_enum_class), + preset)) != NULL) { + gchar *i; + gchar *value_nick = g_strdup(enum_value->value_nick); + + for (i = value_nick; *i; i++) { + if (*i == '-') { + *i = '_'; + } + } + + json_builder_set_member_name(builder, "preset"); + json_builder_add_string_value(builder, value_nick); + g_free(value_nick); + } else { + g_debug("Invalid room preset type"); + } + + g_type_class_unref(_enum_class); + } + + if (room_alias) { + json_builder_set_member_name(builder, "room_alias_name"); + json_builder_add_string_value(builder, room_alias); + } + + if (topic) { + json_builder_set_member_name(builder, "topic"); + json_builder_add_string_value(builder, topic); + } + + if (visibility != MATRIX_API_ROOM_VISIBILITY_DEFAULT) { + GEnumClass *_enum_class = g_type_class_ref( + MATRIX_TYPE_API_ROOM_VISIBILITY); + GEnumValue *enum_value; + + if ((enum_value = g_enum_get_value( + G_ENUM_CLASS(_enum_class), + visibility)) != NULL) { + gchar *i; + gchar *value_nick = g_strdup(enum_value->value_nick); + + for (i = value_nick; *i; i++) { + if (*i == '-') { + *i = '_'; + } + } + + json_builder_set_member_name(builder, "visibility"); + json_builder_add_string_value(builder, value_nick); + g_free(value_nick); + } else { + g_debug("Invalid room visibility type"); + } + + g_type_class_unref(_enum_class); + } + + json_builder_end_object(builder); + body = json_builder_get_root(builder); + g_object_unref(builder); + + _send(MATRIX_HTTP_API(api), + callback, user_data, + "POST", "createRoom", body, + error); +} + static void matrix_http_api_matrix_api_init(MatrixAPIInterface *iface) { @@ -667,4 +801,5 @@ matrix_http_api_matrix_api_init(MatrixAPIInterface *iface) iface->get_user_id = i_get_user_id; iface->get_homeserver = i_get_homeserver; iface->login = i_login; + iface->create_room = i_create_room; } diff --git a/src/test-client.c b/src/test-client.c index dd70d06..73c64f6 100644 --- a/src/test-client.c +++ b/src/test-client.c @@ -38,9 +38,9 @@ create_room_finished(MatrixAPI *api, { if (err) { g_debug("Error: %s", err->message); + } else { + g_printf("Room registered\n"); } - - g_printf("Room registered\n"); } static void @@ -72,13 +72,13 @@ login_finished(MatrixAPI *api, JsonNode *content, gpointer data, GError *err) g_printf("Logged in as %s\n", user_id); - /* matrix_http_api_create_room(api, */ - /* create_room_finished, NULL, */ - /* MATRIX_API_ROOM_PRESET_PUBLIC, */ - /* "matrix-glib-sdk-test", NULL, */ - /* "GLib SDK test room", */ - /* MATRIX_API_ROOM_VISIBILITY_DEFAULT, */ - /* NULL, NULL, NULL); */ + matrix_api_create_room(api, + create_room_finished, NULL, + MATRIX_API_ROOM_PRESET_PUBLIC, + "GLib SDK test room", "matrix-glib-sdk-test", + "GLib SDK test room", + MATRIX_API_ROOM_VISIBILITY_DEFAULT, + NULL, NULL, NULL, NULL); } else { g_printf("Login unsuccessful!\n"); } @@ -118,12 +118,6 @@ main(int argc, char *argv[]) g_info("Starting up: %s with %s:%s", url, user, password); - /* - * [ ] register - * [ ] login - * [ ] create_room - * [ ] join_room - */ api = matrix_http_api_new(url, NULL); builder = json_builder_new(); json_builder_begin_object(builder);