steps towards an ncurses API
This commit is contained in:
@@ -3,11 +3,13 @@
|
||||
|
||||
#include <dc/apisync.h>
|
||||
#include <dc/account.h>
|
||||
#include <dc/guild.h>
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <jansson.h>
|
||||
#include <event.h>
|
||||
#include <glib.h>
|
||||
|
||||
struct dc_api_;
|
||||
typedef struct dc_api_ *dc_api_t;
|
||||
@@ -48,7 +50,11 @@ bool dc_api_authenticate(dc_api_t api, dc_account_t account);
|
||||
bool dc_api_get_userinfo(dc_api_t api, dc_account_t login,
|
||||
dc_account_t user);
|
||||
|
||||
/**
|
||||
* Fetch a list of guilds fro the specified login user. Warning if you
|
||||
* unref the pointer array, you will also unref all the dc_guild_t objects.
|
||||
*/
|
||||
bool dc_api_get_userguilds(dc_api_t api, dc_account_t login,
|
||||
GPtrArray *guilds);
|
||||
GPtrArray **guilds);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -126,8 +126,11 @@ bool dc_account_has_token(dc_account_t a)
|
||||
void dc_account_set_id(dc_account_t a, char const *id)
|
||||
{
|
||||
return_if_true(a == NULL,);
|
||||
free(a->id);
|
||||
a->id = strdup(id);
|
||||
|
||||
if (a->id == NULL || strcmp(a->id, "@me")) {
|
||||
free(a->id);
|
||||
a->id = strdup(id);
|
||||
}
|
||||
}
|
||||
|
||||
char const *dc_account_id(dc_account_t a)
|
||||
|
||||
@@ -354,32 +354,58 @@ bool dc_api_get_userinfo(dc_api_t api, dc_account_t login,
|
||||
|
||||
cleanup:
|
||||
|
||||
if (reply != NULL) {
|
||||
json_decref(reply);
|
||||
reply = NULL;
|
||||
}
|
||||
free(url);
|
||||
json_decref(reply);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool dc_api_get_userguilds(dc_api_t api, dc_account_t login,
|
||||
|
||||
bool dc_api_get_userguilds(dc_api_t api, dc_account_t login, GPtrArray **out)
|
||||
{
|
||||
char *url = NULL;
|
||||
json_t *reply = NULL, *val = NULL;
|
||||
json_t *reply = NULL, *c = NULL, *val = NULL;
|
||||
size_t i = 0;
|
||||
bool ret = false;
|
||||
GPtrArray *guilds = g_ptr_array_new_with_free_func((GDestroyNotify)dc_unref);
|
||||
|
||||
return_if_true(api == NULL, false);
|
||||
return_if_true(login == NULL, false);
|
||||
return_if_true(user == NULL, false);
|
||||
|
||||
asprintf(&url, "users/%s/guilds", dc_account_id(user));
|
||||
asprintf(&url, "users/%s/guilds", dc_account_id(login));
|
||||
|
||||
reply = dc_api_call_sync(api, "GET", dc_account_token(login), url, NULL);
|
||||
goto_if_true(reply == NULL, cleanup);
|
||||
|
||||
goto_if_true(!json_is_array(reply), cleanup);
|
||||
|
||||
json_array_foreach(reply, i, c) {
|
||||
dc_guild_t g = dc_guild_new();
|
||||
|
||||
val = json_object_get(c, "id");
|
||||
goto_if_true(val == NULL || !json_is_string(val), cleanup);
|
||||
dc_guild_set_id(g, json_string_value(val));
|
||||
|
||||
val = json_object_get(c, "name");
|
||||
goto_if_true(val == NULL || !json_is_string(val), cleanup);
|
||||
dc_guild_set_name(g, json_string_value(val));
|
||||
|
||||
g_ptr_array_add(guilds, g);
|
||||
}
|
||||
|
||||
*out = guilds;
|
||||
guilds = NULL;
|
||||
|
||||
ret = true;
|
||||
|
||||
cleanup:
|
||||
|
||||
free(url);
|
||||
json_decref(reply);
|
||||
|
||||
if (guilds) {
|
||||
g_ptr_array_unref(guilds);
|
||||
guilds = NULL;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ dc_guild_t dc_guild_new(void)
|
||||
|
||||
p->ref.cleanup = (dc_cleanup_t)dc_guild_free;
|
||||
|
||||
return p;
|
||||
return dc_ref(p);
|
||||
}
|
||||
|
||||
char const *dc_guild_name(dc_guild_t d)
|
||||
|
||||
@@ -199,7 +199,7 @@ bool dc_loop_once(dc_loop_t l)
|
||||
struct CURLMsg *msg = NULL;
|
||||
size_t i = 0;
|
||||
|
||||
ret = event_base_loop(l->base, EVLOOP_ONCE|EVLOOP_NONBLOCK);
|
||||
ret = event_base_loop(l->base, EVLOOP_ONCE);
|
||||
if (ret < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user