diff --git a/include/linphone/api/c-chat-room.h b/include/linphone/api/c-chat-room.h index 42c6b7dee..948a3708a 100644 --- a/include/linphone/api/c-chat-room.h +++ b/include/linphone/api/c-chat-room.h @@ -202,6 +202,13 @@ LINPHONE_PUBLIC bctbx_list_t *linphone_chat_room_get_history_events (LinphoneCha */ LINPHONE_PUBLIC bctbx_list_t *linphone_chat_room_get_history_range_events (LinphoneChatRoom *cr, int begin, int end); +/** + * Gets the number of events in a chat room. + * @param[in] cr The #LinphoneChatRoom object corresponding to the conversation for which size has to be computed + * @return the number of events. + */ +LINPHONE_PUBLIC int linphone_chat_room_get_history_events_size(LinphoneChatRoom *cr); + /** * Gets the last chat message sent or received in this chat room * @param[in] cr The #LinphoneChatRoom object corresponding to the conversation for which last message should be retrieved diff --git a/src/c-wrapper/api/c-chat-room.cpp b/src/c-wrapper/api/c-chat-room.cpp index ecae6ba28..48930535e 100644 --- a/src/c-wrapper/api/c-chat-room.cpp +++ b/src/c-wrapper/api/c-chat-room.cpp @@ -180,7 +180,7 @@ int linphone_chat_room_get_unread_messages_count (LinphoneChatRoom *cr) { } int linphone_chat_room_get_history_size (LinphoneChatRoom *cr) { - return L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getHistorySize(); + return L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getChatMessagesCount(); } void linphone_chat_room_delete_message (LinphoneChatRoom *cr, LinphoneChatMessage *msg) { @@ -216,6 +216,10 @@ bctbx_list_t *linphone_chat_room_get_history_range_events (LinphoneChatRoom *cr, return L_GET_RESOLVED_C_LIST_FROM_CPP_LIST(L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getHistoryRange(begin, end)); } +int linphone_chat_room_get_history_events_size(LinphoneChatRoom *cr) { + return L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getHistorySize(); +} + LinphoneChatMessage *linphone_chat_room_get_last_message_in_history(LinphoneChatRoom *cr) { shared_ptr cppPtr = L_GET_CPP_PTR_FROM_C_OBJECT(cr)->getLastChatMessageInHistory(); if (!cppPtr) diff --git a/src/chat/chat-room/chat-room.cpp b/src/chat/chat-room/chat-room.cpp index ce9e6af53..35a8f31cc 100644 --- a/src/chat/chat-room/chat-room.cpp +++ b/src/chat/chat-room/chat-room.cpp @@ -354,7 +354,7 @@ list> ChatRoom::getHistoryRange (int begin, int end) { } int ChatRoom::getHistorySize () { - return getCore()->getPrivate()->mainDb->getChatMessagesCount(getChatRoomId()); + return getCore()->getPrivate()->mainDb->getHistorySize(getChatRoomId()); } shared_ptr ChatRoom::getLastChatMessageInHistory() const { @@ -365,6 +365,10 @@ void ChatRoom::deleteHistory () { getCore()->getPrivate()->mainDb->cleanHistory(getChatRoomId()); } +int ChatRoom::getChatMessagesCount () { + return getCore()->getPrivate()->mainDb->getChatMessagesCount(getChatRoomId()); +} + int ChatRoom::getUnreadChatMessagesCount () { return getCore()->getPrivate()->mainDb->getUnreadChatMessagesCount(getChatRoomId()); } diff --git a/src/chat/chat-room/chat-room.h b/src/chat/chat-room/chat-room.h index 36dd0e998..5b253a1e6 100644 --- a/src/chat/chat-room/chat-room.h +++ b/src/chat/chat-room/chat-room.h @@ -68,6 +68,7 @@ public: void deleteHistory (); + int getChatMessagesCount (); int getUnreadChatMessagesCount (); // TODO: Remove useless functions. diff --git a/src/db/main-db.cpp b/src/db/main-db.cpp index 786123d2f..549c4ffa2 100644 --- a/src/db/main-db.cpp +++ b/src/db/main-db.cpp @@ -469,7 +469,8 @@ MainDb::MainDb (const shared_ptr &core) : AbstractDb(*new MainDbPrivate), // 2 - Fetch contents. { soci::session *session = dbSession.getBackendSession(); - const string query = "SELECT chat_message_content.id, content_type.id, content_type.value, body FROM chat_message_content, content_type" + static const string query = "SELECT chat_message_content.id, content_type.id, content_type.value, body" + " FROM chat_message_content, content_type" " WHERE event_id = :eventId AND content_type_id = content_type.id"; soci::rowset rows = (session->prepare << query, soci::use(eventId)); for (const auto &row : rows) { @@ -1314,7 +1315,7 @@ MainDb::MainDb (const shared_ptr &core) : AbstractDb(*new MainDbPrivate), return 0; } - string query = "SELECT COUNT(*) FROM event" + + static string query = "SELECT COUNT(*) FROM event" + buildSqlEventFilter({ ConferenceCallFilter, ConferenceChatMessageFilter, ConferenceInfoFilter }, mask); int count = 0; @@ -1354,7 +1355,7 @@ MainDb::MainDb (const shared_ptr &core) : AbstractDb(*new MainDbPrivate), return event; // TODO: Improve. Deal with all events in the future. - static string query = "SELECT peer_sip_address.value, local_sip_address.value, type, event.creation_time" + static const string query = "SELECT peer_sip_address.value, local_sip_address.value, type, event.creation_time" " FROM event, conference_event, chat_room, sip_address AS peer_sip_address, sip_address as local_sip_address" " WHERE event.id = :eventId" " AND conference_event.event_id = event.id" @@ -1639,8 +1640,7 @@ MainDb::MainDb (const shared_ptr &core) : AbstractDb(*new MainDbPrivate), return chatMessages; } - const long long &dbChatRoomId = d->selectChatRoomId(chatRoomId); - string query = "SELECT id, type, creation_time FROM event" + static const string query = "SELECT id, type, creation_time FROM event" " WHERE id IN (" " SELECT event_id FROM conference_event" " WHERE event_id IN (SELECT event_id FROM conference_chat_message_event WHERE imdn_message_id = :imdnMessageId)" @@ -1657,6 +1657,7 @@ MainDb::MainDb (const shared_ptr &core) : AbstractDb(*new MainDbPrivate), soci::session *session = d->dbSession.getBackendSession(); soci::transaction tr(*session); + const long long &dbChatRoomId = d->selectChatRoomId(chatRoomId); soci::rowset rows = (session->prepare << query, soci::use(imdnMessageId), soci::use(dbChatRoomId)); for (const auto &row : rows) { long long eventId = d->resolveId(row, 0); @@ -1768,6 +1769,35 @@ MainDb::MainDb (const shared_ptr &core) : AbstractDb(*new MainDbPrivate), return list>(); } + int MainDb::getHistorySize (const ChatRoomId &chatRoomId, FilterMask mask) const { + L_D(); + + if (!isConnected()) { + lWarning() << "Unable to get history size. Not connected."; + return 0; + } + + int count = 0; + + const string query = "SELECT COUNT(*) FROM event, conference_event" + " WHERE chat_room_id = :chatRoomId" + " AND event_id = event.id" + + buildSqlEventFilter({ + ConferenceCallFilter, ConferenceChatMessageFilter, ConferenceInfoFilter + }, mask, "AND"); + + L_BEGIN_LOG_EXCEPTION + + const long long &dbChatRoomId = d->selectChatRoomId(chatRoomId); + + soci::session *session = d->dbSession.getBackendSession(); + *session << query, soci::into(count), soci::use(dbChatRoomId); + + L_END_LOG_EXCEPTION + + return count; + } + void MainDb::cleanHistory (const ChatRoomId &chatRoomId, FilterMask mask) { L_D(); @@ -1776,7 +1806,7 @@ MainDb::MainDb (const shared_ptr &core) : AbstractDb(*new MainDbPrivate), return; } - string query = "SELECT event_id FROM conference_event WHERE chat_room_id = :chatRoomId" + + const string query = "SELECT event_id FROM conference_event WHERE chat_room_id = :chatRoomId" + buildSqlEventFilter({ ConferenceCallFilter, ConferenceChatMessageFilter, ConferenceInfoFilter }, mask); @@ -1852,7 +1882,7 @@ MainDb::MainDb (const shared_ptr &core) : AbstractDb(*new MainDbPrivate), list> participants; const long long &dbChatRoomId = d->resolveId(row, 0); - string query = "SELECT sip_address.value, is_admin" + static const string query = "SELECT sip_address.value, is_admin" " FROM sip_address, chat_room, chat_room_participant" " WHERE chat_room.id = :chatRoomId" " AND sip_address.id = chat_room_participant.participant_sip_address_id" @@ -2193,6 +2223,10 @@ MainDb::MainDb (const shared_ptr &core) : AbstractDb(*new MainDbPrivate), return list>(); } + int getHistorySize (const ChatRoomId &, FilterMask) const { + return 0; + } + list> MainDb::getChatRooms () const { return list>(); } diff --git a/src/db/main-db.h b/src/db/main-db.h index 907dd4e8c..f91578573 100644 --- a/src/db/main-db.h +++ b/src/db/main-db.h @@ -104,6 +104,9 @@ public: int end, FilterMask mask = NoFilter ) const; + + int getHistorySize (const ChatRoomId &chatRoomId, FilterMask mask = NoFilter) const; + void cleanHistory (const ChatRoomId &chatRoomId, FilterMask mask = NoFilter); // ---------------------------------------------------------------------------