summaryrefslogtreecommitdiff
path: root/src/message.cpp
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2023-05-23 22:11:42 +0200
committerRoman Smrž <roman.smrz@seznam.cz>2023-05-23 22:32:01 +0200
commit15ad6ae7bd64d8d7319d75dbbb0827addd22fef2 (patch)
treec5f7ac828f4c1d5f51c461ad332b83e570ce3b26 /src/message.cpp
parentfce349f8f1c53d3e205a8d86b79784fdfd2b7b7a (diff)
Message sending to identity or contact
Diffstat (limited to 'src/message.cpp')
-rw-r--r--src/message.cpp30
1 files changed, 21 insertions, 9 deletions
diff --git a/src/message.cpp b/src/message.cpp
index dd4f9d5..ff8e05b 100644
--- a/src/message.cpp
+++ b/src/message.cpp
@@ -1,5 +1,6 @@
#include "message.h"
+#include <erebos/contact.h>
#include <erebos/network.h>
using namespace erebos;
@@ -246,26 +247,37 @@ DirectMessageThread DirectMessageService::thread(const Identity & peer)
return DirectMessageThread::Priv::getThreadLocked(peer.finalOwner());
}
-DirectMessage DirectMessageService::send(const Peer & peer, const string & text)
+DirectMessage DirectMessageService::send(const Identity & to, const string & text)
{
- auto pid = peer.identity();
- if (!pid)
- throw std::runtime_error("Peer without known identity");
- auto powner = pid->finalOwner();
-
scoped_lock lock(threadLock);
auto msg = server.localHead().ref().storage().store(DirectMessageData {
- .prev = DirectMessageThread::Priv::getThreadLocked(powner).p->head,
+ .prev = DirectMessageThread::Priv::getThreadLocked(to).p->head,
.from = server.identity().finalOwner(),
.time = ZonedTime::now(),
.text = text,
});
- DirectMessageThread::Priv::updateThreadLocked(powner, { msg });
- peer.send(myUUID, msg.ref());
+ DirectMessageThread::Priv::updateThreadLocked(to, { msg });
+
+ if (auto peer = server.peer(to))
+ peer->send(myUUID, msg.ref());
return DirectMessage(new DirectMessage::Priv {
.data = msg,
});
}
+
+DirectMessage DirectMessageService::send(const Contact & to, const string & text)
+{
+ if (auto id = to.identity())
+ return send(*id, text);
+ throw std::runtime_error("contact without erebos identity");
+}
+
+DirectMessage DirectMessageService::send(const Peer & to, const string & text)
+{
+ if (auto id = to.identity())
+ return send(id->finalOwner(), text);
+ throw std::runtime_error("peer without known identity");
+}