text
stringlengths
0
357
void dc_lookup_real_nchat_by_contact_id(dc_context_t* context, uint32_t contact_id, uint32_t* ret_chat_id, int* ret_chat_blocked)
{
/* checks for "real" chats or self-chat */
sqlite3_stmt* stmt = NULL;
if (ret_chat_id) { *ret_chat_id = 0; }
if (ret_chat_blocked) { *ret_chat_blocked = 0; }
if (context==NULL || context->magic!=DC_CONTEXT_MAGIC || context->sql->cobj==NULL) {
return; /* no database, no chats - this is no error (needed eg. for information) */
}
stmt = dc_sqlite3_prepare(context->sql,
"SELECT c.id, c.blocked"
" FROM chats c"
" INNER JOIN chats_contacts j ON c.id=j.chat_id"
" WHERE c.type=" DC_STRINGIFY(DC_CHAT_TYPE_SINGLE) " AND c.id>" DC_STRINGIFY(DC_CHAT_ID_LAST_SPECIAL) " AND j.contact_id=?;");
sqlite3_bind_int(stmt, 1, contact_id);
if (sqlite3_step(stmt)==SQLITE_ROW) {
if (ret_chat_id) { *ret_chat_id = sqlite3_column_int(stmt, 0); }
if (ret_chat_blocked) { *ret_chat_blocked = sqlite3_column_int(stmt, 1); }
}
sqlite3_finalize(stmt);
}
void dc_create_or_lookup_nchat_by_contact_id(dc_context_t* context, uint32_t contact_id, int create_blocked, uint32_t* ret_chat_id, int* ret_chat_blocked)
{
uint32_t chat_id = 0;
int chat_blocked = 0;
dc_contact_t* contact = NULL;
char* chat_name = NULL;
char* q = NULL;
sqlite3_stmt* stmt = NULL;
if (ret_chat_id) { *ret_chat_id = 0; }
if (ret_chat_blocked) { *ret_chat_blocked = 0; }
if (context==NULL || context->magic!=DC_CONTEXT_MAGIC || context->sql->cobj==NULL) {
return; /* database not opened - error */
}
if (contact_id==0) {
return;
}
dc_lookup_real_nchat_by_contact_id(context, contact_id, &chat_id, &chat_blocked);
if (chat_id!=0) {
if (ret_chat_id) { *ret_chat_id = chat_id; }
if (ret_chat_blocked) { *ret_chat_blocked = chat_blocked; }
return; /* soon success */
}
/* get fine chat name */
contact = dc_contact_new(context);
if (!dc_contact_load_from_db(contact, context->sql, contact_id)) {
goto cleanup;
}
chat_name = (contact->name&&contact->name[0])? contact->name : contact->addr;
/* create chat record; the grpid is only used to make dc_sqlite3_get_rowid() work (we cannot use last_insert_id() due multi-threading) */
q = sqlite3_mprintf("INSERT INTO chats (type, name, param, blocked, grpid) VALUES(%i, %Q, %Q, %i, %Q)", DC_CHAT_TYPE_SINGLE, chat_name,
contact_id==DC_CONTACT_ID_SELF? "K=1" : "", create_blocked, contact->addr);
assert( DC_PARAM_SELFTALK=='K');
stmt = dc_sqlite3_prepare(context->sql, q);
if (stmt==NULL) {
goto cleanup;
}
if (sqlite3_step(stmt)!=SQLITE_DONE) {
goto cleanup;
}
chat_id = dc_sqlite3_get_rowid(context->sql, "chats", "grpid", contact->addr);
sqlite3_free(q);
q = NULL;
sqlite3_finalize(stmt);
stmt = NULL;
/* add contact IDs to the new chat record (may be replaced by dc_add_to_chat_contacts_table()) */
q = sqlite3_mprintf("INSERT INTO chats_contacts (chat_id, contact_id) VALUES(%i, %i)", chat_id, contact_id);
stmt = dc_sqlite3_prepare(context->sql, q);
if (sqlite3_step(stmt)!=SQLITE_DONE) {
goto cleanup;
}
sqlite3_free(q);
q = NULL;
sqlite3_finalize(stmt);
stmt = NULL;
cleanup:
sqlite3_free(q);
sqlite3_finalize(stmt);
dc_contact_unref(contact);