text
stringlengths
0
357
uint32_t chat_id = 0;
int chat_id_blocked = 0;
if (context==NULL || context->magic!=DC_CONTEXT_MAGIC) {
return 0;
}
dc_lookup_real_nchat_by_contact_id(context, contact_id, &chat_id, &chat_id_blocked);
return chat_id_blocked? 0 : chat_id; /* from outside view, chats only existing in the deaddrop do not exist */
}
uint32_t dc_get_chat_id_by_grpid(dc_context_t* context, const char* grpid, int* ret_blocked, int* ret_verified)
{
uint32_t chat_id = 0;
sqlite3_stmt* stmt = NULL;
if(ret_blocked) { *ret_blocked = 0; }
if(ret_verified) { *ret_verified = 0; }
if (context==NULL || grpid==NULL) {
goto cleanup;
}
stmt = dc_sqlite3_prepare(context->sql,
"SELECT id, blocked, type FROM chats WHERE grpid=?;");
sqlite3_bind_text (stmt, 1, grpid, -1, SQLITE_STATIC);
if (sqlite3_step(stmt)==SQLITE_ROW) {
chat_id = sqlite3_column_int(stmt, 0);
if(ret_blocked) { *ret_blocked = sqlite3_column_int(stmt, 1); }
if(ret_verified) { *ret_verified = (sqlite3_column_int(stmt, 2)==DC_CHAT_TYPE_VERIFIED_GROUP); }
}
cleanup:
sqlite3_finalize(stmt);
return chat_id;
}
/**
* Create a normal chat with a single user. To create group chats,
* see dc_create_group_chat().
*
* If a chat already exists, this ID is returned, otherwise a new chat is created;
* this new chat may already contain messages, eg. from the deaddrop, to get the
* chat messages, use dc_get_chat_msgs().
*
* @memberof dc_context_t
* @param context The context object as returned from dc_context_new().
* @param contact_id The contact ID to create the chat for. If there is already
* a chat with this contact, the already existing ID is returned.
* @return The created or reused chat ID on success. 0 on errors.
*/
uint32_t dc_create_chat_by_contact_id(dc_context_t* context, uint32_t contact_id)
{
uint32_t chat_id = 0;
int chat_blocked = 0;
int send_event = 0;
if (context==NULL || context->magic!=DC_CONTEXT_MAGIC) {
return 0;
}
dc_lookup_real_nchat_by_contact_id(context, contact_id, &chat_id, &chat_blocked);
if (chat_id) {
if (chat_blocked) {
dc_unblock_chat(context, chat_id); /* unblock chat (typically move it from the deaddrop to view) */
send_event = 1;
}
goto cleanup; /* success */
}
if (0==dc_real_contact_exists(context, contact_id) && contact_id!=DC_CONTACT_ID_SELF) {
dc_log_warning(context, 0, "Cannot create chat, contact %i does not exist.", (int)contact_id);
goto cleanup;
}
dc_create_or_lookup_nchat_by_contact_id(context, contact_id, DC_CHAT_NOT_BLOCKED, &chat_id, NULL);
if (chat_id) {
send_event = 1;
}
dc_scaleup_contact_origin(context, contact_id, DC_ORIGIN_CREATE_CHAT);
cleanup:
if (send_event) {
context->cb(context, DC_EVENT_MSGS_CHANGED, 0, 0);
}
return chat_id;
}
/**
* Create a normal chat or a group chat by a messages ID that comes typically
* from the deaddrop, DC_CHAT_ID_DEADDROP (1).
*
* If the given message ID already belongs to a normal chat or to a group chat,
* the chat ID of this chat is returned and no new chat is created.