Skip to content
Snippets Groups Projects
Commit 67f74aae authored by Adrien Béraud's avatar Adrien Béraud
Browse files

multi-device: add text message support

Tuleap: #938
Change-Id: I92e6116702b5eb70d39ec1e0a6f6b876c98ce2cb
parent 4b3ca5c7
No related branches found
No related tags found
No related merge requests found
...@@ -1532,9 +1532,8 @@ RingAccount::doRegister_() ...@@ -1532,9 +1532,8 @@ RingAccount::doRegister_()
} }
// Listen for incoming calls // Listen for incoming calls
auto ringDeviceId = identity_.first->getPublicKey().getId().toString(); callKey_ = dht::InfoHash::get("callto:"+ringDeviceId_);
callKey_ = dht::InfoHash::get("callto:"+ringDeviceId); RING_DBG("Listening on callto:%s : %s", ringDeviceId_.c_str(), callKey_.toString().c_str());
RING_DBG("Listening on callto:%s : %s", ringDeviceId.c_str(), callKey_.toString().c_str());
dht_.listen<dht::IceCandidates>( dht_.listen<dht::IceCandidates>(
callKey_, callKey_,
[shared] (dht::IceCandidates&& msg) { [shared] (dht::IceCandidates&& msg) {
...@@ -1623,9 +1622,10 @@ RingAccount::doRegister_() ...@@ -1623,9 +1622,10 @@ RingAccount::doRegister_()
} }
); );
auto inboxDeviceKey = dht::InfoHash::get("inbox:"+ringDeviceId_);
dht_.listen<dht::ImMessage>( dht_.listen<dht::ImMessage>(
inboxKey, inboxDeviceKey,
[shared, inboxKey](dht::ImMessage&& v) { [shared, inboxDeviceKey](dht::ImMessage&& v) {
auto& this_ = *shared.get(); auto& this_ = *shared.get();
auto res = this_.treatedMessages_.insert(v.id); auto res = this_.treatedMessages_.insert(v.id);
if (!res.second) if (!res.second)
...@@ -1638,7 +1638,7 @@ RingAccount::doRegister_() ...@@ -1638,7 +1638,7 @@ RingAccount::doRegister_()
utf8_make_valid(v.msg)}}; utf8_make_valid(v.msg)}};
shared->onTextMessage(from, payloads); shared->onTextMessage(from, payloads);
RING_DBG("Sending message confirmation %" PRIu64, v.id); RING_DBG("Sending message confirmation %" PRIu64, v.id);
this_.dht_.putEncrypted(inboxKey, this_.dht_.putEncrypted(inboxDeviceKey,
v.from, v.from,
dht::ImMessage(v.id, std::string(), now)); dht::ImMessage(v.id, std::string(), now));
return true; return true;
...@@ -2123,20 +2123,40 @@ RingAccount::sendTextMessage(const std::string& to, const std::map<std::string, ...@@ -2123,20 +2123,40 @@ RingAccount::sendTextMessage(const std::string& to, const std::map<std::string,
messageEngine_.onMessageSent(token, false); messageEngine_.onMessageSent(token, false);
return; return;
} }
if (payloads.size() != 1) {
// Multi-part message
// TODO: not supported yet
RING_ERR("Multi-part im is not supported yet by RingAccount");
messageEngine_.onMessageSent(token, false);
return;
}
std::weak_ptr<RingAccount> wshared = std::static_pointer_cast<RingAccount>(shared_from_this());
auto toUri = parseRingUri(to); auto toUri = parseRingUri(to);
auto toH = dht::InfoHash(toUri); auto toH = dht::InfoHash(toUri);
auto now = system_clock::to_time_t(system_clock::now()); auto now = system_clock::to_time_t(system_clock::now());
auto treatedDevices_ = std::make_shared<std::set<dht::InfoHash>>();
struct PendingConfirmation {
bool replied {false};
std::map<dht::InfoHash, std::future<size_t>> listenTokens {};
};
auto confirm = std::make_shared<PendingConfirmation>();
// Find listening Ring devices for this account
dht_.get<DeviceAnnouncement>(toH, [=](DeviceAnnouncement&& dev) {
if (dev.from != toH)
return true;
if (not treatedDevices_->emplace(dev.dev).second)
return true;
// Single-part message
if (payloads.size() == 1) {
auto e = sentMessages_.emplace(token, PendingMessage {}); auto e = sentMessages_.emplace(token, PendingMessage {});
e.first->second.to = toH; e.first->second.to = dev.dev;
auto h = dht::InfoHash::get("inbox:"+toUri); auto h = dht::InfoHash::get("inbox:"+dev.dev.toString());
std::weak_ptr<RingAccount> wshared = std::static_pointer_cast<RingAccount>(shared_from_this()); RING_DBG("Found device to send message %s -> %s", dev.dev.toString().c_str(), h.toString().c_str());
dht_.listen<dht::ImMessage>(h, [wshared,token](dht::ImMessage&& msg) { auto list_token = dht_.listen<dht::ImMessage>(h, [h,wshared,token,confirm](dht::ImMessage&& msg) {
if (auto this_ = wshared.lock()) { if (auto this_ = wshared.lock()) {
// check expected message confirmation // check expected message confirmation
if (msg.id != token) if (msg.id != token)
...@@ -2165,27 +2185,29 @@ RingAccount::sendTextMessage(const std::string& to, const std::map<std::string, ...@@ -2165,27 +2185,29 @@ RingAccount::sendTextMessage(const std::string& to, const std::map<std::string,
this_->saveTreatedMessages(); this_->saveTreatedMessages();
// report message as confirmed received // report message as confirmed received
for (auto& t : confirm->listenTokens)
this_->dht_.cancelListen(t.first, t.second.get());
confirm->listenTokens.clear();
confirm->replied = true;
this_->messageEngine_.onMessageSent(token, true); this_->messageEngine_.onMessageSent(token, true);
} }
return false; return false;
}); });
confirm->listenTokens.emplace(h, std::move(list_token));
dht_.putEncrypted(h, dht_.putEncrypted(h,
toH, dev.dev,
dht::ImMessage(token, std::string(payloads.begin()->second), now), dht::ImMessage(token, std::string(payloads.begin()->second), now),
[wshared,token](bool ok) { [wshared,token,confirm,h](bool ok) {
if (auto this_ = wshared.lock()) { if (auto this_ = wshared.lock()) {
if (not ok) if (not ok) {
confirm->listenTokens.erase(h);
if (confirm->listenTokens.empty() and not confirm->replied)
this_->messageEngine_.onMessageSent(token, false); this_->messageEngine_.onMessageSent(token, false);
} }
});
return;
} }
});
// Multi-part message });
// TODO: not supported yet
RING_ERR("Multi-part im is not supported yet by RingAccount");
messageEngine_.onMessageSent(token, false);
} }
} // namespace ring } // namespace ring
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment