diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2023-05-23 22:11:42 +0200 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2023-05-23 22:32:01 +0200 |
commit | 15ad6ae7bd64d8d7319d75dbbb0827addd22fef2 (patch) | |
tree | c5f7ac828f4c1d5f51c461ad332b83e570ce3b26 /src/main.cpp | |
parent | fce349f8f1c53d3e205a8d86b79784fdfd2b7b7a (diff) |
Message sending to identity or contact
Diffstat (limited to 'src/main.cpp')
-rw-r--r-- | src/main.cpp | 50 |
1 files changed, 34 insertions, 16 deletions
diff --git a/src/main.cpp b/src/main.cpp index f33bcc4..cc02b55 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -104,6 +104,22 @@ TestPeer & getPeer(const Peer & peer) throw invalid_argument("peer not found"); } +Contact getContact(const string & id) +{ + auto cmp = [](const Contact & x, const Contact & y) { + return x.data() < y.data(); + }; + for (const auto & c : h->behavior().lens<SharedState>().lens<Set<Contact>>().get().view(cmp)) { + if (string(c.leastRoot()) == id) { + return c; + } + } + + ostringstream ss; + ss << "contact '" << id << "' not found"; + throw invalid_argument(ss.str().c_str()); +} + struct Command { string name; @@ -505,22 +521,16 @@ void contactSetName(const vector<string> & args) auto id = args.at(0); auto name = args.at(1); - auto cmp = [](const Contact & x, const Contact & y) { - return x.data() < y.data(); - }; - for (const auto & c : h->behavior().lens<SharedState>().lens<Set<Contact>>().get().view(cmp)) { - if (string(c.leastRoot()) == id) { - auto nh = h->update([&] (const Stored<LocalState> & loc) { - auto st = loc.ref().storage(); - auto cc = c.customName(st, name); - auto contacts = loc->shared<Set<Contact>>(); - return st.store(loc->shared<Set<Contact>>(contacts.add(st, cc))); - }); - if (nh) - *h = *nh; - break; - } - } + auto c = getContact(id); + auto nh = h->update([&] (const Stored<LocalState> & loc) { + auto st = loc.ref().storage(); + auto cc = c.customName(st, name); + auto contacts = loc->shared<Set<Contact>>(); + return st.store(loc->shared<Set<Contact>>(contacts.add(st, cc))); + }); + if (nh) + *h = *nh; + printLine("contact-set-name-done"); } @@ -531,6 +541,13 @@ void dmSendPeer(const vector<string> & args) args.at(1)); } +void dmSendContact(const vector<string> & args) +{ + server->svc<DirectMessageService>().send( + getContact(args.at(0)), + args.at(1)); +} + vector<Command> commands = { { "store", store }, { "stored-generation", storedGeneration }, @@ -556,6 +573,7 @@ vector<Command> commands = { { "contact-list", contactList }, { "contact-set-name", contactSetName }, { "dm-send-peer", dmSendPeer }, + { "dm-send-contact", dmSendContact }, }; } |