implement channel acking and new messages

This commit is contained in:
2019-07-12 18:30:44 +02:00
parent b8fa202ce3
commit ab65d49605
10 changed files with 139 additions and 11 deletions

View File

@@ -96,6 +96,13 @@ bool dc_api_get_messages(dc_api_t api, dc_account_t login, dc_channel_t c);
bool dc_api_post_message(dc_api_t api, dc_account_t login,
dc_channel_t c, dc_message_t m);
/**
* "ack" a channel, meaning that you have read it its contents. You must provide
* a message, so that discord knows which message was the last you read.
*/
bool dc_api_channel_ack(dc_api_t api, dc_account_t login,
dc_channel_t c, dc_message_t msg);
/**
* Fetch a list of friends of the login account "login". The friends are stored
* within the login object.

View File

@@ -67,4 +67,7 @@ void dc_channel_add_messages(dc_channel_t c, dc_message_t *m, size_t s);
bool dc_channel_compare(dc_channel_t a, dc_channel_t b);
bool dc_channel_has_new_messages(dc_channel_t c);
void dc_channel_mark_read(dc_channel_t c);
#endif

View File

@@ -2,6 +2,40 @@
#include "internal.h"
bool dc_api_channel_ack(dc_api_t api, dc_account_t login,
dc_channel_t c, dc_message_t m)
{
bool ret = false;
char *url = NULL;
json_t *reply = NULL, *j = NULL;
return_if_true(api == NULL || login == NULL ||
c == NULL || m == NULL, false);
asprintf(&url, "channels/%s/messages/%s/ack",
dc_channel_id(c),
dc_message_id(m)
);
goto_if_true(url == NULL, cleanup);
j = json_object();
goto_if_true(j == NULL, cleanup);
json_object_set_new(j, "token", json_string(TOKEN(login)));
reply = dc_api_call_sync(api, "POST", TOKEN(login), url, j);
goto_if_true(reply != NULL, cleanup);
ret = true;
cleanup:
free(url);
json_decref(reply);
json_decref(j);
return ret;
}
bool dc_api_post_message(dc_api_t api, dc_account_t login,
dc_channel_t c, dc_message_t m)
{

View File

@@ -46,6 +46,7 @@ struct dc_channel_
GHashTable *messages_byid;
GPtrArray *messages;
bool new_messages;
};
static void dc_channel_free(dc_channel_t c)
@@ -318,6 +319,8 @@ void dc_channel_add_messages(dc_channel_t c, dc_message_t *m, size_t s)
g_hash_table_insert(c->messages_byid, strdup(id), dc_ref(m[i]));
g_ptr_array_add(c->messages, dc_ref(m[i]));
c->new_messages = true;
}
g_ptr_array_sort(c->messages, (GCompareFunc)dc_message_compare);
@@ -329,3 +332,15 @@ bool dc_channel_compare(dc_channel_t a, dc_channel_t b)
return_if_true(a->id == NULL || b->id == NULL, false);
return (strcmp(a->id, b->id) == 0);
}
bool dc_channel_has_new_messages(dc_channel_t c)
{
return_if_true(c == NULL, false);
return c->new_messages;
}
void dc_channel_mark_read(dc_channel_t c)
{
return_if_true(c == NULL,);
c->new_messages = false;
}