LCOV - code coverage report
Current view: top level - foo/src/jamidht - conversation_module.h (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 2 2 100.0 %
Date: 2025-08-24 09:11:10 Functions: 3 3 100.0 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2025 Savoir-faire Linux Inc.
       3             :  *
       4             :  *  This program is free software: you can redistribute it and/or modify
       5             :  *  it under the terms of the GNU General Public License as published by
       6             :  *  the Free Software Foundation, either version 3 of the License, or
       7             :  *  (at your option) any later version.
       8             :  *
       9             :  *  This program is distributed in the hope that it will be useful,
      10             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      12             :  *  GNU General Public License for more details.
      13             :  *
      14             :  *  You should have received a copy of the GNU General Public License
      15             :  *  along with this program. If not, see <https://www.gnu.org/licenses/>.
      16             :  */
      17             : #pragma once
      18             : 
      19             : #include "scheduled_executor.h"
      20             : #include "jamidht/account_manager.h"
      21             : #include "jamidht/conversation.h"
      22             : #include "jamidht/conversationrepository.h"
      23             : #include "jamidht/jami_contact.h"
      24             : 
      25             : #include <mutex>
      26             : #include <msgpack.hpp>
      27             : 
      28             : namespace jami {
      29             : static constexpr const char MIME_TYPE_INVITE[] {"application/invite"};
      30             : static constexpr const char MIME_TYPE_GIT[] {"application/im-gitmessage-id"};
      31             : 
      32             : class SIPCall;
      33             : 
      34             : struct SyncMsg
      35             : {
      36             :     DeviceSync ds;
      37             :     std::map<std::string, ConvInfo> c;
      38             :     std::map<std::string, ConversationRequest> cr;
      39             :     // p is conversation's preferences. It's not stored in c, as
      40             :     // we can update the preferences without touching any confInfo.
      41             :     std::map<std::string, std::map<std::string, std::string>> p;
      42             :     // Last displayed messages [[deprecated]]
      43             :     std::map<std::string, std::map<std::string, std::string>> ld;
      44             :     // Read & fetched status
      45             :     /*
      46             :      * {{ convId,
      47             :      *      { memberUri,, {
      48             :      *          {"fetch", "commitId"},
      49             :      *          {"fetched_ts", "timestamp"},
      50             :      *          {"read", "commitId"},
      51             :      *          {"read_ts", "timestamp"}
      52             :      *       }
      53             :      * }}
      54             :      */
      55             :     std::map<std::string, std::map<std::string, std::map<std::string, std::string>>> ms;
      56             : 
      57         635 :     MSGPACK_DEFINE(ds, c, cr, p, ld, ms)
      58             : };
      59             : 
      60             : using ChannelCb = std::function<bool(const std::shared_ptr<dhtnet::ChannelSocket>&)>;
      61             : using NeedSocketCb
      62             :     = std::function<void(const std::string&, const std::string&, ChannelCb&&, const std::string&)>;
      63             : using SengMsgCb = std::function<
      64             :     uint64_t(const std::string&, const DeviceId&, std::map<std::string, std::string>, uint64_t)>;
      65             : using NeedsSyncingCb = std::function<void(std::shared_ptr<SyncMsg>&&)>;
      66             : using OneToOneRecvCb = std::function<void(const std::string&, const std::string&)>;
      67             : 
      68             : class ConversationModule
      69             : {
      70             : public:
      71             :     ConversationModule(std::shared_ptr<JamiAccount> account,
      72             :                        std::shared_ptr<AccountManager> accountManager,
      73             :                        NeedsSyncingCb&& needsSyncingCb,
      74             :                        SengMsgCb&& sendMsgCb,
      75             :                        NeedSocketCb&& onNeedSocket,
      76             :                        NeedSocketCb&& onNeedSwarmSocket,
      77             :                        OneToOneRecvCb&& oneToOneRecvCb,
      78             :                        bool autoLoadConversations = true);
      79         568 :     ~ConversationModule() = default;
      80             : 
      81             :     void setAccountManager(std::shared_ptr<AccountManager> accountManager);
      82             : 
      83             :     /**
      84             :      * Refresh information about conversations
      85             :      */
      86             :     void loadConversations();
      87             : 
      88             :     void loadSingleConversation(const std::string& convId);
      89             : 
      90             : #ifdef LIBJAMI_TEST
      91             :     void onBootstrapStatus(const std::function<void(std::string, Conversation::BootstrapStatus)>& cb);
      92             : #endif
      93             : 
      94             :     void monitor();
      95             : 
      96             :     /**
      97             :      * Bootstrap swarm managers to other peers
      98             :      */
      99             :     void bootstrap(const std::string& convId = "");
     100             : 
     101             :     /**
     102             :      * Clear not removed fetch
     103             :      */
     104             :     void clearPendingFetch();
     105             : 
     106             :     /**
     107             :      * Reload requests from file
     108             :      */
     109             :     void reloadRequests();
     110             : 
     111             :     /**
     112             :      * Return all conversation's id (including syncing ones)
     113             :      */
     114             :     std::vector<std::string> getConversations() const;
     115             : 
     116             :     /**
     117             :      * Get related conversation with member
     118             :      * @param uri       The member to search for
     119             :      * @return the conversation id if found else empty
     120             :      */
     121             :     std::string getOneToOneConversation(const std::string& uri) const noexcept;
     122             : 
     123             :     /**
     124             :      * Replace linked conversation in contact's details
     125             :      * @param uri           Of the contact
     126             :      * @param oldConv       Current conversation
     127             :      * @param newConv
     128             :      * @return if replaced
     129             :      */
     130             :     bool updateConvForContact(const std::string& uri,
     131             :                               const std::string& oldConv,
     132             :                               const std::string& newConv);
     133             : 
     134             :     /**
     135             :      * Return conversation's requests
     136             :      */
     137             :     std::vector<std::map<std::string, std::string>> getConversationRequests() const;
     138             : 
     139             :     /**
     140             :      * Called when detecting a new trust request with linked one to one
     141             :      * @param uri               Sender's URI
     142             :      * @param conversationId    Related conversation's id
     143             :      * @param payload           VCard
     144             :      * @param received          Received time
     145             :      */
     146             :     void onTrustRequest(const std::string& uri,
     147             :                         const std::string& conversationId,
     148             :                         const std::vector<uint8_t>& payload,
     149             :                         time_t received);
     150             : 
     151             :     /**
     152             :      * Called when receiving a new conversation's request
     153             :      * @param from      Sender
     154             :      * @param value     Conversation's request
     155             :      */
     156             :     void onConversationRequest(const std::string& from, const Json::Value& value);
     157             : 
     158             :     /**
     159             :      * Retrieve author of a conversation request
     160             :      * @param convId    Conversation's id
     161             :      * @return the author of the conversation request
     162             :      */
     163             :     std::string peerFromConversationRequest(const std::string& convId) const;
     164             : 
     165             :     /**
     166             :      * Called when a peer needs an invite for a conversation (generally after that they received
     167             :      * a commit notification for a conversation they don't have yet)
     168             :      * @param from
     169             :      * @param conversationId
     170             :      */
     171             :     void onNeedConversationRequest(const std::string& from, const std::string& conversationId);
     172             : 
     173             :     /**
     174             :      * Accept a conversation's request
     175             :      * @param convId
     176             :      * @param deviceId      If a trust request is accepted from a device (can help to sync)
     177             :      */
     178             :     void acceptConversationRequest(const std::string& conversationId,
     179             :                                    const std::string& deviceId = "");
     180             : 
     181             :     /**
     182             :      * Decline a conversation's request
     183             :      * @param convId
     184             :      */
     185             :     void declineConversationRequest(const std::string& conversationId);
     186             : 
     187             :     /**
     188             :      * Clone conversation from a member
     189             :      * @note used to clone an old conversation after deleting/re-adding a contact
     190             :      * @param conversationId
     191             :      * @param uri
     192             :      * @param oldConvId
     193             :      */
     194             :     void cloneConversationFrom(const std::string& conversationId,
     195             :                                const std::string& uri,
     196             :                                const std::string& oldConvId = "");
     197             : 
     198             :     /**
     199             :      * Starts a new conversation
     200             :      * @param mode          Wanted mode
     201             :      * @param otherMember   If needed (one to one)
     202             :      * @return conversation's id
     203             :      */
     204             :     std::string startConversation(ConversationMode mode = ConversationMode::INVITES_ONLY,
     205             :                                   const dht::InfoHash& otherMember = {});
     206             : 
     207             :     // Message send/load
     208             :     void sendMessage(const std::string& conversationId,
     209             :                      Json::Value&& value,
     210             :                      const std::string& replyTo = "",
     211             :                      bool announce = true,
     212             :                      OnCommitCb&& onCommit = {},
     213             :                      OnDoneCb&& cb = {});
     214             : 
     215             :     void sendMessage(const std::string& conversationId,
     216             :                      std::string message,
     217             :                      const std::string& replyTo = "",
     218             :                      const std::string& type = "text/plain",
     219             :                      bool announce = true,
     220             :                      OnCommitCb&& onCommit = {},
     221             :                      OnDoneCb&& cb = {});
     222             : 
     223             :     void editMessage(const std::string& conversationId,
     224             :                      const std::string& newBody,
     225             :                      const std::string& editedId);
     226             :     void reactToMessage(const std::string& conversationId,
     227             :                         const std::string& newBody,
     228             :                         const std::string& reactToId);
     229             : 
     230             :     /**
     231             :      * Add to the related conversation the call history message
     232             :      * @param uri           Peer number
     233             :      * @param duration_ms   The call duration in ms
     234             :      * @param reason
     235             :      */
     236             :     void addCallHistoryMessage(const std::string& uri, uint64_t duration_ms, const std::string& reason);
     237             : 
     238             :     // Received that a peer displayed a message
     239             :     bool onMessageDisplayed(const std::string& peer,
     240             :                             const std::string& conversationId,
     241             :                             const std::string& interactionId);
     242             :     std::map<std::string, std::map<std::string, std::map<std::string, std::string>>> convMessageStatus() const;
     243             : 
     244             :     /**
     245             :      * Load conversation's messages
     246             :      * @param conversationId    Conversation to load
     247             :      * @param fromMessage
     248             :      * @param n                 Max interactions to load
     249             :      * @return id of the operation
     250             :      */
     251             :     uint32_t loadConversationMessages(const std::string& conversationId,
     252             :                                       const std::string& fromMessage = "",
     253             :                                       size_t n = 0);
     254             :     uint32_t loadConversation(const std::string& conversationId,
     255             :                               const std::string& fromMessage = "",
     256             :                               size_t n = 0);
     257             :     uint32_t loadConversationUntil(const std::string& conversationId,
     258             :                                    const std::string& fromMessage,
     259             :                                    const std::string& to);
     260             :     uint32_t loadSwarmUntil(const std::string& conversationId,
     261             :                             const std::string& fromMessage,
     262             :                             const std::string& toMessage);
     263             :     /**
     264             :      * Clear loaded interactions
     265             :      * @param conversationId
     266             :      */
     267             :     void clearCache(const std::string& conversationId);
     268             : 
     269             :     // File transfer
     270             :     /**
     271             :      * Returns related transfer manager
     272             :      * @param id        Conversation's id
     273             :      * @return nullptr if not found, else the manager
     274             :      */
     275             :     std::shared_ptr<TransferManager> dataTransfer(const std::string& id) const;
     276             : 
     277             :     /**
     278             :      * Choose if we can accept channel request
     279             :      * @param member        Member to check
     280             :      * @param fileId        File transfer to check (needs to be waiting)
     281             :      * @param verifyShaSum  For debug only
     282             :      * @return if we accept the channel request
     283             :      */
     284             :     bool onFileChannelRequest(const std::string& conversationId,
     285             :                               const std::string& member,
     286             :                               const std::string& fileId,
     287             :                               bool verifyShaSum = true) const;
     288             : 
     289             :     /**
     290             :      * Ask conversation's members to send a file to this device
     291             :      * @param conversationId    Related conversation
     292             :      * @param interactionId     Related interaction
     293             :      * @param fileId            Related fileId
     294             :      * @param path              where to download the file
     295             :      */
     296             :     bool downloadFile(const std::string& conversationId,
     297             :                       const std::string& interactionId,
     298             :                       const std::string& fileId,
     299             :                       const std::string& path);
     300             : 
     301             :     // Sync
     302             :     /**
     303             :      * Sync conversations with detected peer
     304             :      */
     305             :     void syncConversations(const std::string& peer, const std::string& deviceId);
     306             : 
     307             :     /**
     308             :      * Detect new conversations and request from other devices
     309             :      * @param msg       Received data
     310             :      * @param peerId    Sender
     311             :      * @param deviceId
     312             :      */
     313             :     void onSyncData(const SyncMsg& msg, const std::string& peerId, const std::string& deviceId);
     314             : 
     315             :     /**
     316             :      * Check if we need to share infos with a contact
     317             :      * @param memberUri
     318             :      * @param deviceId
     319             :      */
     320             :     bool needsSyncingWith(const std::string& memberUri, const std::string& deviceId) const;
     321             : 
     322             :     /**
     323             :      * Notify that a peer fetched a commit
     324             :      * @note: this definitely remove the repository when needed (when we left and someone fetched
     325             :      * the information)
     326             :      * @param conversationId    Related conv
     327             :      * @param deviceId          Device who synced
     328             :      * @param commit            HEAD synced
     329             :      */
     330             :     void setFetched(const std::string& conversationId,
     331             :                     const std::string& deviceId,
     332             :                     const std::string& commit);
     333             : 
     334             :     /**
     335             :      * Launch fetch on new commit
     336             :      * @param peer              Who sent the notification
     337             :      * @param deviceId          Who sent the notification
     338             :      * @param conversationId    Related conversation
     339             :      * @param commitId          Commit to retrieve
     340             :      */
     341             :     void fetchNewCommits(const std::string& peer,
     342             :                          const std::string& deviceId,
     343             :                          const std::string& conversationId,
     344             :                          const std::string& commitId);
     345             : 
     346             :     // Conversation's member
     347             :     /**
     348             :      * Adds a new member to a conversation (this will triggers a member event + new message on success)
     349             :      * @param conversationId
     350             :      * @param contactUri
     351             :      * @param sendRequest   If we need to inform the peer (used for tests)
     352             :      */
     353             :     void addConversationMember(const std::string& conversationId,
     354             :                                const dht::InfoHash& contactUri,
     355             :                                bool sendRequest = true);
     356             :     /**
     357             :      * Remove a member from a conversation (this will trigger a member event + new message on success)
     358             :      * @param conversationId
     359             :      * @param contactUri
     360             :      * @param isDevice
     361             :      */
     362             :     void removeConversationMember(const std::string& conversationId,
     363             :                                   const dht::InfoHash& contactUri,
     364             :                                   bool isDevice = false);
     365             :     /**
     366             :      * Get members
     367             :      * @param conversationId
     368             :      * @param includeBanned
     369             :      * @return a map of members with their role and details
     370             :      */
     371             :     std::vector<std::map<std::string, std::string>> getConversationMembers(
     372             :         const std::string& conversationId, bool includeBanned = false) const;
     373             :     /**
     374             :      * Retrieve the number of interactions from interactionId to HEAD
     375             :      * @param convId
     376             :      * @param interactionId     "" for getting the whole history
     377             :      * @param authorUri         Stop when detect author
     378             :      * @return number of interactions since interactionId
     379             :      */
     380             :     uint32_t countInteractions(const std::string& convId,
     381             :                                const std::string& toId,
     382             :                                const std::string& fromId,
     383             :                                const std::string& authorUri) const;
     384             : 
     385             :     /**
     386             :      * Search in conversations via a filter
     387             :      * @param req       Id of the request
     388             :      * @param convId    Leave empty to search in all conversation, else add the conversation's id
     389             :      * @param filter    Parameters for the search
     390             :      * @note triggers messagesFound
     391             :      */
     392             :     void search(uint32_t req, const std::string& convId, const Filter& filter) const;
     393             : 
     394             :     // Conversation's infos management
     395             :     /**
     396             :      * Update metadatas from conversations (like title, avatar, etc)
     397             :      * @param conversationId
     398             :      * @param infos
     399             :      * @param sync              If we need to sync with others (used for tests)
     400             :      */
     401             :     void updateConversationInfos(const std::string& conversationId,
     402             :                                  const std::map<std::string, std::string>& infos,
     403             :                                  bool sync = true);
     404             :     std::map<std::string, std::string> conversationInfos(const std::string& conversationId) const;
     405             :     /**
     406             :      * Update user's preferences (like color, notifications, etc) to be synced across devices
     407             :      * @param conversationId
     408             :      * @param preferences
     409             :      */
     410             :     void setConversationPreferences(const std::string& conversationId,
     411             :                                     const std::map<std::string, std::string>& prefs);
     412             :     std::map<std::string, std::string> getConversationPreferences(const std::string& conversationId,
     413             :                                                                   bool includeCreated = false) const;
     414             :     /**
     415             :      * Retrieve all conversation preferences to sync with other devices
     416             :      */
     417             :     std::map<std::string, std::map<std::string, std::string>> convPreferences() const;
     418             :     // Get the map into a VCard format for storing
     419             :     std::vector<uint8_t> conversationVCard(const std::string& conversationId) const;
     420             : 
     421             :     /**
     422             :      * Return if a device or member is banned from a conversation
     423             :      * @param convId
     424             :      * @param uri
     425             :      */
     426             :     bool isBanned(const std::string& convId, const std::string& uri) const;
     427             : 
     428             :     // Remove swarm
     429             :     /**
     430             :      * Remove one to one conversations related to a contact
     431             :      * @param uri       Of the contact
     432             :      * @param ban       If banned
     433             :      */
     434             :     void removeContact(const std::string& uri, bool ban);
     435             : 
     436             :     /**
     437             :      * Remove a conversation, but not the contact
     438             :      * @param conversationId
     439             :      * @return if successfully removed
     440             :      */
     441             :     bool removeConversation(const std::string& conversationId);
     442             :     void initReplay(const std::string& oldConvId, const std::string& newConvId);
     443             :     /**
     444             :      * Check if we're hosting a specific conference
     445             :      * @param conversationId (empty to search all conv)
     446             :      * @param confId
     447             :      * @return true if hosting this conference
     448             :      */
     449             :     bool isHosting(const std::string& conversationId, const std::string& confId) const;
     450             :     /**
     451             :      * Return active calls
     452             :      * @param convId        Which conversation to choose
     453             :      * @return {{"id":id}, {"uri":uri}, {"device":device}}
     454             :      */
     455             :     std::vector<std::map<std::string, std::string>> getActiveCalls(
     456             :         const std::string& conversationId) const;
     457             :     /**
     458             :      * Call the conversation
     459             :      * @param url       Url to call (swarm:conversation or swarm:conv/account/device/conf to join)
     460             :      * @param mediaList The media list
     461             :      * @param cb        Callback to pass which device to call (called in the same thread)
     462             :      * @return call if a call is started, else nullptr
     463             :      */
     464             :     std::shared_ptr<SIPCall> call(const std::string& url,
     465             :                                 const std::vector<libjami::MediaMap>& mediaList,
     466             :                                 std::function<void(const std::string&, const DeviceId&, const std::shared_ptr<SIPCall>&)>&& cb);
     467             :     void hostConference(const std::string& conversationId,
     468             :                         const std::string& confId,
     469             :                         const std::string& callId,
     470             :                         const std::vector<libjami::MediaMap>& mediaList = {});
     471             : 
     472             :     // The following methods modify what is stored on the disk
     473             :     static void saveConvInfos(const std::string& accountId,
     474             :                               const std::map<std::string, ConvInfo>& conversations);
     475             :     static void saveConvInfosToPath(const std::filesystem::path& path,
     476             :                                     const std::map<std::string, ConvInfo>& conversations);
     477             :     static void saveConvRequests(
     478             :         const std::string& accountId,
     479             :         const std::map<std::string, ConversationRequest>& conversationsRequests);
     480             :     static void saveConvRequestsToPath(
     481             :         const std::filesystem::path& path,
     482             :         const std::map<std::string, ConversationRequest>& conversationsRequests);
     483             : 
     484             :     static std::map<std::string, ConvInfo> convInfos(const std::string& accountId);
     485             :     static std::map<std::string, ConvInfo> convInfosFromPath(const std::filesystem::path& path);
     486             :     static std::map<std::string, ConversationRequest> convRequests(const std::string& accountId);
     487             :     static std::map<std::string, ConversationRequest> convRequestsFromPath(
     488             :         const std::filesystem::path& path);
     489             :     void addConvInfo(const ConvInfo& info);
     490             : 
     491             :     /**
     492             :      * Get a conversation
     493             :      * @param convId
     494             :      */
     495             :     std::shared_ptr<Conversation> getConversation(const std::string& convId);
     496             :     /**
     497             :      * Return current git socket used for a conversation
     498             :      * @param deviceId          Related device
     499             :      * @param conversationId    Related conversation
     500             :      * @return the related socket
     501             :      */
     502             :     std::shared_ptr<dhtnet::ChannelSocket> gitSocket(std::string_view deviceId,
     503             :                                                      std::string_view convId) const;
     504             :     void removeGitSocket(std::string_view deviceId, std::string_view convId);
     505             :     void addGitSocket(std::string_view deviceId,
     506             :                       std::string_view convId,
     507             :                       const std::shared_ptr<dhtnet::ChannelSocket>& channel);
     508             :     /**
     509             :      * Clear all connection (swarm channels)
     510             :      */
     511             :     void shutdownConnections();
     512             :     /**
     513             :      * Add a swarm connection
     514             :      * @param conversationId
     515             :      * @param socket
     516             :      */
     517             :     void addSwarmChannel(const std::string& conversationId,
     518             :                          std::shared_ptr<dhtnet::ChannelSocket> socket);
     519             :     /**
     520             :      * Triggers a bucket maintainance for DRTs
     521             :      */
     522             :     void connectivityChanged();
     523             : 
     524             :     /**
     525             :      * Get Typers object for a conversation
     526             :      * @param convId
     527             :      * @return the Typer object
     528             :      */
     529             :     std::shared_ptr<Typers> getTypers(const std::string& convId);
     530             : 
     531             : private:
     532             :     class Impl;
     533             :     std::shared_ptr<Impl> pimpl_;
     534             : };
     535             : 
     536             : } // namespace jami

Generated by: LCOV version 1.14