diff --git a/libdc/CMakeLists.txt b/libdc/CMakeLists.txt index c6d94d8..0cfae74 100644 --- a/libdc/CMakeLists.txt +++ b/libdc/CMakeLists.txt @@ -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" diff --git a/libdc/include/dc/api.h b/libdc/include/dc/api.h index 503abfd..cbb6665 100644 --- a/libdc/include/dc/api.h +++ b/libdc/include/dc/api.h @@ -4,6 +4,7 @@ #include #include #include +#include #include @@ -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. diff --git a/libdc/src/api-channel.c b/libdc/src/api-channel.c new file mode 100644 index 0000000..103cd3e --- /dev/null +++ b/libdc/src/api-channel.c @@ -0,0 +1,53 @@ +#include + +#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; +}