From 6c58f1e095f7dbe1e7e1654c1807a76276a2f3f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sat, 5 Jun 2021 23:10:24 +0200 Subject: Contact list in shared state --- src/CMakeLists.txt | 1 + src/contact.cpp | 153 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/contact.h | 42 +++++++++++++++ src/identity.cpp | 12 +++++ src/identity.h | 3 +- 5 files changed, 209 insertions(+), 2 deletions(-) create mode 100644 src/contact.cpp create mode 100644 src/contact.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8f65555..0fd7125 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,6 +5,7 @@ include_directories( add_library(erebos attach channel + contact frp identity message diff --git a/src/contact.cpp b/src/contact.cpp new file mode 100644 index 0000000..edc33ea --- /dev/null +++ b/src/contact.cpp @@ -0,0 +1,153 @@ +#include "contact.h" + +#include "identity.h" + +using namespace erebos; + +using std::move; + +DEFINE_SHARED_TYPE(List, + "34fbb61e-6022-405f-b1b3-a5a1abecd25e", + &Contact::loadList, + [](const List & list) { + if (list.empty()) + return vector(); + return list.front().refs(); + }) + + +List Contact::prepend(const Storage & st, Identity id, List list) +{ + auto cd = st.store(ContactData { + .prev = list.empty() ? vector>() : list.front().p->data, + .identity = id.data(), + .name = nullopt, + }); + return list.push_front( + Contact(shared_ptr(new Priv { + .data = { cd }, + .identity = move(id), + })) + ); +} + +Identity Contact::identity() const +{ + return p->identity; +} + +optional Contact::name() const +{ + p->init(); + return p->name; +} + +bool Contact::operator==(const Contact & other) const +{ + return p->data == other.p->data; +} + +bool Contact::operator!=(const Contact & other) const +{ + return p->data != other.p->data; +} + +List Contact::loadList(const vector & refs) +{ + vector> cdata; + cdata.reserve(refs.size()); + + for (const auto & r : refs) + cdata.push_back(Stored::load(r)); + return Priv::loadList(move(cdata), {}); +} + +List Contact::Priv::loadList(vector> && cdata, vector && seen) +{ + if (cdata.empty()) + return {}; + + filterAncestors(cdata); + + for (size_t i = 0; i < cdata.size(); i++) { + auto id = Identity::load(cdata[i]->identity); + if (!id) + continue; + + bool skip = false; + for (const auto & sid : seen) { + if (id->sameAs(sid)) { + skip = true; + break; + } + } + if (skip) + continue; + + vector> next; + next.reserve(cdata.size() - i - 1 + cdata[i]->prev.size()); + for (size_t j = i + 1; j < cdata.size(); j++) + next.push_back(cdata[j]); + for (const auto & x : cdata[i]->prev) + next.push_back(x); + + seen.push_back(*id); + auto p = shared_ptr(new Priv { .data = move(cdata), .identity = move(*id) }); + return List(Contact(p), loadList(move(next), move(seen))); + } + + return {}; +} + +vector Contact::refs() const +{ + vector res; + res.reserve(p->data.size()); + for (const auto & x : p->data) + res.push_back(x.ref()); + return res; +} + +void Contact::Priv::init() +{ + std::call_once(initFlag, [this]() { + name = identity.name(); + }); +} + +ContactData ContactData::load(const Ref & ref) +{ + auto rec = ref->asRecord(); + if (!rec) + return ContactData(); + + vector> prev; + for (const auto & x : rec->items("PREV")) + if (const auto & p = x.as()) + prev.push_back(*p); + + vector>> identity; + for (const auto & x : rec->items("identity")) + if (const auto & i = x.asRef()) + identity.push_back(*i); + + return ContactData { + .prev = std::move(prev), + .identity = std::move(identity), + .name = rec->item("name").asText(), + }; +} + +Ref ContactData::store(const Storage & st) const +{ + vector items; + + for (const auto & prev : prev) + items.emplace_back("PREV", prev.ref()); + for (const auto & idt : identity) + items.emplace_back("identity", idt); + if (name) + items.emplace_back("name", *name); + + return st.storeObject(Record(std::move(items))); +} diff --git a/src/contact.h b/src/contact.h new file mode 100644 index 0000000..31deceb --- /dev/null +++ b/src/contact.h @@ -0,0 +1,42 @@ +#pragma once + +#include + +#include +#include +#include +#include + +namespace erebos { + +using std::optional; +using std::string; +using std::vector; + +struct ContactData; +struct IdentityData; + +struct Contact::Priv +{ + vector> data; + Identity identity; + + void init(); + std::once_flag initFlag {}; + + optional name {}; + + static List loadList(vector> &&, vector &&); +}; + +struct ContactData +{ + static ContactData load(const Ref &); + Ref store(const Storage &) const; + + vector> prev; + vector>> identity; + optional name; +}; + +} diff --git a/src/identity.cpp b/src/identity.cpp index f55f6dd..0d35122 100644 --- a/src/identity.cpp +++ b/src/identity.cpp @@ -6,6 +6,8 @@ #include #include +#include + using namespace erebos; using std::async; @@ -38,6 +40,11 @@ optional Identity::load(const vector & refs) for (const auto & ref : refs) data.push_back(Stored>::load(ref)); + return load(data); +} + +optional Identity::load(const vector>> & data) +{ if (auto ptr = Priv::validate(data)) return Identity(ptr); return nullopt; @@ -61,6 +68,11 @@ vector Identity::store(const Storage & st) const return res; } +const vector>> & Identity::data() const +{ + return p->data; +} + optional Identity::name() const { return p->name.get(); diff --git a/src/identity.h b/src/identity.h index 1dfc193..98db80a 100644 --- a/src/identity.h +++ b/src/identity.h @@ -10,9 +10,8 @@ using std::vector; namespace erebos { -class IdentityData +struct IdentityData { -public: static IdentityData load(const Ref &); Ref store(const Storage & st) const; -- cgit v1.2.3