diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2020-04-16 21:42:45 +0200 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2020-04-16 21:42:45 +0200 |
commit | 6cf5244bc514042fe419fffe1cd26a7f5e3c778f (patch) | |
tree | 4a72abf80980c3d10656bd82606a69329867d057 /src/message.cpp | |
parent | a76a9ad65fa549d2c1650bb5a7d9a657186edc43 (diff) |
Remove optional from load result
Makes loading of data well-defined for arbitrary object contents.
Introduce zero reference and object to represent missing or mismatched
parts.
Diffstat (limited to 'src/message.cpp')
-rw-r--r-- | src/message.cpp | 39 |
1 files changed, 17 insertions, 22 deletions
diff --git a/src/message.cpp b/src/message.cpp index b7d4bda..354703e 100644 --- a/src/message.cpp +++ b/src/message.cpp @@ -48,11 +48,11 @@ DirectMessage::DirectMessage(Priv * p): p(p) {} -optional<DirectMessageData> DirectMessageData::load(const Ref & ref) +DirectMessageData DirectMessageData::load(const Ref & ref) { auto rec = ref->asRecord(); if (!rec) - return nullopt; + return DirectMessageData(); vector<Stored<DirectMessageData>> prev; for (auto p : rec->items("PREV")) @@ -60,16 +60,10 @@ optional<DirectMessageData> DirectMessageData::load(const Ref & ref) prev.push_back(*x); auto fref = rec->item("from").asRef(); - if (!fref) - return nullopt; - - auto from = Identity::load(*fref); - if (!from) - return nullopt; return DirectMessageData { .prev = std::move(prev), - .from = *from, + .from = fref ? Identity::load(*fref) : nullopt, .time = *rec->item("time").asDate(), .text = rec->item("text").asText().value(), }; @@ -81,27 +75,32 @@ Ref DirectMessageData::store(const Storage & st) const for (const auto prev : prev) items.emplace_back("PREV", prev.ref()); - items.emplace_back("from", from.ref().value()); - items.emplace_back("time", time); - items.emplace_back("text", text); + if (from) + items.emplace_back("from", from->ref().value()); + if (time) + items.emplace_back("time", *time); + if (text) + items.emplace_back("text", *text); return st.storeObject(Record(std::move(items))); } -const Identity & DirectMessage::from() const +const optional<Identity> & DirectMessage::from() const { return p->data->from; } -const ZonedTime & DirectMessage::time() const +const optional<ZonedTime> & DirectMessage::time() const { return p->data->time; } -const string & DirectMessage::text() const +string DirectMessage::text() const { - return p->data->text; + if (p->data->text) + return p->data->text.value(); + return ""; } @@ -138,7 +137,7 @@ DirectMessageThread::Iterator & DirectMessageThread::Iterator::operator++() auto ncur = p->next[0]; for (const auto & m : p->next) - if (m->time.time >= ncur->time.time) + if (!ncur->time || (m->time && m->time->time >= ncur->time->time)) ncur = m; p->current.emplace(DirectMessage(new DirectMessage::Priv { @@ -216,10 +215,6 @@ UUID DirectMessageService::uuid() const void DirectMessageService::handle(Context & ctx) const { - auto dm = Stored<DirectMessageData>::load(ctx.ref()); - if (!dm) - return; - auto pid = ctx.peer().identity(); if (!pid) return; @@ -228,7 +223,7 @@ void DirectMessageService::handle(Context & ctx) const unique_lock lock(threadLock); vector<Stored<DirectMessageData>> head(DirectMessageThread::Priv::getThreadLocked(powner).p->head); - head.push_back(*dm); + head.push_back(Stored<DirectMessageData>::load(ctx.ref())); filterAncestors(head); auto dmt = DirectMessageThread::Priv::updateThreadLocked(powner, std::move(head)); |