Line data Source code
1 : /*
2 : * Copyright (C) 2013-2024 Savoir-faire Linux Inc.
3 : *
4 : * Author: Adrien BĂ©raud <adrien.beraud@savoirfairelinux.com>
5 : *
6 : * This program is free software; you can redistribute it and/or modify
7 : * it under the terms of the GNU General Public License as published by
8 : * the Free Software Foundation; either version 3 of the License, or
9 : * (at your option) any later version.
10 : *
11 : * This program is distributed in the hope that it will be useful,
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : * GNU General Public License for more details.
15 : *
16 : * You should have received a copy of the GNU General Public License
17 : * along with this program; if not, write to the Free Software
18 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 : */
20 :
21 : #ifdef HAVE_CONFIG_H
22 : #include "config.h"
23 : #endif
24 :
25 : #include "conversation_interface.h"
26 :
27 : #include <cerrno>
28 : #include <sstream>
29 : #include <cstring>
30 :
31 : #include "logger.h"
32 : #include "manager.h"
33 : #include "jamidht/jamiaccount.h"
34 : #include "jamidht/conversation_module.h"
35 :
36 : namespace libjami {
37 :
38 : std::string
39 113 : startConversation(const std::string& accountId)
40 : {
41 113 : if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
42 113 : if (auto convModule = acc->convModule(true))
43 113 : return convModule->startConversation();
44 0 : return {};
45 : }
46 :
47 : void
48 133 : acceptConversationRequest(const std::string& accountId, const std::string& conversationId)
49 : {
50 133 : if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
51 133 : if (auto convModule = acc->convModule(true))
52 133 : convModule->acceptConversationRequest(conversationId);
53 133 : }
54 :
55 : void
56 3 : declineConversationRequest(const std::string& accountId, const std::string& conversationId)
57 : {
58 3 : if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
59 3 : acc->declineConversationRequest(conversationId);
60 3 : }
61 :
62 : bool
63 9 : removeConversation(const std::string& accountId, const std::string& conversationId)
64 : {
65 9 : if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
66 9 : if (auto convModule = acc->convModule(true))
67 9 : return convModule->removeConversation(conversationId);
68 0 : return false;
69 : }
70 :
71 : std::vector<std::string>
72 12 : getConversations(const std::string& accountId)
73 : {
74 12 : if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
75 12 : if (auto convModule = acc->convModule(true))
76 12 : return convModule->getConversations();
77 0 : return {};
78 : }
79 :
80 : std::vector<std::map<std::string, std::string>>
81 16 : getActiveCalls(const std::string& accountId, const std::string& conversationId)
82 : {
83 16 : if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
84 16 : if (auto convModule = acc->convModule(true))
85 16 : return convModule->getActiveCalls(conversationId);
86 0 : return {};
87 : }
88 :
89 : std::vector<std::map<std::string, std::string>>
90 12 : getConversationRequests(const std::string& accountId)
91 : {
92 12 : if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
93 12 : if (auto convModule = acc->convModule(true))
94 12 : return convModule->getConversationRequests();
95 0 : return {};
96 : }
97 :
98 : void
99 3 : updateConversationInfos(const std::string& accountId,
100 : const std::string& conversationId,
101 : const std::map<std::string, std::string>& infos)
102 : {
103 3 : if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
104 3 : if (auto convModule = acc->convModule(true))
105 3 : convModule->updateConversationInfos(conversationId, infos);
106 3 : }
107 :
108 : std::map<std::string, std::string>
109 5 : conversationInfos(const std::string& accountId, const std::string& conversationId)
110 : {
111 5 : if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
112 5 : if (auto convModule = acc->convModule(true))
113 5 : return convModule->conversationInfos(conversationId);
114 0 : return {};
115 : }
116 :
117 : void
118 5 : setConversationPreferences(const std::string& accountId,
119 : const std::string& conversationId,
120 : const std::map<std::string, std::string>& prefs)
121 : {
122 5 : if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
123 5 : if (auto convModule = acc->convModule(true))
124 5 : convModule->setConversationPreferences(conversationId, prefs);
125 5 : }
126 :
127 : std::map<std::string, std::string>
128 4 : getConversationPreferences(const std::string& accountId, const std::string& conversationId)
129 : {
130 4 : if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
131 4 : if (auto convModule = acc->convModule(true))
132 4 : return convModule->getConversationPreferences(conversationId);
133 0 : return {};
134 : }
135 :
136 : // Member management
137 : void
138 131 : addConversationMember(const std::string& accountId,
139 : const std::string& conversationId,
140 : const std::string& contactUri)
141 : {
142 131 : if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
143 131 : if (auto convModule = acc->convModule(true))
144 131 : convModule->addConversationMember(conversationId, contactUri);
145 131 : }
146 :
147 : void
148 14 : removeConversationMember(const std::string& accountId,
149 : const std::string& conversationId,
150 : const std::string& contactUri)
151 : {
152 14 : if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
153 14 : if (auto convModule = acc->convModule(true))
154 14 : convModule->removeConversationMember(conversationId, contactUri);
155 14 : }
156 :
157 : std::vector<std::map<std::string, std::string>>
158 21 : getConversationMembers(const std::string& accountId, const std::string& conversationId)
159 : {
160 21 : if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
161 21 : if (auto convModule = acc->convModule(true))
162 21 : return convModule->getConversationMembers(conversationId, true);
163 0 : return {};
164 : }
165 :
166 : // Message send/load
167 : void
168 95 : sendMessage(const std::string& accountId,
169 : const std::string& conversationId,
170 : const std::string& message,
171 : const std::string& commitId,
172 : const int32_t& flag)
173 : {
174 95 : if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
175 95 : if (auto convModule = acc->convModule(true)) {
176 95 : if (flag == 0 /* Reply or simple commit */) {
177 86 : convModule->sendMessage(conversationId, message, commitId);
178 9 : } else if (flag == 1 /* message edition */) {
179 6 : convModule->editMessage(conversationId, message, commitId);
180 3 : } else if (flag == 2 /* reaction */) {
181 3 : convModule->reactToMessage(conversationId, message, commitId);
182 : }
183 95 : }
184 95 : }
185 :
186 : uint32_t
187 0 : loadConversationMessages(const std::string& accountId,
188 : const std::string& conversationId,
189 : const std::string& fromMessage,
190 : size_t n)
191 : {
192 0 : if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
193 0 : if (auto convModule = acc->convModule(true))
194 0 : return convModule->loadConversationMessages(conversationId, fromMessage, n);
195 0 : return 0;
196 : }
197 :
198 : uint32_t
199 2 : loadConversation(const std::string& accountId,
200 : const std::string& conversationId,
201 : const std::string& fromMessage,
202 : size_t n)
203 : {
204 2 : if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
205 2 : if (auto convModule = acc->convModule(true))
206 2 : return convModule->loadConversation(conversationId, fromMessage, n);
207 0 : return 0;
208 : }
209 :
210 : uint32_t
211 0 : loadConversationUntil(const std::string& accountId,
212 : const std::string& conversationId,
213 : const std::string& fromMessage,
214 : const std::string& toMessage)
215 : {
216 0 : if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
217 0 : if (auto convModule = acc->convModule(true))
218 0 : return convModule->loadConversationUntil(conversationId, fromMessage, toMessage);
219 0 : return 0;
220 : }
221 :
222 : uint32_t
223 0 : loadSwarmUntil(const std::string& accountId,
224 : const std::string& conversationId,
225 : const std::string& fromMessage,
226 : const std::string& toMessage)
227 : {
228 0 : if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
229 0 : if (auto convModule = acc->convModule(true))
230 0 : return convModule->loadSwarmUntil(conversationId, fromMessage, toMessage);
231 0 : return 0;
232 : }
233 :
234 : uint32_t
235 4 : countInteractions(const std::string& accountId,
236 : const std::string& conversationId,
237 : const std::string& toId,
238 : const std::string& fromId,
239 : const std::string& authorUri)
240 : {
241 4 : if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
242 4 : if (auto convModule = acc->convModule(true))
243 4 : return convModule->countInteractions(conversationId, toId, fromId, authorUri);
244 0 : return 0;
245 : }
246 :
247 : void
248 0 : clearCache(const std::string& accountId, const std::string& conversationId)
249 : {
250 0 : if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId))
251 0 : if (auto convModule = acc->convModule(true))
252 0 : convModule->clearCache(conversationId);
253 0 : }
254 :
255 : uint32_t
256 4 : searchConversation(const std::string& accountId,
257 : const std::string& conversationId,
258 : const std::string& author,
259 : const std::string& lastId,
260 : const std::string& regexSearch,
261 : const std::string& type,
262 : const int64_t& after,
263 : const int64_t& before,
264 : const uint32_t& maxResult,
265 : const int32_t& flag)
266 : {
267 4 : uint32_t res = 0;
268 4 : jami::Filter filter {author, lastId, regexSearch, type, after, before, maxResult, flag != 0};
269 16 : for (const auto& accId : jami::Manager::instance().getAccountList()) {
270 12 : if (!accountId.empty() && accId != accountId)
271 8 : continue;
272 4 : if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accId)) {
273 4 : res = std::uniform_int_distribution<uint32_t>()(acc->rand);
274 4 : if (auto convModule = acc->convModule(true)) {
275 4 : convModule->search(res, conversationId, filter);
276 : }
277 4 : }
278 4 : }
279 4 : return res;
280 4 : }
281 :
282 : void
283 0 : reloadConversationsAndRequests(const std::string& accountId)
284 : {
285 0 : if (auto acc = jami::Manager::instance().getAccount<jami::JamiAccount>(accountId)) {
286 0 : if (auto convModule = acc->convModule(true)) {
287 0 : convModule->reloadRequests();
288 0 : convModule->loadConversations();
289 : }
290 0 : }
291 0 : }
292 :
293 : } // namespace libjami
|