From c2df0381313ffe5e155b7dc3d6d4c7271a4b73a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sun, 8 Jan 2023 22:31:08 +0100 Subject: Contact name setting and property lookup --- src/contact.cpp | 40 ++++++++++++++++++++++++++++++++++++++-- src/main.cpp | 27 ++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/contact.cpp b/src/contact.cpp index 796e896..787831c 100644 --- a/src/contact.cpp +++ b/src/contact.cpp @@ -2,8 +2,11 @@ #include "identity.h" +#include + using namespace erebos; +using std::array; using std::move; DEFINE_SHARED_TYPE(Set, @@ -34,6 +37,19 @@ optional Contact::customName() const return p->name; } +Contact Contact::customName(const Storage & st, const string & name) const +{ + auto cdata = st.store(ContactData { + .prev = p->data, + .identity = {}, + .name = name, + }); + + return Contact(shared_ptr(new Contact::Priv { + .data = { cdata }, + })); +} + string Contact::name() const { if (auto cust = customName()) @@ -59,13 +75,33 @@ vector> Contact::data() const return p->data; } +Digest Contact::leastRoot() const +{ + if (p->data.empty()) + return Digest(array {}); + + vector roots; + for (const auto & d : p->data) + for (const auto & r : d.ref().roots()) + roots.push_back(r); + roots.erase(std::unique(roots.begin(), roots.end()), roots.end()); + return roots[0]; +} + void Contact::Priv::init() { std::call_once(initFlag, [this]() { - // TODO: property lookup - identity = Identity::load(data[0]->identity); + vector>> idata; + for (const auto & c : findPropertyComponents(data, "identity")) + for (const auto & i : c->identity) + idata.push_back(i); + + identity = Identity::load(idata); if (identity) name = identity->name(); + + if (auto opt = findPropertyComponent(data, "name")) + name = (*opt)->name; }); } diff --git a/src/main.cpp b/src/main.cpp index 12f46ac..edf68d7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -465,7 +465,7 @@ void contactList(const vector &) }; for (const auto & c : h->behavior().lens().lens>().get().view(cmp)) { ostringstream ss; - ss << "contact-list-item " << c.name(); + ss << "contact-list-item " << string(c.leastRoot()) << " " << c.name(); if (auto id = c.identity()) if (auto iname = id->name()) ss << " " << *iname; @@ -474,6 +474,30 @@ void contactList(const vector &) printLine("contact-list-done"); } +void contactSetName(const vector & 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().lens>().get().view(cmp)) { + if (string(c.leastRoot()) == id) { + auto nh = h->update([&] (const Stored & loc) { + auto st = loc.ref().storage(); + auto cc = c.customName(st, name); + auto contacts = loc->shared>(); + return st.store(loc->shared>(contacts.add(st, cc))); + }); + if (nh) + *h = *nh; + break; + } + } + printLine("contact-set-name-done"); +} + vector commands = { { "store", store }, { "stored-generation", storedGeneration }, @@ -496,6 +520,7 @@ vector commands = { { "contact-accept", contactAccept }, { "contact-reject", contactReject }, { "contact-list", contactList }, + { "contact-set-name", contactSetName }, }; } -- cgit v1.2.3