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 : #include "jamidht/ydoc_channel_handler.h"
18 :
19 : #include "jamidht/collaborative_editing.h"
20 :
21 : namespace jami {
22 :
23 : /// The document id a "ydoc://<device>/<documentId>" channel name carries.
24 : static std::string_view
25 23 : documentIdOf(std::string_view name)
26 : {
27 23 : auto sep = name.find_last_of('/');
28 23 : return sep != std::string_view::npos ? name.substr(sep + 1) : std::string_view {};
29 : }
30 :
31 721 : YdocChannelHandler::YdocChannelHandler(const std::shared_ptr<JamiAccount>& acc, dhtnet::ConnectionManager& cm)
32 : : ChannelHandlerInterface()
33 721 : , account_(acc)
34 721 : , connectionManager_(cm)
35 721 : {}
36 :
37 1442 : YdocChannelHandler::~YdocChannelHandler() {}
38 :
39 : void
40 20 : YdocChannelHandler::connect(const DeviceId& deviceId,
41 : const std::string& name,
42 : ConnectCb&& cb,
43 : const std::string& /*connectionType*/,
44 : bool /*forceNewConnection*/)
45 : {
46 20 : auto channelName = "ydoc://" + deviceId.toString() + "/" + name;
47 20 : if (connectionManager_.isConnecting(deviceId, channelName)) {
48 0 : JAMI_LOG("Already connecting to {} for document {}", deviceId, name);
49 0 : return;
50 : }
51 20 : connectionManager_.connectDevice(deviceId, channelName, std::move(cb));
52 40 : }
53 :
54 : bool
55 17 : YdocChannelHandler::onRequest(const std::shared_ptr<dht::crypto::Certificate>& cert, const std::string& name)
56 : {
57 17 : auto acc = account_.lock();
58 17 : if (!cert || !cert->issuer || !acc)
59 0 : return false;
60 17 : const auto documentId = documentIdOf(name);
61 17 : if (documentId.empty())
62 0 : return false;
63 68 : return acc->collaborativeEditing()->acceptsRealtimeChannel(std::string(documentId),
64 34 : cert->issuer->getId().toString(),
65 51 : cert->getLongId().toString());
66 17 : }
67 :
68 : void
69 6 : YdocChannelHandler::onReady(const std::shared_ptr<dht::crypto::Certificate>& cert,
70 : const std::string& name,
71 : std::shared_ptr<dhtnet::ChannelSocket> channel)
72 : {
73 6 : auto acc = account_.lock();
74 6 : if (!cert || !cert->issuer || !acc || !channel)
75 0 : return;
76 6 : const auto documentId = documentIdOf(name);
77 6 : if (documentId.empty()) {
78 0 : channel->shutdown();
79 0 : return;
80 : }
81 24 : acc->collaborativeEditing()->onRealtimeChannel(std::string(documentId),
82 12 : cert->issuer->getId().toString(),
83 12 : cert->getLongId().toString(),
84 6 : std::move(channel));
85 6 : }
86 :
87 : } // namespace jami
|