support for changing online status

This commit is contained in:
2019-07-23 18:37:11 +02:00
parent 95d23bf6a9
commit 318c874dc6
10 changed files with 137 additions and 5 deletions

View File

@@ -31,6 +31,11 @@
#include <event.h>
#include <glib.h>
#define DC_API_USER_STATUS_ONLINE "online"
#define DC_API_USER_STATUS_IDLE "idle"
#define DC_API_USER_STATUS_DND "dnd"
#define DC_API_USER_STATUS_INVISIBLE "invisible"
struct dc_api_;
typedef struct dc_api_ *dc_api_t;
@@ -93,6 +98,14 @@ 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);
/**
* Set the online status of the currently logged in user "login". "status" must
* be one of the valid string macros defined in this header, or the function
* fails.
*/
bool dc_api_set_user_status(dc_api_t api, dc_account_t login,
char const *status);
/**
* 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

View File

@@ -19,6 +19,40 @@
#include <dc/api.h>
#include "internal.h"
bool dc_api_set_user_status(dc_api_t api, dc_account_t login,
char const *status)
{
char const *url = "users/@me/settings";
json_t *reply = NULL, *data = NULL;
bool ret = false;
return_if_true(api == NULL || login == NULL || status == NULL, false);
if (strcmp(status, DC_API_USER_STATUS_ONLINE) != 0 &&
strcmp(status, DC_API_USER_STATUS_IDLE) != 0 &&
strcmp(status, DC_API_USER_STATUS_DND) != 0 &&
strcmp(status, DC_API_USER_STATUS_INVISIBLE) != 0) {
return false;
}
data = json_object();
goto_if_true(data == NULL, cleanup);
json_object_set_new(data, "status", json_string(status));
reply = dc_api_call_sync(api, "PATCH", TOKEN(login), url, data);
goto_if_true(reply != NULL, cleanup);
ret = true;
cleanup:
json_decref(reply);
json_decref(data);
return ret;
}
bool dc_api_get_userinfo(dc_api_t api, dc_account_t login,
dc_account_t user)
{
@@ -36,7 +70,7 @@ bool dc_api_get_userinfo(dc_api_t api, dc_account_t login,
asprintf(&url, "users/%s", dc_account_id(user));
}
reply = dc_api_call_sync(api, "GET", dc_account_token(login), url, NULL);
reply = dc_api_call_sync(api, "GET", TOKEN(login), url, NULL);
goto_if_true(reply == NULL, cleanup);
val = json_object_get(reply, "username");
@@ -74,7 +108,7 @@ bool dc_api_get_userguilds(dc_api_t api, dc_account_t login, GPtrArray **out)
return_if_true(api == NULL, false);
return_if_true(login == NULL, false);
reply = dc_api_call_sync(api, "GET", dc_account_token(login), url, NULL);
reply = dc_api_call_sync(api, "GET", TOKEN(login), url, NULL);
goto_if_true(reply == NULL, cleanup);
goto_if_true(!json_is_array(reply), cleanup);

View File

@@ -185,7 +185,8 @@ dc_api_do(dc_api_t api, char const *verb,
}
if (strcmp(verb, "PUT") == 0 ||
strcmp(verb, "DELETE") == 0) {
strcmp(verb, "DELETE") == 0 ||
strcmp(verb, "PATCH") == 0) {
curl_easy_setopt(c, CURLOPT_CUSTOMREQUEST, verb);
}