store json information in account

This commit is contained in:
Florian Stinglmayr 2019-06-25 17:00:25 +02:00
parent 42183539d3
commit aed7dacf29
3 changed files with 74 additions and 4 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -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;