text
stringlengths
0
357
}
}
return 0;
}
/*******************************************************************************
* Misc. Tools
******************************************************************************/
static void calc_timestamps(dc_context_t* context, uint32_t chat_id, uint32_t from_id, time_t message_timestamp, int is_fresh_msg,
time_t* sort_timestamp, time_t* sent_timestamp, time_t* rcvd_timestamp)
{
*rcvd_timestamp = time(NULL);
*sent_timestamp = message_timestamp;
if (*sent_timestamp > *rcvd_timestamp /* no sending times in the future */) {
*sent_timestamp = *rcvd_timestamp;
}
*sort_timestamp = message_timestamp; /* truncatd below to smeared time (not to _now_ to keep the order) */
/* use the last message from another user (including SELF) as the MINIMUM for sort_timestamp;
this is to force fresh messages popping up at the end of the list.
(we do this check only for fresh messages, other messages may pop up whereever, this may happen eg. when restoring old messages or synchronizing different clients) */
if (is_fresh_msg)
{
sqlite3_stmt* stmt = dc_sqlite3_prepare(context->sql,
"SELECT MAX(timestamp) FROM msgs WHERE chat_id=? and from_id!=? AND timestamp>=?");
sqlite3_bind_int (stmt, 1, chat_id);
sqlite3_bind_int (stmt, 2, from_id);
sqlite3_bind_int64(stmt, 3, *sort_timestamp);
if (sqlite3_step(stmt)==SQLITE_ROW)
{
time_t last_msg_time = sqlite3_column_int64(stmt, 0);
if (last_msg_time > 0 /* may happen as we do not check against sqlite3_column_type()!=SQLITE_NULL */) {
if (*sort_timestamp <= last_msg_time) {
*sort_timestamp = last_msg_time+1; /* this may result in several incoming messages having the same
one-second-after-the-last-other-message-timestamp. however, this is no big deal
as we do not try to recrete the order of bad-date-messages and as we always order by ID as second criterion */
}
}
}
sqlite3_finalize(stmt);
}
/* use the (smeared) current time as the MAXIMUM */
if (*sort_timestamp >= dc_smeared_time(context)) {
*sort_timestamp = dc_create_smeared_timestamp(context);
}
}
static dc_array_t* search_chat_ids_by_contact_ids(dc_context_t* context, const dc_array_t* unsorted_contact_ids)
{
/* searches chat_id's by the given contact IDs, may return zero, one or more chat_id's */
sqlite3_stmt* stmt = NULL;
dc_array_t* contact_ids = dc_array_new(context, 23);
char* contact_ids_str = NULL;
char* q3 = NULL;
dc_array_t* chat_ids = dc_array_new(context, 23);
if (context==NULL || context->magic!=DC_CONTEXT_MAGIC) {
goto cleanup;
}
/* copy array, remove duplicates and SELF, sort by ID */
{
int i, iCnt = dc_array_get_cnt(unsorted_contact_ids);
if (iCnt <= 0) {
goto cleanup;
}
for (i = 0; i < iCnt; i++) {
uint32_t curr_id = dc_array_get_id(unsorted_contact_ids, i);
if (curr_id!=DC_CONTACT_ID_SELF && !dc_array_search_id(contact_ids, curr_id, NULL)) {
dc_array_add_id(contact_ids, curr_id);
}
}
if (dc_array_get_cnt(contact_ids)==0) {
goto cleanup;
}
dc_array_sort_ids(contact_ids); /* for easy comparison, we also sort the sql result below */
}
/* collect all possible chats with the contact count as the data (as contact_ids have no doubles, this is sufficient) */
contact_ids_str = dc_array_get_string(contact_ids, ",");
q3 = sqlite3_mprintf("SELECT DISTINCT cc.chat_id, cc.contact_id "
" FROM chats_contacts cc "
" LEFT JOIN chats c ON c.id=cc.chat_id "
" WHERE cc.chat_id IN(SELECT chat_id FROM chats_contacts WHERE contact_id IN(%s))"
" AND c.type=" DC_STRINGIFY(DC_CHAT_TYPE_GROUP) /* no verified groups and no single chats (which are equal to a group with a single member and without SELF) */
" AND cc.contact_id!=" DC_STRINGIFY(DC_CONTACT_ID_SELF) /* ignore SELF, we've also removed it above - if the user has left the group, it is still the same group */
" ORDER BY cc.chat_id, cc.contact_id;",
contact_ids_str);
stmt = dc_sqlite3_prepare(context->sql, q3);