text
stringlengths
0
357
cleanup:
sqlite3_finalize(stmt);
return ret;
}
/**
* Load a chatlist from the database to the chatlist object.
*
* @private @memberof dc_chatlist_t
*/
static int dc_chatlist_load_from_db(dc_chatlist_t* chatlist, int listflags, const char* query__, uint32_t query_contact_id)
{
//clock_t start = clock();
int success = 0;
int add_archived_link_item = 0;
sqlite3_stmt* stmt = NULL;
char* strLikeCmd = NULL;
char* query = NULL;
if (chatlist==NULL || chatlist->magic!=DC_CHATLIST_MAGIC || chatlist->context==NULL) {
goto cleanup;
}
dc_chatlist_empty(chatlist);
// select with left join and minimum:
// - the inner select must use `hidden` and _not_ `m.hidden`
// which would refer the outer select and take a lot of time
// - `GROUP BY` is needed several messages may have the same timestamp
// - the list starts with the newest chats
#define QUR1 "SELECT c.id, m.id FROM chats c " \
" LEFT JOIN msgs m " \
" ON c.id=m.chat_id " \
" AND m.timestamp=( " \
"SELECT MAX(timestamp) " \
" FROM msgs " \
" WHERE chat_id=c.id " \
" AND (hidden=0 OR (hidden=1 AND state=" DC_STRINGIFY(DC_STATE_OUT_DRAFT) ")))" \
" WHERE c.id>" DC_STRINGIFY(DC_CHAT_ID_LAST_SPECIAL) \
" AND c.blocked=0"
#define QUR2 " GROUP BY c.id " \
" ORDER BY IFNULL(m.timestamp,0) DESC, m.id DESC;"
// nb: the query currently shows messages from blocked contacts in groups.
// however, for normal-groups, this is okay as the message is also returned by dc_get_chat_msgs()
// (otherwise it would be hard to follow conversations, wa and tg do the same)
// for the deaddrop, however, they should really be hidden, however, _currently_ the deaddrop is not
// shown at all permanent in the chatlist.
if (query_contact_id)
{
// show chats shared with a given contact
stmt = dc_sqlite3_prepare(chatlist->context->sql,
QUR1 " AND c.id IN(SELECT chat_id FROM chats_contacts WHERE contact_id=?) " QUR2);
sqlite3_bind_int(stmt, 1, query_contact_id);
}
else if (listflags & DC_GCL_ARCHIVED_ONLY)
{
/* show archived chats */
stmt = dc_sqlite3_prepare(chatlist->context->sql,
QUR1 " AND c.archived=1 " QUR2);
}
else if (query__==NULL)
{
/* show normal chatlist */
if (!(listflags & DC_GCL_NO_SPECIALS)) {
uint32_t last_deaddrop_fresh_msg_id = get_last_deaddrop_fresh_msg(chatlist->context);
if (last_deaddrop_fresh_msg_id > 0) {
dc_array_add_id(chatlist->chatNlastmsg_ids, DC_CHAT_ID_DEADDROP); /* show deaddrop with the last fresh message */
dc_array_add_id(chatlist->chatNlastmsg_ids, last_deaddrop_fresh_msg_id);
}
add_archived_link_item = 1;
}
stmt = dc_sqlite3_prepare(chatlist->context->sql,
QUR1 " AND c.archived=0 " QUR2);
}
else
{
/* show chatlist filtered by a search string, this includes archived and unarchived */
query = dc_strdup(query__);
dc_trim(query);
if (query[0]==0) {
success = 1; /*empty result*/
goto cleanup;
}
strLikeCmd = dc_mprintf("%%%s%%", query);
stmt = dc_sqlite3_prepare(chatlist->context->sql,
QUR1 " AND c.name LIKE ? " QUR2);
sqlite3_bind_text(stmt, 1, strLikeCmd, -1, SQLITE_STATIC);
}
while (sqlite3_step(stmt)==SQLITE_ROW)
{
dc_array_add_id(chatlist->chatNlastmsg_ids, sqlite3_column_int(stmt, 0));
dc_array_add_id(chatlist->chatNlastmsg_ids, sqlite3_column_int(stmt, 1));
}