From f117ff958c17777f3b8fe8e5c15b6098d1bb1bb2 Mon Sep 17 00:00:00 2001 From: Ghislain MARY Date: Thu, 16 Nov 2017 14:08:36 +0100 Subject: [PATCH] Fix chat related code that was broken with the introduction of ChatRoomId. --- coreapi/callbacks.c | 14 ++++++------- coreapi/chat.c | 18 +++++++++++------ coreapi/linphonecore.c | 27 +++++++++++++------------- coreapi/tester_utils.cpp | 13 +++++++------ coreapi/tester_utils.h | 2 +- src/chat/chat-message/chat-message-p.h | 5 +++++ src/chat/chat-message/chat-message.cpp | 25 ++++++++++-------------- src/chat/chat-message/chat-message.h | 10 ++-------- src/chat/chat-room/chat-room.cpp | 6 +++--- src/core/core-chat-room.cpp | 4 +++- 10 files changed, 62 insertions(+), 62 deletions(-) diff --git a/coreapi/callbacks.c b/coreapi/callbacks.c index ea68b4db5..8d497bd97 100644 --- a/coreapi/callbacks.c +++ b/coreapi/callbacks.c @@ -129,7 +129,7 @@ static void call_received(SalCallOp *h) { linphone_address_unref(toAddr); linphone_address_unref(fromAddr); shared_ptr chatRoom = lc->cppCore->findChatRoom( - ChatRoomId(SimpleAddress(h->get_to()), SimpleAddress(h->get_from())) + ChatRoomId(SimpleAddress(h->get_to()), SimpleAddress(h->get_to())) ); if (chatRoom) { L_GET_PRIVATE(static_pointer_cast(chatRoom))->confirmJoining(h); @@ -753,10 +753,10 @@ static void refer_received(SalOp *op, const SalAddress *refer_to){ if (linphone_core_conference_server_enabled(lc)) { // Removal of a participant at the server side shared_ptr chatRoom = lc->cppCore->findChatRoom( - ChatRoomId(SimpleAddress(op->get_to()), SimpleAddress(op->get_from())) + ChatRoomId(SimpleAddress(op->get_to()), SimpleAddress(op->get_to())) ); if (chatRoom) { - std::shared_ptr participant = chatRoom->findParticipant(chatRoom->getLocalAddress()); + std::shared_ptr participant = chatRoom->findParticipant(SimpleAddress(op->get_from())); if (!participant || !participant->isAdmin()) { static_cast(op)->reply(SalReasonDeclined); return; @@ -770,7 +770,7 @@ static void refer_received(SalOp *op, const SalAddress *refer_to){ } else { // The server asks a participant to leave a chat room LinphoneChatRoom *cr = L_GET_C_BACK_PTR( - lc->cppCore->findChatRoom(ChatRoomId(addr, SimpleAddress(op->get_from()))) + lc->cppCore->findChatRoom(ChatRoomId(addr, SimpleAddress(op->get_to()))) ); if (cr) { L_GET_CPP_PTR_FROM_C_OBJECT(cr)->leave(); @@ -781,7 +781,7 @@ static void refer_received(SalOp *op, const SalAddress *refer_to){ } } else if (addr.hasParam("admin")) { LinphoneChatRoom *cr = L_GET_C_BACK_PTR(lc->cppCore->findChatRoom( - ChatRoomId(SimpleAddress(op->get_to()), SimpleAddress(op->get_from())) + ChatRoomId(SimpleAddress(op->get_to()), SimpleAddress(op->get_to())) )); if (cr) { Address fromAddr(op->get_from()); @@ -799,9 +799,7 @@ static void refer_received(SalOp *op, const SalAddress *refer_to){ return; } } else { - LinphoneChatRoom *cr = L_GET_C_BACK_PTR(lc->cppCore->findChatRoom( - ChatRoomId(addr, SimpleAddress(op->get_from())) - )); + LinphoneChatRoom *cr = L_GET_C_BACK_PTR(lc->cppCore->findChatRoom(ChatRoomId(addr, SimpleAddress(op->get_to())))); if (!cr) cr = _linphone_client_group_chat_room_new(lc, addr.asString().c_str(), nullptr); L_GET_CPP_PTR_FROM_C_OBJECT(cr)->join(); diff --git a/coreapi/chat.c b/coreapi/chat.c index 8ad8a7232..8a0900a67 100644 --- a/coreapi/chat.c +++ b/coreapi/chat.c @@ -111,15 +111,21 @@ LinphoneChatRoom *linphone_core_get_chat_room_from_uri(LinphoneCore *lc, const c int linphone_core_message_received(LinphoneCore *lc, LinphonePrivate::SalOp *op, const SalMessage *sal_msg) { LinphoneReason reason = LinphoneReasonNotAcceptable; - const char *peerAddress = linphone_core_conference_server_enabled(lc) ? op->get_to() : op->get_from(); + const char *peerAddress; + const char *localAddress; + if (linphone_core_conference_server_enabled(lc)) { + localAddress = peerAddress = op->get_to(); + } else { + peerAddress = op->get_from(); + localAddress = op->get_to(); + } - // TODO: Use local address. - list> chatRooms = lc->cppCore->findChatRooms( - LinphonePrivate::SimpleAddress(peerAddress) + shared_ptr chatRoom = lc->cppCore->findChatRoom( + LinphonePrivate::ChatRoomId(LinphonePrivate::SimpleAddress(peerAddress), LinphonePrivate::SimpleAddress(localAddress)) ); - if (!chatRooms.empty()) - reason = L_GET_PRIVATE(chatRooms.front())->messageReceived(op, sal_msg); + if (chatRoom) + reason = L_GET_PRIVATE(chatRoom)->messageReceived(op, sal_msg); else { LinphoneAddress *addr = linphone_address_new(sal_msg->from); linphone_address_clean(addr); diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c index fb06d3135..8d280a322 100644 --- a/coreapi/linphonecore.c +++ b/coreapi/linphonecore.c @@ -2135,16 +2135,17 @@ static void linphone_core_internal_notify_received(LinphoneCore *lc, LinphoneEve } } else if (strcmp(notified_event, "conference") == 0) { const LinphoneAddress *resource = linphone_event_get_resource(lev); + const LinphoneAddress *from = linphone_event_get_from(lev); - // TODO: Ensure it is the good solution. - list> chatRooms = lc->cppCore->findChatRooms( - SimpleAddress(*L_GET_CPP_PTR_FROM_C_OBJECT(resource)) - ); + shared_ptr chatRoom = lc->cppCore->findChatRoom(LinphonePrivate::ChatRoomId( + SimpleAddress(*L_GET_CPP_PTR_FROM_C_OBJECT(resource)), + SimpleAddress(*L_GET_CPP_PTR_FROM_C_OBJECT(from)) + )); - if (!chatRooms.empty()) - L_GET_PRIVATE(static_pointer_cast(chatRooms.front()))->notifyReceived( + if (chatRoom) + L_GET_PRIVATE(static_pointer_cast(chatRoom))->notifyReceived( linphone_content_get_string_buffer(body) - ); + ); } } @@ -2154,15 +2155,13 @@ static void _linphone_core_conference_subscription_state_changed(LinphoneCore *l state == LinphoneSubscriptionIncomingReceived ) { const LinphoneAddress *resource = linphone_event_get_resource(lev); - - // TODO: Ensure it is the good solution. - list> chatRooms = lc->cppCore->findChatRooms( + shared_ptr chatRoom = lc->cppCore->findChatRoom(LinphonePrivate::ChatRoomId( + SimpleAddress(*L_GET_CPP_PTR_FROM_C_OBJECT(resource)), SimpleAddress(*L_GET_CPP_PTR_FROM_C_OBJECT(resource)) - ); - - if (!chatRooms.empty()) { + )); + if (chatRoom) { linphone_event_accept_subscription(lev); - L_GET_PRIVATE(static_pointer_cast(chatRooms.front()))->subscribeReceived(lev); + L_GET_PRIVATE(static_pointer_cast(chatRoom))->subscribeReceived(lev); } else linphone_event_deny_subscription(lev, LinphoneReasonDeclined); } diff --git a/coreapi/tester_utils.cpp b/coreapi/tester_utils.cpp index 15523fdcc..b166d2947 100644 --- a/coreapi/tester_utils.cpp +++ b/coreapi/tester_utils.cpp @@ -56,13 +56,14 @@ bctbx_list_t **linphone_core_get_call_logs_attribute(LinphoneCore *lc) { return &lc->call_logs; } -LinphoneChatRoom * linphone_core_find_chat_room (const LinphoneCore *lc, const LinphoneAddress *addr) { - list> chatRooms = lc->cppCore->findChatRooms( - SimpleAddress(*L_GET_CPP_PTR_FROM_C_OBJECT(addr)) - ); +LinphoneChatRoom * linphone_core_find_chat_room (const LinphoneCore *lc, const LinphoneAddress *peerAddr, const LinphoneAddress *localAddr) { + shared_ptr chatRoom = lc->cppCore->findChatRoom(ChatRoomId( + SimpleAddress(*L_GET_CPP_PTR_FROM_C_OBJECT(peerAddr)), + SimpleAddress(*L_GET_CPP_PTR_FROM_C_OBJECT(localAddr)) + )); - if (!chatRooms.empty()) - return L_GET_C_BACK_PTR(chatRooms.front()); + if (chatRoom) + return L_GET_C_BACK_PTR(chatRoom); return nullptr; } diff --git a/coreapi/tester_utils.h b/coreapi/tester_utils.h index 1d6f7f701..2dade201e 100644 --- a/coreapi/tester_utils.h +++ b/coreapi/tester_utils.h @@ -94,7 +94,7 @@ LINPHONE_PUBLIC LinphoneQualityReporting *linphone_call_log_get_quality_reportin LINPHONE_PUBLIC reporting_session_report_t **linphone_quality_reporting_get_reports(LinphoneQualityReporting *qreporting); LINPHONE_PUBLIC bctbx_list_t * linphone_chat_room_get_transient_messages(const LinphoneChatRoom *cr); -LINPHONE_PUBLIC LinphoneChatRoom * linphone_core_find_chat_room (const LinphoneCore *lc, const LinphoneAddress *addr); +LINPHONE_PUBLIC LinphoneChatRoom * linphone_core_find_chat_room (const LinphoneCore *lc, const LinphoneAddress *peerAddr, const LinphoneAddress *localAddr); LINPHONE_PUBLIC MSList* linphone_core_fetch_friends_from_db(LinphoneCore *lc, LinphoneFriendList *list); LINPHONE_PUBLIC MSList* linphone_core_fetch_friends_lists_from_db(LinphoneCore *lc); diff --git a/src/chat/chat-message/chat-message-p.h b/src/chat/chat-message/chat-message-p.h index d89accaa3..82303a0b4 100644 --- a/src/chat/chat-message/chat-message-p.h +++ b/src/chat/chat-message/chat-message-p.h @@ -67,6 +67,10 @@ public: this->fromAddress = fromAddress; } + inline void forceToAddress (const SimpleAddress &toAddress) { + this->toAddress = toAddress; + } + unsigned int getStorageId() const; void setStorageId(unsigned int id); @@ -149,6 +153,7 @@ private: std::weak_ptr chatRoom; ChatRoomId chatRoomId; SimpleAddress fromAddress; + SimpleAddress toAddress; ChatMessage::State state = ChatMessage::State::Idle; ChatMessage::Direction direction = ChatMessage::Direction::Incoming; diff --git a/src/chat/chat-message/chat-message.cpp b/src/chat/chat-message/chat-message.cpp index eb9d3fe49..181b9c53f 100644 --- a/src/chat/chat-message/chat-message.cpp +++ b/src/chat/chat-message/chat-message.cpp @@ -678,26 +678,21 @@ void ChatMessagePrivate::send () { // ----------------------------------------------------------------------------- -ChatMessage::ChatMessage (const shared_ptr &chatRoom) : +ChatMessage::ChatMessage (const shared_ptr &chatRoom, ChatMessage::Direction direction) : Object(*new ChatMessagePrivate), CoreAccessor(chatRoom->getCore()) { L_ASSERT(chatRoom); L_D(); d->chatRoom = chatRoom; d->chatRoomId = chatRoom->getChatRoomId(); - d->fromAddress = chatRoom->getLocalAddress(); - d->direction = Direction::Outgoing; -} - -ChatMessage::ChatMessage (const shared_ptr &chatRoom, const SimpleAddress &fromAddress) : - Object(*new ChatMessagePrivate), CoreAccessor(chatRoom->getCore()) { - L_ASSERT(chatRoom); - L_D(); - - d->chatRoom = chatRoom; - d->chatRoomId = chatRoom->getChatRoomId(); - d->fromAddress = fromAddress; - d->direction = Direction::Incoming; + if (direction == Direction::Outgoing) { + d->fromAddress = chatRoom->getLocalAddress(); + d->toAddress = chatRoom->getPeerAddress(); + } else { + d->fromAddress = chatRoom->getPeerAddress(); + d->toAddress = chatRoom->getLocalAddress(); + } + d->direction = direction; } ChatMessage::~ChatMessage () { @@ -779,7 +774,7 @@ const SimpleAddress &ChatMessage::getFromAddress () const { const SimpleAddress &ChatMessage::getToAddress () const { L_D(); - return d->direction == Direction::Outgoing ? d->chatRoomId.getPeerAddress() : d->chatRoomId.getLocalAddress(); + return d->toAddress; } const SimpleAddress &ChatMessage::getLocalAddress () const { diff --git a/src/chat/chat-message/chat-message.h b/src/chat/chat-message/chat-message.h index f2eb00bf3..8bca457ff 100644 --- a/src/chat/chat-message/chat-message.h +++ b/src/chat/chat-message/chat-message.h @@ -55,14 +55,6 @@ public: L_DECLARE_ENUM(State, L_ENUM_VALUES_CHAT_MESSAGE_STATE); L_DECLARE_ENUM(Direction, L_ENUM_VALUES_CHAT_MESSAGE_DIRECTION); - // TODO: Make me private. - - // Build an outgoing message. - ChatMessage (const std::shared_ptr &chatRoom); - - // Build and incoming message. - ChatMessage (const std::shared_ptr &chatRoom, const SimpleAddress &fromAddress); - ~ChatMessage (); // ----- TODO: Remove me. @@ -114,6 +106,8 @@ public: bool downloadFile (FileTransferContent &content); private: + ChatMessage (const std::shared_ptr &chatRoom, ChatMessage::Direction direction); + L_DECLARE_PRIVATE(ChatMessage); L_DISABLE_COPY(ChatMessage); }; diff --git a/src/chat/chat-room/chat-room.cpp b/src/chat/chat-room/chat-room.cpp index 319cce5e3..b4b29ead7 100644 --- a/src/chat/chat-room/chat-room.cpp +++ b/src/chat/chat-room/chat-room.cpp @@ -151,7 +151,7 @@ void ChatRoomPrivate::sendIsComposingNotification () { if (linphone_im_notif_policy_get_send_is_composing(policy)) { string payload = isComposingHandler->marshal(isComposing); if (!payload.empty()) { - shared_ptr msg = q->createMessage(); + shared_ptr msg = createChatMessage(ChatMessage::Direction::Outgoing); Content *content = new Content(); content->setContentType(ContentType::ImIsComposing); content->setBody(payload); @@ -164,8 +164,8 @@ void ChatRoomPrivate::sendIsComposingNotification () { // ----------------------------------------------------------------------------- shared_ptr ChatRoomPrivate::createChatMessage (ChatMessage::Direction direction) { - // TODO: Create me. - return nullptr; + L_Q(); + return shared_ptr(new ChatMessage(q->getSharedFromThis(), direction)); } // ----------------------------------------------------------------------------- diff --git a/src/core/core-chat-room.cpp b/src/core/core-chat-room.cpp index fc10049ff..186748997 100644 --- a/src/core/core-chat-room.cpp +++ b/src/core/core-chat-room.cpp @@ -50,7 +50,9 @@ static inline ChatRoomId resolveWorkaroundClientGroupChatRoomId ( SimpleAddress peerAddress = chatRoomId.getPeerAddress(); peerAddress.setDomain(Address(uri).getDomain()); - return ChatRoomId(peerAddress, chatRoomId.getLocalAddress()); + SimpleAddress localAddress = chatRoomId.getLocalAddress(); + localAddress.setDomain(Address(uri).getDomain()); + return ChatRoomId(peerAddress, localAddress); } // TODO: Remove me later.