From 076c7715ec0054697d8afb58d3f2271149360b43 Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Wed, 14 Feb 2018 11:36:25 +0100 Subject: [PATCH] feat(MainDb): delete chat_message_participant when message is displayed --- src/db/main-db.cpp | 110 +++++++++++++++++++----------------------- tester/db/linphone.db | Bin 4382720 -> 4382720 bytes 2 files changed, 49 insertions(+), 61 deletions(-) diff --git a/src/db/main-db.cpp b/src/db/main-db.cpp index 92cb2d078..84ec4112c 100644 --- a/src/db/main-db.cpp +++ b/src/db/main-db.cpp @@ -17,9 +17,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ -#include #include +#include "linphone/utils/algorithm.h" #include "linphone/utils/utils.h" #include "chat/chat-message/chat-message-p.h" @@ -216,6 +216,7 @@ static constexpr const char *mapEnumToSql (const EnumToSql enumToSql[], size_ ); } +// Update me event-log-enums values are changed! static constexpr EnumToSql eventFilterToSql[] = { { MainDb::ConferenceCallFilter, "3, 4" }, { MainDb::ConferenceChatMessageFilter, "5" }, @@ -235,11 +236,7 @@ static string buildSqlEventFilter ( MainDb::FilterMask mask, const string &condKeyWord = "WHERE" ) { - L_ASSERT( - find_if(filters.cbegin(), filters.cend(), [](const MainDb::Filter &filter) { - return filter == MainDb::NoFilter; - }) == filters.cend() - ); + L_ASSERT(findIf(filters, [](const MainDb::Filter &filter) { return filter == MainDb::NoFilter; }) == filters.cend()); if (mask == MainDb::NoFilter) return ""; @@ -475,7 +472,7 @@ void MainDbPrivate::insertChatMessageParticipant (long long eventId, long long s soci::use(state), soci::use(eventId), soci::use(sipAddressId) ); statement.execute(); - if (statement.get_affected_rows() == 0 && state != static_cast(ChatMessage::State::Displayed)) + if (statement.get_affected_rows() == 0 && state != int(ChatMessage::State::Displayed)) *session << "INSERT INTO chat_message_participant (event_id, participant_sip_address_id, state)" " VALUES (:eventId, :sipAddressId, :state)", soci::use(eventId), soci::use(sipAddressId), soci::use(state); @@ -692,12 +689,12 @@ shared_ptr MainDbPrivate::selectConferenceChatMessageEvent ( chatMessage = shared_ptr(new ChatMessage( chatRoom, - static_cast(direction) + ChatMessage::Direction(direction) )); - chatMessage->setIsSecured(static_cast(isSecured)); + chatMessage->setIsSecured(bool(isSecured)); ChatMessagePrivate *dChatMessage = chatMessage->getPrivate(); - dChatMessage->setState(static_cast(state), true); + dChatMessage->setState(ChatMessage::State(state), true); dChatMessage->forceFromAddress(IdentityAddress(fromSipAddress)); dChatMessage->forceToAddress(IdentityAddress(toSipAddress)); @@ -734,7 +731,7 @@ shared_ptr MainDbPrivate::selectConferenceChatMessageEvent ( FileContent *fileContent = new FileContent(); fileContent->setFileName(name); - fileContent->setFileSize(static_cast(size)); + fileContent->setFileSize(size_t(size)); fileContent->setFilePath(path); content = fileContent; @@ -856,7 +853,7 @@ shared_ptr MainDbPrivate::selectConferenceSubjectEvent ( long long MainDbPrivate::insertEvent (const shared_ptr &eventLog) { soci::session *session = dbSession.getBackendSession(); - const int &type = static_cast(eventLog->getType()); + const int &type = int(eventLog->getType()); const tm &creationTime = Utils::getTimeTAsTm(eventLog->getCreationTime()); *session << "INSERT INTO event (type, creation_time) VALUES (:type, :creationTime)", soci::use(type), @@ -921,8 +918,8 @@ long long MainDbPrivate::insertConferenceChatMessageEvent (const shared_ptrgetFromAddress().asString()); const long long &toSipAddressId = insertSipAddress(chatMessage->getToAddress().asString()); const tm &messageTime = Utils::getTimeTAsTm(chatMessage->getTime()); - const int &state = static_cast(chatMessage->getState()); - const int &direction = static_cast(chatMessage->getDirection()); + const int &state = int(chatMessage->getState()); + const int &direction = int(chatMessage->getDirection()); const string &imdnMessageId = chatMessage->getImdnMessageId(); const int &isSecured = chatMessage->isSecured() ? 1 : 0; @@ -955,7 +952,7 @@ void MainDbPrivate::updateConferenceChatMessageEvent (const shared_ptr const long long &eventId = dEventKey->storageId; soci::session *session = dbSession.getBackendSession(); - const int &state = static_cast(chatMessage->getState()); + const int &state = int(chatMessage->getState()); const string &imdnMessageId = chatMessage->getImdnMessageId(); *session << "UPDATE conference_chat_message_event SET state = :state, imdn_message_id = :imdnMessageId" " WHERE event_id = :eventId", @@ -1206,7 +1203,7 @@ static T getValueFromRow (const soci::row &row, int index, bool &isNull) { isNull = false; try { - return row.get(static_cast(index)); + return row.get(size_t(index)); } catch (const exception &) { isNull = true; } @@ -1319,9 +1316,9 @@ void MainDbPrivate::importLegacyHistory (DbSession &inDbSession) { } const int &state = message.get( - LegacyMessageColState, static_cast(ChatMessage::State::Displayed) + LegacyMessageColState, int(ChatMessage::State::Displayed) ); - if (state < 0 || state > static_cast(ChatMessage::State::Displayed)) { + if (state < 0 || state > int(ChatMessage::State::Displayed)) { lWarning() << "Unable to import legacy message with invalid state."; continue; } @@ -1368,7 +1365,7 @@ void MainDbPrivate::importLegacyHistory (DbSession &inDbSession) { } soci::session *session = dbSession.getBackendSession(); - const int &eventType = static_cast(EventLog::Type::ConferenceChatMessage); + const int &eventType = int(EventLog::Type::ConferenceChatMessage); *session << "INSERT INTO event (type, creation_time) VALUES (:type, :creationTime)", soci::use(eventType), soci::use(creationTime); @@ -1398,7 +1395,7 @@ void MainDbPrivate::importLegacyHistory (DbSession &inDbSession) { insertContent(eventId, content); insertChatRoomParticipant(chatRoomId, remoteSipAddressId, false); - if (state != static_cast(ChatMessage::State::Displayed)) + if (state != int(ChatMessage::State::Displayed)) insertChatMessageParticipant(eventId, remoteSipAddressId, state); } @@ -1697,32 +1694,6 @@ void MainDb::init () { " ON DELETE CASCADE" ") " + charset; - // Trigger to delete participant_message cache entries. - // TODO: Fix me in the future. (Problem on Mysql backend.) - #if 0 - string displayedId = Utils::toString(static_cast(ChatMessage::State::Displayed)); - string participantMessageDeleter = - "CREATE TRIGGER IF NOT EXISTS chat_message_participant_deleter" - " AFTER UPDATE OF state ON chat_message_participant FOR EACH ROW" - " WHEN NEW.state = "; - participantMessageDeleter += displayedId; - participantMessageDeleter += " AND (SELECT COUNT(*) FROM (" - " SELECT state FROM chat_message_participant WHERE" - " NEW.event_id = chat_message_participant.event_id" - " AND state <> "; - participantMessageDeleter += displayedId; - participantMessageDeleter += " LIMIT 1" - " )) = 0" - " BEGIN" - " DELETE FROM chat_message_participant WHERE NEW.event_id = chat_message_participant.event_id;" - " UPDATE conference_chat_message_event SET state = "; - participantMessageDeleter += displayedId; - participantMessageDeleter += " WHERE event_id = NEW.event_id;" - " END"; - - *session << participantMessageDeleter; - #endif - *session << "CREATE TABLE IF NOT EXISTS friends_list (" " id" + primaryKeyStr("INT UNSIGNED") + "," @@ -1775,6 +1746,24 @@ void MainDb::init () { " version INT UNSIGNED NOT NULL" ") " + charset; + if (getBackend() == Backend::Mysql) + *session << + "CREATE TRIGGER IF NOT EXISTS chat_message_participant_deleter" + " AFTER UPDATE ON conference_chat_message_event FOR EACH ROW" + " BEGIN" + " IF NEW.state = " + Utils::toString(int(ChatMessage::State::Displayed)) + " THEN" + " DELETE FROM chat_message_participant WHERE event_id = NEW.event_id;" + " END IF;" + " END "; + else + *session << + "CREATE TRIGGER IF NOT EXISTS chat_message_participant_deleter" + " AFTER UPDATE OF state ON conference_chat_message_event FOR EACH ROW" + " WHEN NEW.state = " + Utils::toString(int(ChatMessage::State::Displayed)) + + " BEGIN" + " DELETE FROM chat_message_participant WHERE event_id = NEW.event_id;" + " END "; + d->updateSchema(); d->updateModuleVersion("events", ModuleVersionEvents); @@ -1969,7 +1958,7 @@ shared_ptr MainDb::getEventFromKey (const MainDbKey &dbKey) { return d->selectGenericConferenceEvent( storageId, - static_cast(type), + EventLog::Type(type), Utils::getTmAsTimeT(creationTime), ChatRoomId(IdentityAddress(peerSipAddress), IdentityAddress(localSipAddress)) ); @@ -2009,7 +1998,7 @@ list> MainDb::getConferenceNotifiedEvents ( events.push_back(eventLog ? eventLog : d->selectGenericConferenceEvent( eventId, - static_cast(row.get(1)), + EventLog::Type(row.get(1)), Utils::getTmAsTimeT(row.get(2)), chatRoomId )); @@ -2056,8 +2045,8 @@ int MainDb::getUnreadChatMessageCount (const ChatRoomId &chatRoomId) const { " SELECT event_id FROM conference_event WHERE chat_room_id = :chatRoomId" ") AND"; - query += " direction = " + Utils::toString(static_cast(ChatMessage::Direction::Incoming)) + - + " AND state <> " + Utils::toString(static_cast(ChatMessage::State::Displayed)); + query += " direction = " + Utils::toString(int(ChatMessage::Direction::Incoming)) + + + " AND state <> " + Utils::toString(int(ChatMessage::State::Displayed)); DurationLogger durationLogger( "Get unread chat messages count of: (peer=" + chatRoomId.getPeerAddress().asString() + @@ -2088,13 +2077,13 @@ void MainDb::markChatMessagesAsRead (const ChatRoomId &chatRoomId) const { return; string query = "UPDATE conference_chat_message_event" - " SET state = " + Utils::toString(static_cast(ChatMessage::State::Displayed)) + " "; + " SET state = " + Utils::toString(int(ChatMessage::State::Displayed)) + " "; query += "WHERE"; if (chatRoomId.isValid()) query += " event_id IN (" " SELECT event_id FROM conference_event WHERE chat_room_id = :chatRoomId" ") AND"; - query += " direction = " + Utils::toString(static_cast(ChatMessage::Direction::Incoming)); + query += " direction = " + Utils::toString(int(ChatMessage::Direction::Incoming)); DurationLogger durationLogger( "Mark chat messages as read of: (peer=" + chatRoomId.getPeerAddress().asString() + @@ -2125,8 +2114,8 @@ list> MainDb::getUnreadChatMessages (const ChatRoomId &c if (chatRoomId.isValid()) query += " chat_room_id = :chatRoomId AND "; query += " conference_event.event_id = conference_chat_message_event.event_id" - " AND direction = " + Utils::toString(static_cast(ChatMessage::Direction::Incoming)) + - " AND state <> " + Utils::toString(static_cast(ChatMessage::State::Displayed)) + + " AND direction = " + Utils::toString(int(ChatMessage::Direction::Incoming)) + + " AND state <> " + Utils::toString(int(ChatMessage::State::Displayed)) + ")"; DurationLogger durationLogger( @@ -2210,7 +2199,7 @@ list> MainDb::findChatMessages ( if (!event) event = d->selectGenericConferenceEvent( eventId, - static_cast(row.get(1)), + EventLog::Type(row.get(1)), Utils::getTmAsTimeT(row.get(2)), chatRoomId ); @@ -2285,7 +2274,7 @@ list> MainDb::getHistoryRange ( if (!event) event = d->selectGenericConferenceEvent( eventId, - static_cast(row.get(1)), + EventLog::Type(row.get(1)), Utils::getTmAsTimeT(row.get(2)), chatRoomId ); @@ -2419,11 +2408,10 @@ list> MainDb::getChatRooms () const { soci::rowset rows = (session->prepare << query, soci::use(participantId)); for (const auto &row : rows) { shared_ptr device = dParticipant->addDevice(IdentityAddress(row.get(0))); - ParticipantDevice::State state = static_cast(getBackend() == Backend::Mysql + device->setState(ParticipantDevice::State(getBackend() == Backend::Mysql ? row.get(1, 0) : static_cast(row.get(1, 0)) - ); - device->setState(state); + )); } } @@ -2690,9 +2678,9 @@ void MainDb::enableChatRoomMigration (const ChatRoomId &chatRoomId, bool enable) *session << "SELECT capabilities FROM chat_room WHERE id = :chatRoomId", soci::use(dbChatRoomId), soci::into(capabilities); if (enable) - capabilities |= static_cast(ChatRoom::Capabilities::Migratable); + capabilities |= int(ChatRoom::Capabilities::Migratable); else - capabilities &= ~static_cast(ChatRoom::Capabilities::Migratable); + capabilities &= ~int(ChatRoom::Capabilities::Migratable); *session << "UPDATE chat_room SET capabilities = :capabilities WHERE id = :chatRoomId", soci::use(capabilities), soci::use(dbChatRoomId); diff --git a/tester/db/linphone.db b/tester/db/linphone.db index 8e7f0f8a8666184fb0398fa8d75a34c79536b207..ae7fe37abfda8ce2a6e080f37dbc361e3f3df2f2 100644 GIT binary patch delta 572 zcmYMuO-NKx6bJBo=6&7wym#J=rQ`I*yw0TQ%g?dzEYp07SgnfI#Y_-{g^IZJgEDZV ztGCLDaNi>GE;Dc=;VL2oH+}3Y+XN-~w-zmaoO3w*;Xkv!n3#d3ARRJhb}+`?GM4y) znO$z{u|n4VsdcvWszgs40#C6dtumuxD)W}PW7Sv{=PS>n^pb47cIJ7`3gm41d;GjbXbV3(&LlJu5B%FfNa0br8IXDj&pcgK} PCFq08(66}|`1x)hr*n&l delta 724 zcmZwB%}*0S7zXf}ZoAu$-F}GB)>3&X6x#|dr6MX6EU;>o7K6ne8dt1c1x>uPx@ttaz$^X zxL1GWP(sN}evu;A)FpMHKnu&O`9dTXr3)*|If@Wg_hzGZqoy5}JDGkeUQ$=o_KUn) zjCaN^Rmzm6&h_NK3SP9oI1HGXrKxt$QZ|<@P?9JbMawwRym~R4*S|TGG1vd{dj953 z6Rlod%oJ48JxLyPV!%Q-JWW$|E-T;!7r4O#N1z9eLND}z7kuD{V-SFT2*Pm~0D?gX z!4M2X7)Ia(L?8+=I0>UL260HhI3!^LQg8|;;WV^oJp*Uq98ALuq+u53;5=juF3%r) F{sR+-yIKGM