implement adding friends

This commit is contained in:
2019-07-03 22:47:12 +02:00
parent 2e563b724a
commit efce6a6543
8 changed files with 231 additions and 68 deletions

View File

@@ -8,8 +8,19 @@
struct dc_account_;
typedef struct dc_account_ *dc_account_t;
typedef enum {
FRIEND_STATE_NONE = 0,
/* accountt is a mutual friend
*/
FRIEND_STATE_FRIEND = 1,
/* pending account, the other side hasn't accepted yet
*/
FRIEND_STATE_PENDING = 4,
} dc_account_friend_states;
dc_account_t dc_account_new(void);
dc_account_t dc_account_new2(char const *email, char const *pass);
dc_account_t dc_account_from_fullid(char const *fullid);
void dc_account_set_email(dc_account_t a, char const *email);
char const *dc_account_email(dc_account_t a);
@@ -37,5 +48,7 @@ bool dc_account_has_token(dc_account_t a);
void dc_account_set_friends(dc_account_t a, dc_account_t *ptr, size_t len);
dc_account_t dc_account_nthfriend(dc_account_t a, size_t i);
size_t dc_account_friends_size(dc_account_t a);
int dc_account_friend_state(dc_account_t a);
void dc_account_set_friend_state(dc_account_t a, int state);
#endif

View File

@@ -65,4 +65,9 @@ bool dc_api_get_userguilds(dc_api_t api, dc_account_t login,
*/
bool dc_api_get_friends(dc_api_t api, dc_account_t login);
/**
* Add a given account as a friend to the friends list
*/
bool dc_api_add_friend(dc_api_t api, dc_account_t login, dc_account_t friend);
#endif

View File

@@ -34,6 +34,9 @@ struct dc_account_
/* friends we have
*/
GPtrArray *friends;
/* our own friend state
*/
int friend_state;
};
static void dc_account_free(dc_account_t ptr)
@@ -81,6 +84,38 @@ dc_account_t dc_account_new2(char const *email, char const *pass)
return ptr;
}
dc_account_t dc_account_from_fullid(char const *fullid)
{
return_if_true(fullid == NULL, NULL);
char *name = strdup(fullid), *discriminator = NULL;
dc_account_t acc = NULL;
return_if_true(name == NULL, NULL);
discriminator = strchr(name, '#');
if (discriminator == NULL || *discriminator == '\0') {
free(name);
return NULL;
}
*discriminator = '\0';
++discriminator;
acc = dc_account_new();
if (acc == NULL) {
free(name);
return NULL;
}
dc_account_set_username(acc, name);
dc_account_set_discriminator(acc, discriminator);
free(name);
return acc;
}
void dc_account_set_email(dc_account_t a, char const *email)
{
return_if_true(a == NULL,);
@@ -219,3 +254,15 @@ size_t dc_account_friends_size(dc_account_t a)
return_if_true(a == NULL || a->friends == NULL, 0);
return a->friends->len;
}
int dc_account_friend_state(dc_account_t a)
{
return_if_true(a == NULL, 0);
return a->friend_state;
}
void dc_account_set_friend_state(dc_account_t a, int state)
{
return_if_true(a == NULL,);
a->friend_state = state;
}

View File

@@ -349,6 +349,29 @@ error:
return NULL;
}
static json_t *dc_api_user_to_json(dc_account_t a)
{
json_t *j = NULL;
return_if_true(a == NULL, NULL);
return_if_true(dc_account_username(a) == NULL ||
dc_account_discriminator(a) == NULL,
NULL
);
j = json_object();
return_if_true(j == NULL, NULL);
json_object_set_new(j, "username",
json_string(dc_account_username(a))
);
json_object_set_new(j, "discriminator",
json_string(dc_account_discriminator(a))
);
return j;
}
bool dc_api_get_userinfo(dc_api_t api, dc_account_t login,
dc_account_t user)
{
@@ -420,6 +443,15 @@ bool dc_api_get_friends(dc_api_t api, dc_account_t login)
if (a == NULL) {
continue;
}
/* read the type also known as "typ"
*/
val = json_object_get(c, "type");
if (val != NULL && json_is_integer(val)) {
int state = json_integer_value(val);
dc_account_set_friend_state(a, state);
}
g_ptr_array_add(f, a);
}
@@ -440,6 +472,36 @@ cleanup:
return ret;
}
/**
* Add a given account as a friend to the friends list
*/
bool dc_api_add_friend(dc_api_t api, dc_account_t login, dc_account_t friend)
{
char const *url = "users/@me/relationships";
json_t *reply = NULL, *post = NULL;
bool ret = false;
return_if_true(api == NULL, false);
return_if_true(login == NULL, false);
post = dc_api_user_to_json(friend);
return_if_true(post == NULL, false);
reply = dc_api_call_sync(api, "POST", dc_account_token(login), url, post);
/* apparently if no data comes back, then the whole thing was a success
*/
goto_if_true(reply != NULL, cleanup);
ret = true;
cleanup:
json_decref(post);
json_decref(reply);
return ret;
}
bool dc_api_get_userguilds(dc_api_t api, dc_account_t login, GPtrArray **out)
{
char const *url = "users/@me/guilds";