From b5b6ef454533557a5df6667c26643d0be9d9140a Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Wed, 14 Feb 2018 11:29:59 +0100 Subject: [PATCH] Added new chat room callback to let app decides whether or not store chat message in database --- include/linphone/api/c-callbacks.h | 7 +++++++ include/linphone/api/c-chat-room-cbs.h | 13 +++++++++++++ src/c-wrapper/api/c-chat-room-cbs.cpp | 9 +++++++++ src/chat/chat-message/chat-message.cpp | 21 +++++++++++++-------- src/chat/chat-room/chat-room-p.h | 1 - src/chat/chat-room/chat-room.cpp | 17 ----------------- 6 files changed, 42 insertions(+), 26 deletions(-) diff --git a/include/linphone/api/c-callbacks.h b/include/linphone/api/c-callbacks.h index a053e78a8..ea533f2c8 100644 --- a/include/linphone/api/c-callbacks.h +++ b/include/linphone/api/c-callbacks.h @@ -249,6 +249,13 @@ typedef void (*LinphoneChatRoomCbsParticipantDeviceFetchedCb) (LinphoneChatRoom */ typedef void (*LinphoneChatRoomCbsParticipantsCapabilitiesCheckedCb) (LinphoneChatRoom *cr, const LinphoneAddress *deviceAddr, const bctbx_list_t *participantsAddr); +/** + * Callback used to tell the core whether or not to store the incoming message in db or not using linphone_chat_message_set_to_be_stored(). + * @param[in] cr #LinphoneChatRoom object + * @param[in] msg The #LinphoneChatMessage that is being received + */ +typedef void (*LinphoneChatRoomCbsShouldChatMessageBeStoredCb) (LinphoneChatRoom *cr, LinphoneChatMessage *msg); + /** * @} **/ diff --git a/include/linphone/api/c-chat-room-cbs.h b/include/linphone/api/c-chat-room-cbs.h index 9dc396fac..0e02b615c 100644 --- a/include/linphone/api/c-chat-room-cbs.h +++ b/include/linphone/api/c-chat-room-cbs.h @@ -271,6 +271,19 @@ LINPHONE_PUBLIC LinphoneChatRoomCbsParticipantsCapabilitiesCheckedCb linphone_ch */ LINPHONE_PUBLIC void linphone_chat_room_cbs_set_participants_capabilities_checked (LinphoneChatRoomCbs *cbs, LinphoneChatRoomCbsParticipantsCapabilitiesCheckedCb cb); +/** + * Get the message should be stored callback. + * @param[in] cbs LinphoneChatRoomCbs object + * @return The message should be stored getting callback + */ +LINPHONE_PUBLIC LinphoneChatRoomCbsShouldChatMessageBeStoredCb linphone_chat_room_cbs_get_chat_message_should_be_stored( LinphoneChatRoomCbs *cbs); +/** + * Set the message should be stored callback. + * @param[in] cbs LinphoneChatRoomCbs object + * @param[in] cb The message should be stored callback to be used + */ +LINPHONE_PUBLIC void linphone_chat_room_cbs_set_chat_message_should_be_stored( LinphoneChatRoomCbs *cbs, LinphoneChatRoomCbsShouldChatMessageBeStoredCb cb); + /** * @} */ diff --git a/src/c-wrapper/api/c-chat-room-cbs.cpp b/src/c-wrapper/api/c-chat-room-cbs.cpp index c8c8ca89d..cce0b165a 100644 --- a/src/c-wrapper/api/c-chat-room-cbs.cpp +++ b/src/c-wrapper/api/c-chat-room-cbs.cpp @@ -41,6 +41,7 @@ struct _LinphoneChatRoomCbs { LinphoneChatRoomCbsConferenceAddressGenerationCb conferenceAddressGenerationCb; LinphoneChatRoomCbsParticipantDeviceFetchedCb participantDeviceFetchedCb; LinphoneChatRoomCbsParticipantsCapabilitiesCheckedCb participantsCapabilitiesChecked; + LinphoneChatRoomCbsShouldChatMessageBeStoredCb shouldMessageBeStoredCb; }; BELLE_SIP_DECLARE_VPTR_NO_EXPORT(LinphoneChatRoomCbs); @@ -196,3 +197,11 @@ LinphoneChatRoomCbsParticipantsCapabilitiesCheckedCb linphone_chat_room_cbs_get_ void linphone_chat_room_cbs_set_participants_capabilities_checked (LinphoneChatRoomCbs *cbs, LinphoneChatRoomCbsParticipantsCapabilitiesCheckedCb cb) { cbs->participantsCapabilitiesChecked = cb; } + +LinphoneChatRoomCbsShouldChatMessageBeStoredCb linphone_chat_room_cbs_get_chat_message_should_be_stored( LinphoneChatRoomCbs *cbs) { + return cbs->shouldMessageBeStoredCb; +} + +void linphone_chat_room_cbs_set_chat_message_should_be_stored( LinphoneChatRoomCbs *cbs, LinphoneChatRoomCbsShouldChatMessageBeStoredCb cb) { + cbs->shouldMessageBeStoredCb = cb; +} diff --git a/src/chat/chat-message/chat-message.cpp b/src/chat/chat-message/chat-message.cpp index 7b5b569bd..40a80054e 100644 --- a/src/chat/chat-message/chat-message.cpp +++ b/src/chat/chat-message/chat-message.cpp @@ -401,18 +401,23 @@ void ChatMessagePrivate::notifyReceiving () { LinphoneChatRoom *chatRoom = L_GET_C_BACK_PTR(q->getChatRoom()); LinphoneChatRoomCbs *cbs = linphone_chat_room_get_callbacks(chatRoom); - LinphoneChatRoomCbsParticipantAddedCb cb = linphone_chat_room_cbs_get_chat_message_received(cbs); - shared_ptr event = make_shared( - ::time(nullptr), q->getSharedFromThis() - ); - if (cb) - cb(chatRoom, L_GET_C_BACK_PTR(event)); - // Legacy - q->getChatRoom()->getPrivate()->notifyChatMessageReceived(q->getSharedFromThis()); + + LinphoneChatRoomCbsShouldChatMessageBeStoredCb shouldMessageBeStoredCb = linphone_chat_room_cbs_get_chat_message_should_be_stored(cbs); + if (shouldMessageBeStoredCb) + shouldMessageBeStoredCb(chatRoom, L_GET_C_BACK_PTR(q->getSharedFromThis())); if (toBeStored) storeInDb(); + shared_ptr event = make_shared( + ::time(nullptr), q->getSharedFromThis() + ); + LinphoneChatRoomCbsChatMessageReceivedCb messageReceivedCb = linphone_chat_room_cbs_get_chat_message_received(cbs); + if (messageReceivedCb) + messageReceivedCb(chatRoom, L_GET_C_BACK_PTR(event)); + // Legacy + q->getChatRoom()->getPrivate()->notifyChatMessageReceived(q->getSharedFromThis()); + q->sendDeliveryNotification(LinphoneReasonNone); } diff --git a/src/chat/chat-room/chat-room-p.h b/src/chat/chat-room/chat-room-p.h index 5492f9bdc..7b45cc6c0 100644 --- a/src/chat/chat-room/chat-room-p.h +++ b/src/chat/chat-room/chat-room-p.h @@ -78,7 +78,6 @@ private: time_t creationTime = std::time(nullptr); time_t lastUpdateTime = std::time(nullptr); - std::shared_ptr pendingMessage; std::unique_ptr isComposingHandler; bool isComposing = false; diff --git a/src/chat/chat-room/chat-room.cpp b/src/chat/chat-room/chat-room.cpp index 257892be1..6a9fa970c 100644 --- a/src/chat/chat-room/chat-room.cpp +++ b/src/chat/chat-room/chat-room.cpp @@ -189,7 +189,6 @@ void ChatRoomPrivate::notifyUndecryptableChatMessageReceived (const shared_ptr msg; @@ -224,29 +223,18 @@ LinphoneReason ChatRoomPrivate::onSipMessageReceived (SalOp *op, const SalMessag if (msg->getPrivate()->getContentType() == ContentType::ImIsComposing) { onIsComposingReceived(msg->getFromAddress(), msg->getPrivate()->getText()); - increaseMsgCount = FALSE; if (lp_config_get_int(linphone_core_get_config(cCore), "sip", "deliver_imdn", 0) != 1) { goto end; } } else if (msg->getPrivate()->getContentType() == ContentType::Imdn) { onImdnReceived(msg->getPrivate()->getText()); - increaseMsgCount = FALSE; if (lp_config_get_int(linphone_core_get_config(cCore), "sip", "deliver_imdn", 0) != 1) { goto end; } } - if (increaseMsgCount) { - /* Mark the message as pending so that if ChatRoom::markAsRead() is called in the - * ChatRoomPrivate::chatMessageReceived() callback, it will effectively be marked as - * being read before being stored. */ - pendingMessage = msg; - } - onChatMessageReceived(msg); - pendingMessage = nullptr; - end: return reason; } @@ -438,11 +426,6 @@ void ChatRoom::markAsRead () { chatMessage->sendDisplayNotification(); dCore->mainDb->markChatMessagesAsRead(d->chatRoomId); - - if (d->pendingMessage) { - d->pendingMessage->updateState(ChatMessage::State::Displayed); - d->pendingMessage->sendDisplayNotification(); - } } LINPHONE_END_NAMESPACE