first channel API

This commit is contained in:
Florian Stinglmayr 2019-07-05 13:37:50 +02:00
parent be4e5cedc3
commit e2bdd05775
3 changed files with 65 additions and 0 deletions

View File

@ -18,6 +18,7 @@ SET(SOURCES
"include/dc/util.h"
"src/account.c"
"src/api.c"
"src/api-channel.c"
"src/api-friends.c"
"src/apisync.c"
"src/channel.c"

View File

@ -4,6 +4,7 @@
#include <dc/apisync.h>
#include <dc/account.h>
#include <dc/guild.h>
#include <dc/channel.h>
#include <stdbool.h>
@ -59,6 +60,16 @@ bool dc_api_get_userinfo(dc_api_t api, dc_account_t login,
bool dc_api_get_userguilds(dc_api_t api, dc_account_t login,
GPtrArray **guilds);
/**
* Create a 1:1 or 1:N DM channel with the given recipients. The recipients must
* have their ID (snowflake) set. Returns the new channel, complete with ID, and
* all in "channel". Note that the "login" user is automatically added to the DM
* session, so it is not needed to add him to recipients.
*/
bool dc_api_create_channel(dc_api_t api, dc_account_t login,
dc_account_t *recipients, size_t nrecp,
dc_channel_t *channel);
/**
* Fetch a list of friends of the login account "login". The friends are stored
* within the login object.

53
libdc/src/api-channel.c Normal file
View File

@ -0,0 +1,53 @@
#include <dc/api.h>
#include "internal.h"
bool dc_api_create_channel(dc_api_t api, dc_account_t login,
dc_account_t *recipients, size_t nrecp,
dc_channel_t *channel)
{
bool ret = false;
json_t *data = NULL, *array = NULL, *reply = NULL;
char *url = NULL;
dc_channel_t c = NULL;
size_t i = 0;
return_if_true(api == NULL || login == NULL || channel == NULL, false);
asprintf(&url, "users/%s/channels", dc_account_id(login));
goto_if_true(url == NULL, cleanup);
/* build a JSON object that contains one array called "recipients":
* {"recipients": ["snowflake#1", "snowflake#N"]}
*/
data = json_object();
array = json_array();
goto_if_true(data == NULL || array == NULL, cleanup);
for (i = 0; i < nrecp; i++) {
dc_account_t r = recipients[0];
if (dc_account_id(r) == NULL) {
continue;
}
json_array_append_new(array, json_string(dc_account_id(r)));
}
goto_if_true(json_array_size(array) == 0, cleanup);
json_object_set_new(data, "recipients", array);
reply = dc_api_call_sync(api, "POST", dc_account_token(login), url, data);
goto_if_true(reply == NULL, cleanup);
c = dc_channel_from_json(reply);
goto_if_true(c == NULL, cleanup);
*channel = c;
cleanup:
free(url);
json_decref(reply);
json_decref(data);
return ret;
}