summaryrefslogtreecommitdiff
path: root/src/contact.cpp
blob: edc33eaf01e10b1887ae7caa09c6123391707e51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "contact.h"

#include "identity.h"

using namespace erebos;

using std::move;

DEFINE_SHARED_TYPE(List<Contact>,
		"34fbb61e-6022-405f-b1b3-a5a1abecd25e",
		&Contact::loadList,
		[](const List<Contact> & list) {
			if (list.empty())
				return vector<Ref>();
			return list.front().refs();
		})


List<Contact> Contact::prepend(const Storage & st, Identity id, List<Contact> list)
{
	auto cd = st.store(ContactData {
		.prev = list.empty() ? vector<Stored<ContactData>>() : list.front().p->data,
		.identity = id.data(),
		.name = nullopt,
	});
	return list.push_front(
			Contact(shared_ptr<Priv>(new Priv {
				.data = { cd },
				.identity = move(id),
			}))
	);
}

Identity Contact::identity() const
{
	return p->identity;
}

optional<string> 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> Contact::loadList(const vector<Ref> & refs)
{
	vector<Stored<ContactData>> cdata;
	cdata.reserve(refs.size());

	for (const auto & r : refs)
		cdata.push_back(Stored<ContactData>::load(r));
	return Priv::loadList(move(cdata), {});
}

List<Contact> Contact::Priv::loadList(vector<Stored<ContactData>> && cdata, vector<Identity> && 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<Stored<ContactData>> 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<Priv>(new Priv { .data = move(cdata), .identity = move(*id) });
		return List(Contact(p), loadList(move(next), move(seen)));
	}

	return {};
}

vector<Ref> Contact::refs() const
{
	vector<Ref> 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<Stored<ContactData>> prev;
	for (const auto & x : rec->items("PREV"))
		if (const auto & p = x.as<ContactData>())
			prev.push_back(*p);

	vector<Stored<Signed<IdentityData>>> 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<Record::Item> 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)));
}