diff options
author | Richard van der Hoff <richard@matrix.org> | 2015-10-30 15:38:23 +0000 |
---|---|---|
committer | Richard van der Hoff <richard@matrix.org> | 2015-10-30 15:38:23 +0000 |
commit | e354189a87d01942a0abe782583f0b30eb7f8839 (patch) | |
tree | 5a5e4bcf21d845daf1d1c6175b56bbe5081e2ccb | |
parent | 77d0978bd3fea5837f843a81065b2d8a9054f136 (diff) | |
download | purple-matrix-e354189a87d01942a0abe782583f0b30eb7f8839.tar.gz |
Implement joining rooms
Double-clicking on a room in the buddy list now joins a room if necessary.
-rw-r--r-- | libmatrix.c | 3 | ||||
-rw-r--r-- | matrix-api.c | 29 | ||||
-rw-r--r-- | matrix-api.h | 35 | ||||
-rw-r--r-- | matrix-connection.c | 68 | ||||
-rw-r--r-- | matrix-connection.h | 8 |
5 files changed, 142 insertions, 1 deletions
diff --git a/libmatrix.c b/libmatrix.c index c1e696f..0c38941 100644 --- a/libmatrix.c +++ b/libmatrix.c @@ -136,7 +136,8 @@ static void matrixprpl_join_chat(PurpleConnection *gc, GHashTable *components) /* already in chat */ return; } - purple_debug_info("matrixprpl", "Request to join chat room %s\n", room); + + matrix_connection_join_room(gc, room, components); } diff --git a/matrix-api.c b/matrix-api.c index 2bc6865..69049cd 100644 --- a/matrix-api.c +++ b/matrix-api.c @@ -603,6 +603,7 @@ MatrixApiRequestData *matrix_api_sync(MatrixConnectionData *conn, return fetch_data; } + MatrixApiRequestData *matrix_api_send(MatrixConnectionData *conn, const gchar *room_id, const gchar *event_type, const gchar *txn_id, JsonObject *content, MatrixApiCallback callback, @@ -650,6 +651,33 @@ MatrixApiRequestData *matrix_api_send(MatrixConnectionData *conn, } +MatrixApiRequestData *matrix_api_join_room(MatrixConnectionData *conn, + const gchar *room, + MatrixApiCallback callback, + MatrixApiErrorCallback error_callback, + MatrixApiBadResponseCallback bad_response_callback, + gpointer user_data) +{ + GString *url; + MatrixApiRequestData *fetch_data; + + url = g_string_new(conn->homeserver); + g_string_append(url, "_matrix/client/api/v1/rooms/"); + g_string_append(url, purple_url_encode(room)); + g_string_append(url, "/join?access_token="); + g_string_append(url, purple_url_encode(conn->access_token)); + + purple_debug_info("matrixprpl", "joining %s\n", room); + + fetch_data = matrix_api_start(url->str, "POST", "{}", conn, callback, + error_callback, bad_response_callback, + user_data, 0); + g_string_free(url, TRUE); + + return fetch_data; +} + + MatrixApiRequestData *matrix_api_leave_room(MatrixConnectionData *conn, const gchar *room_id, MatrixApiCallback callback, @@ -676,6 +704,7 @@ MatrixApiRequestData *matrix_api_leave_room(MatrixConnectionData *conn, return fetch_data; } + #if 0 MatrixApiRequestData *matrix_api_get_room_state(MatrixConnectionData *conn, const gchar *room_id, diff --git a/matrix-api.h b/matrix-api.h index 3e1e2c8..3cccb3b 100644 --- a/matrix-api.h +++ b/matrix-api.h @@ -183,7 +183,42 @@ MatrixApiRequestData *matrix_api_send(MatrixConnectionData *conn, gpointer user_data); +/** + * Make a request to join a room + * + * @param conn The connection with which to make the request + * @param room The room (id or alias) to join + * @param callback Function to be called when the request completes + * @param error_callback Function to be called if there is an error making + * the request. If NULL, matrix_api_error will be + * used. + * @param bad_response_callback Function to be called if the API gives a non-200 + * response. If NULL, matrix_api_bad_response will be + * used. + * @param user_data Opaque data to be passed to the callbacks + */ +MatrixApiRequestData *matrix_api_join_room(MatrixConnectionData *conn, + const gchar *room, + MatrixApiCallback callback, + MatrixApiErrorCallback error_callback, + MatrixApiBadResponseCallback bad_response_callback, + gpointer user_data); + +/** + * Leave a room + * + * @param conn The connection with which to make the request + * @param room_id The id of the room to leave + * @param callback Function to be called when the request completes + * @param error_callback Function to be called if there is an error making + * the request. If NULL, matrix_api_error will be + * used. + * @param bad_response_callback Function to be called if the API gives a non-200 + * response. If NULL, matrix_api_bad_response will be + * used. + * @param user_data Opaque data to be passed to the callbacks + */ MatrixApiRequestData *matrix_api_leave_room(MatrixConnectionData *conn, const gchar *room_id, MatrixApiCallback callback, diff --git a/matrix-connection.c b/matrix-connection.c index b77942a..c35dcbe 100644 --- a/matrix-connection.c +++ b/matrix-connection.c @@ -224,3 +224,71 @@ void matrix_connection_start_login(PurpleConnection *pc) purple_account_get_password(acct), _login_completed, conn); } + +static void _join_completed(MatrixConnectionData *conn, + gpointer user_data, + JsonNode *json_root) +{ + GHashTable *components = user_data; + JsonObject *root_obj; + const gchar *room_id; + + root_obj = matrix_json_node_get_object(json_root); + room_id = matrix_json_object_get_string_member(root_obj, "room_id"); + purple_debug_info("matrixprpl", "join %s completed", room_id); + + g_hash_table_destroy(components); +} + + +static void _join_error(MatrixConnectionData *conn, + gpointer user_data, const gchar *error_message) +{ + GHashTable *components = user_data; + g_hash_table_destroy(components); + matrix_api_error(conn, user_data, error_message); +} + + +static void _join_failed(MatrixConnectionData *conn, + gpointer user_data, int http_response_code, + struct _JsonNode *json_root) +{ + GHashTable *components = user_data; + JsonObject *json_obj; + const gchar *error = NULL; + const gchar *title = "Error joining chat"; + + if (json_root != NULL) { + json_obj = matrix_json_node_get_object(json_root); + error = matrix_json_object_get_string_member(json_obj, "error"); + } + + purple_notify_error(conn->pc, title, title, error); + purple_serv_got_join_chat_failed(conn->pc, components); + g_hash_table_destroy(components); +} + + + +void matrix_connection_join_room(struct _PurpleConnection *pc, + const gchar *room, GHashTable *components) +{ + GHashTable *copy; + GHashTableIter iter; + gpointer key, value; + + MatrixConnectionData *conn = purple_connection_get_protocol_data(pc); + + /* we have to copy the components table, so that we can pass it back + * later on :/ + */ + copy = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); + g_hash_table_iter_init (&iter, components); + while (g_hash_table_iter_next (&iter, &key, &value)) { + g_hash_table_insert(copy, g_strdup(key), g_strdup(value)); + } + + matrix_api_join_room(conn, room, _join_completed, _join_error, _join_failed, + copy); +} diff --git a/matrix-connection.h b/matrix-connection.h index a79c5cf..4bb3d72 100644 --- a/matrix-connection.h +++ b/matrix-connection.h @@ -63,4 +63,12 @@ void matrix_connection_free(struct _PurpleConnection *pc); void matrix_connection_cancel_sync(struct _PurpleConnection *pc); +/** + * start the process for joining a room + */ +void matrix_connection_join_room(struct _PurpleConnection *pc, + const gchar *room, GHashTable *components); + + + #endif |