LCOV - code coverage report
Current view: top level - foo/src/client - conversation_interface.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 98 130 75.4 %
Date: 2025-12-18 10:07:43 Functions: 18 25 72.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             : 
      18             : #ifdef HAVE_CONFIG_H
      19             : #include "config.h"
      20             : #endif
      21             : 
      22             : #include "conversation_interface.h"
      23             : 
      24             : #include <cerrno>
      25             : #include <sstream>
      26             : #include <cstring>
      27             : 
      28             : #include "logger.h"
      29             : #include "manager.h"
      30             : #include "jamidht/jamiaccount.h"
      31             : #include "jamidht/conversation_module.h"
      32             : 
      33             : namespace libjami {
      34             : 
      35             : std::string
      36         117 : startConversation(const std::string& accountId)
      37             : {
      38         117 :     if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
      39         117 :         if (auto convModule = acc->convModule(true))
      40         117 :             return convModule->startConversation();
      41           0 :     return {};
      42             : }
      43             : 
      44             : void
      45         136 : acceptConversationRequest(const std::string& accountId, const std::string& conversationId)
      46             : {
      47         136 :     if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
      48         136 :         if (auto convModule = acc->convModule(true))
      49         136 :             convModule->acceptConversationRequest(conversationId);
      50         136 : }
      51             : 
      52             : void
      53           3 : declineConversationRequest(const std::string& accountId, const std::string& conversationId)
      54             : {
      55           3 :     if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
      56           3 :         acc->declineConversationRequest(conversationId);
      57           3 : }
      58             : 
      59             : bool
      60           9 : removeConversation(const std::string& accountId, const std::string& conversationId)
      61             : {
      62           9 :     if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
      63           9 :         if (auto convModule = acc->convModule(true))
      64           9 :             return convModule->removeConversation(conversationId);
      65           0 :     return false;
      66             : }
      67             : 
      68             : std::vector<std::string>
      69          12 : getConversations(const std::string& accountId)
      70             : {
      71          12 :     if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
      72          12 :         if (auto convModule = acc->convModule(true))
      73          12 :             return convModule->getConversations();
      74           0 :     return {};
      75             : }
      76             : 
      77             : std::vector<std::map<std::string, std::string>>
      78          15 : getActiveCalls(const std::string& accountId, const std::string& conversationId)
      79             : {
      80          15 :     if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
      81          15 :         if (auto convModule = acc->convModule(true))
      82          15 :             return convModule->getActiveCalls(conversationId);
      83           0 :     return {};
      84             : }
      85             : 
      86             : std::vector<std::map<std::string, std::string>>
      87          12 : getConversationRequests(const std::string& accountId)
      88             : {
      89          12 :     if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
      90          12 :         if (auto convModule = acc->convModule(true))
      91          12 :             return convModule->getConversationRequests();
      92           0 :     return {};
      93             : }
      94             : 
      95             : void
      96           3 : updateConversationInfos(const std::string& accountId,
      97             :                         const std::string& conversationId,
      98             :                         const std::map<std::string, std::string>& infos)
      99             : {
     100           3 :     if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
     101           3 :         if (auto convModule = acc->convModule(true))
     102           3 :             convModule->updateConversationInfos(conversationId, infos);
     103           3 : }
     104             : 
     105             : std::map<std::string, std::string>
     106           5 : conversationInfos(const std::string& accountId, const std::string& conversationId)
     107             : {
     108           5 :     if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
     109           5 :         if (auto convModule = acc->convModule(true))
     110           5 :             return convModule->conversationInfos(conversationId);
     111           0 :     return {};
     112             : }
     113             : 
     114             : void
     115           5 : setConversationPreferences(const std::string& accountId,
     116             :                            const std::string& conversationId,
     117             :                            const std::map<std::string, std::string>& prefs)
     118             : {
     119           5 :     if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
     120           5 :         if (auto convModule = acc->convModule(true))
     121           5 :             convModule->setConversationPreferences(conversationId, prefs);
     122           5 : }
     123             : 
     124             : std::map<std::string, std::string>
     125           4 : getConversationPreferences(const std::string& accountId, const std::string& conversationId)
     126             : {
     127           4 :     if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
     128           4 :         if (auto convModule = acc->convModule(true))
     129           4 :             return convModule->getConversationPreferences(conversationId);
     130           0 :     return {};
     131             : }
     132             : 
     133             : // Member management
     134             : void
     135         134 : addConversationMember(const std::string& accountId, const std::string& conversationId, const std::string& contactUri)
     136             : {
     137         134 :     if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
     138         134 :         if (auto convModule = acc->convModule(true)) {
     139         134 :             dht::InfoHash h(contactUri);
     140         134 :             if (not h) {
     141           0 :                 JAMI_ERROR("addConversationMember: invalid contact URI `{}`", contactUri);
     142           0 :                 return;
     143             :             }
     144         134 :             convModule->addConversationMember(conversationId, h);
     145         134 :         }
     146             : }
     147             : 
     148             : void
     149          14 : removeConversationMember(const std::string& accountId, const std::string& conversationId, const std::string& contactUri)
     150             : {
     151          14 :     if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
     152          14 :         if (auto convModule = acc->convModule(true)) {
     153          14 :             dht::InfoHash h(contactUri);
     154          14 :             if (not h) {
     155           0 :                 JAMI_ERROR("removeConversationMember: invalid contact URI `{}`", contactUri);
     156           0 :                 return;
     157             :             }
     158          14 :             convModule->removeConversationMember(conversationId, h);
     159          14 :         }
     160             : }
     161             : 
     162             : std::vector<std::map<std::string, std::string>>
     163          23 : getConversationMembers(const std::string& accountId, const std::string& conversationId)
     164             : {
     165          23 :     if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
     166          23 :         if (auto convModule = acc->convModule(true))
     167          23 :             return convModule->getConversationMembers(conversationId, true);
     168           0 :     return {};
     169             : }
     170             : 
     171             : // Message send/load
     172             : void
     173          96 : sendMessage(const std::string& accountId,
     174             :             const std::string& conversationId,
     175             :             const std::string& message,
     176             :             const std::string& commitId,
     177             :             const int32_t& flag)
     178             : {
     179          96 :     if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
     180          96 :         if (auto convModule = acc->convModule(true)) {
     181          96 :             if (flag == 0 /* Reply or simple commit */) {
     182          86 :                 convModule->sendMessage(conversationId, message, commitId);
     183          10 :             } else if (flag == 1 /* message edition */) {
     184           7 :                 convModule->editMessage(conversationId, message, commitId);
     185           3 :             } else if (flag == 2 /* reaction */) {
     186           3 :                 convModule->reactToMessage(conversationId, message, commitId);
     187             :             }
     188          96 :         }
     189          96 : }
     190             : 
     191             : uint32_t
     192           2 : loadConversation(const std::string& accountId,
     193             :                  const std::string& conversationId,
     194             :                  const std::string& fromMessage,
     195             :                  size_t n)
     196             : {
     197           2 :     if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
     198           2 :         if (auto convModule = acc->convModule(true))
     199           2 :             return convModule->loadConversation(conversationId, fromMessage, n);
     200           0 :     return 0;
     201             : }
     202             : 
     203             : uint32_t
     204           0 : loadSwarmUntil(const std::string& accountId,
     205             :                const std::string& conversationId,
     206             :                const std::string& fromMessage,
     207             :                const std::string& toMessage)
     208             : {
     209           0 :     if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
     210           0 :         if (auto convModule = acc->convModule(true))
     211           0 :             return convModule->loadSwarmUntil(conversationId, fromMessage, toMessage);
     212           0 :     return 0;
     213             : }
     214             : 
     215             : uint32_t
     216           4 : countInteractions(const std::string& accountId,
     217             :                   const std::string& conversationId,
     218             :                   const std::string& toId,
     219             :                   const std::string& fromId,
     220             :                   const std::string& authorUri)
     221             : {
     222           4 :     if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
     223           4 :         if (auto convModule = acc->convModule(true))
     224           4 :             return convModule->countInteractions(conversationId, toId, fromId, authorUri);
     225           0 :     return 0;
     226             : }
     227             : 
     228             : void
     229           0 : clearCache(const std::string& accountId, const std::string& conversationId)
     230             : {
     231           0 :     if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
     232           0 :         if (auto convModule = acc->convModule(true))
     233           0 :             convModule->clearCache(conversationId);
     234           0 : }
     235             : 
     236             : uint32_t
     237           4 : searchConversation(const std::string& accountId,
     238             :                    const std::string& conversationId,
     239             :                    const std::string& author,
     240             :                    const std::string& lastId,
     241             :                    const std::string& regexSearch,
     242             :                    const std::string& type,
     243             :                    const int64_t& after,
     244             :                    const int64_t& before,
     245             :                    const uint32_t& maxResult,
     246             :                    const int32_t& flag)
     247             : {
     248           4 :     uint32_t res = 0;
     249           4 :     jami::Filter filter {author, lastId, regexSearch, type, after, before, maxResult, flag != 0};
     250          16 :     for (const auto& accId : jami::Manager::instance().getAccountList()) {
     251          12 :         if (!accountId.empty() && accId != accountId)
     252           8 :             continue;
     253           4 :         if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accId)) {
     254           4 :             res = std::uniform_int_distribution<uint32_t>()(acc->rand);
     255           4 :             if (auto convModule = acc->convModule(true)) {
     256           4 :                 convModule->search(res, conversationId, filter);
     257             :             }
     258           4 :         }
     259           4 :     }
     260           4 :     return res;
     261           4 : }
     262             : 
     263             : void
     264           0 : reloadConversationsAndRequests(const std::string& accountId)
     265             : {
     266           0 :     if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId)) {
     267           0 :         acc->reloadContacts();
     268           0 :         if (auto convModule = acc->convModule(true)) {
     269           0 :             convModule->reloadRequests();
     270           0 :             convModule->loadConversations();
     271             :         }
     272           0 :     }
     273           0 : }
     274             : 
     275             : } // namespace libjami

Generated by: LCOV version 1.14