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