diff --git a/libdc/include/dc/account.h b/libdc/include/dc/account.h index e4552a9..adddef7 100644 --- a/libdc/include/dc/account.h +++ b/libdc/include/dc/account.h @@ -19,6 +19,12 @@ char const *dc_account_password(dc_account_t a); void dc_account_set_id(dc_account_t a, char const *id); char const *dc_account_id(dc_account_t a); +void dc_account_set_username(dc_account_t a, char const *id); +char const *dc_account_username(dc_account_t a); + +void dc_account_set_discriminator(dc_account_t a, char const *id); +char const *dc_account_discriminator(dc_account_t a); + void dc_account_set_token(dc_account_t a, char const *token); char const *dc_account_token(dc_account_t a); bool dc_account_has_token(dc_account_t a); diff --git a/libdc/src/account.c b/libdc/src/account.c index 8066a9b..2402654 100644 --- a/libdc/src/account.c +++ b/libdc/src/account.c @@ -17,6 +17,15 @@ struct dc_account_ /* internal ID */ char *id; + /* username + */ + char *username; + /* discriminator + */ + char *discriminator; + /* full username username#discriminator + */ + char *full; /* authentication token */ @@ -121,3 +130,50 @@ char const *dc_account_id(dc_account_t a) return_if_true(a == NULL,NULL); return a->id; } + +void dc_account_update_full(dc_account_t a) +{ + free(a->full); + a->full = NULL; + + asprintf(&a->full, "%s/%s", + (a->username != NULL ? a->username : ""), + (a->discriminator != NULL ? a->discriminator : "") + ); +} + +void dc_account_set_username(dc_account_t a, char const *id) +{ + return_if_true(a == NULL,); + + free(a->username); + a->username = strdup(id); + dc_account_update_full(a); +} + +char const *dc_account_username(dc_account_t a) +{ + return_if_true(a == NULL, NULL); + return a->username; +} + +void dc_account_set_discriminator(dc_account_t a, char const *id) +{ + return_if_true(a == NULL,); + + free(a->discriminator); + a->discriminator = strdup(id); + dc_account_update_full(a); +} + +char const *dc_account_discriminator(dc_account_t a) +{ + return_if_true(a == NULL, NULL); + return a->discriminator; +} + +char const *dc_account_full_username(dc_account_t a) +{ + return_if_true(a == NULL, NULL); + return a->full; +} diff --git a/libdc/src/api.c b/libdc/src/api.c index 2a33087..ea3def8 100644 --- a/libdc/src/api.c +++ b/libdc/src/api.c @@ -336,7 +336,7 @@ bool dc_api_userinfo(dc_api_t api, dc_account_t login, dc_account_t user) { char *url = NULL; - json_t *reply = NULL; + json_t *reply = NULL, *val = NULL; bool ret = false; return_if_true(api == NULL, false); @@ -348,9 +348,17 @@ bool dc_api_userinfo(dc_api_t api, dc_account_t login, reply = dc_api_call_sync(api, dc_account_token(login), url, NULL); goto_if_true(reply == NULL, cleanup); - /* TODO: parse json and store info in user - */ - dc_util_dump_json(reply); + val = json_object_get(reply, "username"); + goto_if_true(val == NULL || !json_is_string(val), cleanup); + dc_account_set_username(user, json_string_value(val)); + + val = json_object_get(reply, "discriminator"); + goto_if_true(val == NULL || !json_is_string(val), cleanup); + dc_account_set_discriminator(user, json_string_value(val)); + + val = json_object_get(reply, "id"); + goto_if_true(val == NULL || !json_is_string(val), cleanup); + dc_account_set_id(user, json_string_value(val)); ret = true;