1
0
mirror of https://github.com/Tha14/toxic.git synced 2025-06-28 21:26:46 +02:00

implement read receipts

This commit is contained in:
Jfreegman
2014-09-07 02:43:53 -04:00
parent 3c2c1f15ce
commit 5b9bd603ea
10 changed files with 141 additions and 35 deletions

View File

@ -25,6 +25,8 @@
#include "toxic.h"
#include "windows.h"
#include "message_queue.h"
#include "misc_tools.h"
#include "line_info.h"
void cqueue_cleanup(struct chat_queue *q)
{
@ -41,7 +43,7 @@ void cqueue_cleanup(struct chat_queue *q)
void cqueue_add(struct chat_queue *q, const char *msg, int len, uint8_t type, uint32_t line_id)
{
struct cqueue_msg *new_m = malloc(sizeof(struct cqueue_msg));
struct cqueue_msg *new_m = calloc(1, sizeof(struct cqueue_msg));
if (new_m == NULL)
exit_toxic_err("failed in cqueue_message", FATALERR_MEMORY);
@ -50,38 +52,90 @@ void cqueue_add(struct chat_queue *q, const char *msg, int len, uint8_t type, ui
new_m->len = len;
new_m->type = type;
new_m->line_id = line_id;
new_m->next = NULL;
if (q->root == NULL)
if (q->root == NULL) {
new_m->prev = NULL;
q->root = new_m;
else
} else {
new_m->prev = q->end;
q->end->next = new_m;
}
q->end = new_m;
}
static void cqueue_remove(struct chat_queue *q)
/* update line to show receipt was received after queue removal */
static void cqueue_mark_read(ToxWindow *self, uint32_t id, uint8_t type)
{
struct cqueue_msg *new_root = q->root->next;
free(q->root);
q->root = new_root;
struct line_info *line = self->chatwin->hst->line_end;
while (line) {
if (line->id == id) {
line->type = type == OUT_ACTION ? OUT_ACTION_READ : OUT_MSG_READ;
line->len -= 2; /* removes " x" */
return;
}
line = line->prev;
}
}
void cqueue_try_send(Tox *m, struct chat_queue *q)
/* removes the message with the same receipt number from queue and updates line to show the message was received*/
void cqueue_remove(ToxWindow *self, struct chat_queue *q, uint32_t receipt)
{
struct cqueue_msg *q_msg = q->root;
while (q_msg) {
struct cqueue_msg *next = q_msg->next;
if (q_msg->receipt == receipt) {
uint32_t line_id = q_msg->line_id;
uint8_t type = q_msg->type;
if (q_msg->prev == NULL) {
if (next)
next->prev = NULL;
free(q->root);
q->root = next;
} else {
q_msg->prev->next = next;
free(q_msg);
}
cqueue_mark_read(self, line_id, type);
return;
}
q_msg = next;
}
}
#define CQUEUE_TRY_SEND_INTERVAL 5
/* Tries to send oldest message in queue. If fails, tries again in CQUEUE_TRY_SEND_INTERVAL seconds */
void cqueue_try_send(ToxWindow *self, Tox *m)
{
struct chat_queue *q = self->chatwin->cqueue;
if (q->root == NULL)
return;
struct cqueue_msg *q_msg = q->root;
uint32_t receipt;
uint64_t curtime = get_unix_time();
if (q_msg->type == QMESSAGE)
/* allow some time for a laggy read receipt before resending
TODO: timeout should be removed when receipts are fixed in core) */
if (q_msg->receipt != 0 && !timed_out(q_msg->last_send_try, curtime, CQUEUE_TRY_SEND_INTERVAL))
return;
uint32_t receipt = 0;
if (q_msg->type == OUT_MSG)
receipt = tox_send_message(m, q->friendnum, (uint8_t *) q_msg->message, q_msg->len);
else
receipt = tox_send_action(m, q->friendnum, (uint8_t *) q_msg->message, q_msg->len);
if (receipt == 0)
return;
cqueue_remove(q);
q->root->last_send_try = curtime;
q->root->receipt = receipt;
}