Skip to content

Commit

Permalink
Merge pull request #40 from EionRobb/reuse-device-id
Browse files Browse the repository at this point in the history
Re-use the device-id provided by the server
  • Loading branch information
richvdh authored Mar 13, 2017
2 parents 5ceba10 + 482e9d9 commit 00c43fe
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
10 changes: 7 additions & 3 deletions matrix-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ void matrix_api_cancel(MatrixApiRequestData *data)
}


gchar *_build_login_body(const gchar *username, const gchar *password)
gchar *_build_login_body(const gchar *username, const gchar *password, const gchar *device_id)
{
JsonObject *body;
JsonNode *node;
Expand All @@ -596,7 +596,10 @@ gchar *_build_login_body(const gchar *username, const gchar *password)
json_object_set_string_member(body, "type", "m.login.password");
json_object_set_string_member(body, "user", username);
json_object_set_string_member(body, "password", password);

json_object_set_string_member(body, "initial_device_display_name", "purple-matrix");
if (device_id != NULL)
json_object_set_string_member(body, "device_id", device_id);

node = json_node_new(JSON_NODE_OBJECT);
json_node_set_object(node, body);
json_object_unref(body);
Expand All @@ -612,6 +615,7 @@ gchar *_build_login_body(const gchar *username, const gchar *password)
MatrixApiRequestData *matrix_api_password_login(MatrixConnectionData *conn,
const gchar *username,
const gchar *password,
const gchar *device_id,
MatrixApiCallback callback,
gpointer user_data)
{
Expand All @@ -625,7 +629,7 @@ MatrixApiRequestData *matrix_api_password_login(MatrixConnectionData *conn,
url = g_strconcat(conn->homeserver, "_matrix/client/api/v1/login",
NULL);

json = _build_login_body(username, password);
json = _build_login_body(username, password, device_id);

fetch_data = matrix_api_start(url, "POST", json, conn, callback,
NULL, NULL, user_data, 0);
Expand Down
1 change: 1 addition & 0 deletions matrix-api.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ void matrix_api_cancel(MatrixApiRequestData *request);
MatrixApiRequestData *matrix_api_password_login(MatrixConnectionData *conn,
const gchar *username,
const gchar *password,
const gchar *device_id,
MatrixApiCallback callback,
gpointer user_data);

Expand Down
6 changes: 5 additions & 1 deletion matrix-connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ static void _login_completed(MatrixConnectionData *conn,
conn->access_token = g_strdup(access_token);
conn->user_id = g_strdup(matrix_json_object_get_string_member(root_obj,
"user_id"));
purple_account_set_string(pc->account, "device_id",
matrix_json_object_get_string_member(root_obj, "device_id"));

/* start the sync loop */
next_batch = purple_account_get_string(pc->account,
Expand Down Expand Up @@ -233,7 +235,9 @@ void matrix_connection_start_login(PurpleConnection *pc)
purple_connection_update_progress(pc, _("Logging in"), 0, 3);

matrix_api_password_login(conn, acct->username,
purple_account_get_password(acct), _login_completed, conn);
purple_account_get_password(acct),
purple_account_get_string(acct, "device_id", NULL),
_login_completed, conn);
}


Expand Down

0 comments on commit 00c43fe

Please sign in to comment.