text
stringlengths
0
357
sqlite3_bind_int (stmt, 2, chat_id);
}
else {
dc_log_info(context, 0, "set gossiped_timestamp for all chats to %i.",
(int)timestamp);
stmt = dc_sqlite3_prepare(context->sql,
"UPDATE chats SET gossiped_timestamp=?;");
sqlite3_bind_int64(stmt, 1, timestamp);
}
sqlite3_step(stmt);
sqlite3_finalize(stmt);
}
void dc_reset_gossiped_timestamp(dc_context_t* context, uint32_t chat_id)
{
dc_set_gossiped_timestamp(context, chat_id, 0);
}
/*******************************************************************************
* Context functions to work with chats
******************************************************************************/
size_t dc_get_chat_cnt(dc_context_t* context)
{
size_t ret = 0;
sqlite3_stmt* stmt = NULL;
if (context==NULL || context->magic!=DC_CONTEXT_MAGIC || context->sql->cobj==NULL) {
goto cleanup; /* no database, no chats - this is no error (needed eg. for information) */
}
stmt = dc_sqlite3_prepare(context->sql,
"SELECT COUNT(*) FROM chats WHERE id>" DC_STRINGIFY(DC_CHAT_ID_LAST_SPECIAL) " AND blocked=0;");
if (sqlite3_step(stmt)!=SQLITE_ROW) {
goto cleanup;
}
ret = sqlite3_column_int(stmt, 0);
cleanup:
sqlite3_finalize(stmt);
return ret;
}
int dc_add_to_chat_contacts_table(dc_context_t* context, uint32_t chat_id, uint32_t contact_id)
{
/* add a contact to a chat; the function does not check the type or if any of the record exist or are already added to the chat! */
int ret = 0;
sqlite3_stmt* stmt = dc_sqlite3_prepare(context->sql,
"INSERT INTO chats_contacts (chat_id, contact_id) VALUES(?, ?)");
sqlite3_bind_int(stmt, 1, chat_id);
sqlite3_bind_int(stmt, 2, contact_id);
ret = (sqlite3_step(stmt)==SQLITE_DONE)? 1 : 0;
sqlite3_finalize(stmt);
return ret;
}
/**
* Get chat object by a chat ID.
*
* @memberof dc_context_t
* @param context The context object as returned from dc_context_new().
* @param chat_id The ID of the chat to get the chat object for.
* @return A chat object of the type dc_chat_t,
* must be freed using dc_chat_unref() when done.
* On errors, NULL is returned.
*/
dc_chat_t* dc_get_chat(dc_context_t* context, uint32_t chat_id)
{
int success = 0;
dc_chat_t* obj = dc_chat_new(context);
if (context==NULL || context->magic!=DC_CONTEXT_MAGIC) {
goto cleanup;
}
if (!dc_chat_load_from_db(obj, chat_id)) {
goto cleanup;
}
success = 1;
cleanup:
if (success) {
return obj;
}
else {
dc_chat_unref(obj);
return NULL;
}
}