add api function that gets the last time a friend was seen online

This commit is contained in:
Jfreegman 2014-03-13 20:20:48 -04:00
parent cb9b8fc7cd
commit 7d1060abc2
4 changed files with 41 additions and 6 deletions

View File

@ -276,7 +276,7 @@ int m_addfriend(Messenger *m, uint8_t *address, uint8_t *data, uint16_t length)
memcpy(&(m->friendlist[i].friendrequest_nospam), address + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t)); memcpy(&(m->friendlist[i].friendrequest_nospam), address + crypto_box_PUBLICKEYBYTES, sizeof(uint32_t));
if (m->numfriends == i) if (m->numfriends == i)
++ m->numfriends; ++m->numfriends;
return i; return i;
} }
@ -321,7 +321,7 @@ int m_addfriend_norequest(Messenger *m, uint8_t *client_id)
m->friendlist[i].receives_read_receipts = 1; /* Default: YES. */ m->friendlist[i].receives_read_receipts = 1; /* Default: YES. */
if (m->numfriends == i) if (m->numfriends == i)
++ m->numfriends; ++m->numfriends;
return i; return i;
} }
@ -617,6 +617,14 @@ USERSTATUS m_get_self_userstatus(Messenger *m)
return m->userstatus; return m->userstatus;
} }
uint64_t m_get_last_online(Messenger *m, int friendnumber)
{
if (friend_not_valid(m, friendnumber))
return -1;
return m->friendlist[friendnumber].ping_lastrecv;
}
int m_set_usertyping(Messenger *m, int friendnumber, uint8_t is_typing) int m_set_usertyping(Messenger *m, int friendnumber, uint8_t is_typing)
{ {
if (is_typing != 0 && is_typing != 1) { if (is_typing != 0 && is_typing != 1) {
@ -2353,6 +2361,7 @@ struct SAVED_FRIEND {
uint16_t statusmessage_length; uint16_t statusmessage_length;
uint8_t userstatus; uint8_t userstatus;
uint32_t friendrequest_nospam; uint32_t friendrequest_nospam;
uint64_t ping_lastrecv;
}; };
static uint32_t saved_friendslist_size(Messenger *m) static uint32_t saved_friendslist_size(Messenger *m)
@ -2382,6 +2391,11 @@ static uint32_t friends_list_save(Messenger *m, uint8_t *data)
memcpy(temp.statusmessage, m->friendlist[i].statusmessage, m->friendlist[i].statusmessage_length); memcpy(temp.statusmessage, m->friendlist[i].statusmessage, m->friendlist[i].statusmessage_length);
temp.statusmessage_length = htons(m->friendlist[i].statusmessage_length); temp.statusmessage_length = htons(m->friendlist[i].statusmessage_length);
temp.userstatus = m->friendlist[i].userstatus; temp.userstatus = m->friendlist[i].userstatus;
uint8_t lastonline[sizeof(uint64_t)];
memcpy(lastonline, &m->friendlist[i].ping_lastrecv, sizeof(uint64_t));
host_to_net(lastonline, sizeof(uint64_t));
memcpy(&temp.ping_lastrecv, lastonline, sizeof(uint64_t));
} }
memcpy(data + num * sizeof(struct SAVED_FRIEND), &temp, sizeof(struct SAVED_FRIEND)); memcpy(data + num * sizeof(struct SAVED_FRIEND), &temp, sizeof(struct SAVED_FRIEND));
@ -2409,6 +2423,11 @@ static int friends_list_load(Messenger *m, uint8_t *data, uint32_t length)
setfriendname(m, fnum, temp.name, ntohs(temp.name_length)); setfriendname(m, fnum, temp.name, ntohs(temp.name_length));
set_friend_statusmessage(m, fnum, temp.statusmessage, ntohs(temp.statusmessage_length)); set_friend_statusmessage(m, fnum, temp.statusmessage, ntohs(temp.statusmessage_length));
set_friend_userstatus(m, fnum, temp.userstatus); set_friend_userstatus(m, fnum, temp.userstatus);
uint8_t lastonline[sizeof(uint64_t)];
memcpy(lastonline, &temp.ping_lastrecv, sizeof(uint64_t));
net_to_host(lastonline, sizeof(uint64_t));
memcpy(&m->friendlist[fnum].ping_lastrecv, lastonline, sizeof(uint64_t));
} else if (temp.status != 0) { } else if (temp.status != 0) {
/* TODO: This is not a good way to do this. */ /* TODO: This is not a good way to do this. */
uint8_t address[FRIEND_ADDRESS_SIZE]; uint8_t address[FRIEND_ADDRESS_SIZE];
@ -2852,4 +2871,3 @@ uint32_t copy_chatlist(Messenger *m, int *out_list, uint32_t list_size)
return ret; return ret;
} }

View File

@ -404,6 +404,11 @@ int m_copy_self_statusmessage(Messenger *m, uint8_t *buf, uint32_t maxlen);
USERSTATUS m_get_userstatus(Messenger *m, int friendnumber); USERSTATUS m_get_userstatus(Messenger *m, int friendnumber);
USERSTATUS m_get_self_userstatus(Messenger *m); USERSTATUS m_get_self_userstatus(Messenger *m);
/* returns timestamp of last time friendnumber was seen online, or 0 if never seen.
* returns -1 on error.
*/
uint64_t m_get_last_online(Messenger *m, int friendnumber);
/* Set our typing status for a friend. /* Set our typing status for a friend.
* You are responsible for turning it on or off. * You are responsible for turning it on or off.
* *
@ -770,4 +775,3 @@ uint32_t count_chatlist(Messenger *m);
uint32_t copy_chatlist(Messenger *m, int *out_list, uint32_t list_size); uint32_t copy_chatlist(Messenger *m, int *out_list, uint32_t list_size);
#endif #endif

View File

@ -270,6 +270,15 @@ TOX_USERSTATUS tox_get_self_user_status(Tox *tox)
return (TOX_USERSTATUS)m_get_self_userstatus(m); return (TOX_USERSTATUS)m_get_self_userstatus(m);
} }
/* returns timestamp of last time friendnumber was seen online, or 0 if never seen.
* returns -1 on error.
*/
uint64_t tox_get_last_online(Tox *tox, int friendnumber)
{
Messenger *m = tox;
return m_get_last_online(m, friendnumber);
}
/* Set our typing status for a friend. /* Set our typing status for a friend.
* You are responsible for turning it on or off. * You are responsible for turning it on or off.
* *
@ -293,7 +302,6 @@ int tox_get_is_typing(Tox *tox, int friendnumber)
return m_get_istyping(m, friendnumber); return m_get_istyping(m, friendnumber);
} }
/* Sets whether we send read receipts for friendnumber. /* Sets whether we send read receipts for friendnumber.
* This function is not lazy, and it will fail if yesno is not (0 or 1). * This function is not lazy, and it will fail if yesno is not (0 or 1).
*/ */

View File

@ -280,6 +280,12 @@ int tox_get_self_status_message(Tox *tox, uint8_t *buf, uint32_t maxlen);
TOX_USERSTATUS tox_get_user_status(Tox *tox, int friendnumber); TOX_USERSTATUS tox_get_user_status(Tox *tox, int friendnumber);
TOX_USERSTATUS tox_get_self_user_status(Tox *tox); TOX_USERSTATUS tox_get_self_user_status(Tox *tox);
/* returns timestamp of last time friendnumber was seen online, or 0 if never seen.
* returns -1 on error.
*/
uint64_t tox_get_last_online(Tox *tox, int friendnumber);
/* Set our typing status for a friend. /* Set our typing status for a friend.
* You are responsible for turning it on or off. * You are responsible for turning it on or off.
* *
@ -726,4 +732,3 @@ int tox_load_encrypted(Tox *tox, uint8_t *data, uint32_t length, uint8_t *key, u
#endif #endif
#endif #endif