summaryrefslogtreecommitdiff
path: root/src/contact.cpp
blob: 787831c85460adab8df9009aadee659a69fb1c7d (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "contact.h"

#include "identity.h"

#include <array>

using namespace erebos;

using std::array;
using std::move;

DEFINE_SHARED_TYPE(Set<Contact>,
		"34fbb61e-6022-405f-b1b3-a5a1abecd25e",
		&Set<Contact>::load,
		[](const Set<Contact> & set) {
			return set.store();
		})

static const UUID serviceUUID("d9c37368-0da1-4280-93e9-d9bd9a198084");

Contact::Contact(vector<Stored<ContactData>> data):
	p(shared_ptr<Priv>(new Priv {
		.data = data,
	}))
{
}

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

optional<string> Contact::customName() const
{
	p->init();
	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<Contact::Priv>(new Contact::Priv {
		.data = { cdata },
	}));
}

string Contact::name() const
{
	if (auto cust = customName())
		return *cust;
	if (auto id = p->identity)
		if (auto idname = id->name())
			return *idname;
	return "";
}

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;
}

vector<Stored<ContactData>> Contact::data() const
{
	return p->data;
}

Digest Contact::leastRoot() const
{
	if (p->data.empty())
		return Digest(array<uint8_t, Digest::size> {});

	vector<Digest> 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]() {
		vector<Stored<Signed<IdentityData>>> idata;
		for (const auto & c : findPropertyComponents<Contact>(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<Contact>(data, "name"))
			name = (*opt)->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)));
}

ContactService::ContactService() = default;
ContactService::~ContactService() = default;

UUID ContactService::uuid() const
{
	return serviceUUID;
}

void ContactService::serverStarted(const Server & s)
{
	PairingService<ContactAccepted>::serverStarted(s);
	server = &s;
}

void ContactService::request(const Peer & peer)
{
	requestPairing(serviceUUID, peer);
}

Stored<ContactAccepted> ContactService::handlePairingComplete(const Peer & peer)
{
	server->localHead().update([&] (const Stored<LocalState> & local) {
		auto cdata = local.ref().storage().store(ContactData {
			.prev = {},
			.identity = peer.identity()->finalOwner().data(),
			.name = std::nullopt,
		});

		Contact contact(shared_ptr<Contact::Priv>(new Contact::Priv {
			.data = { cdata },
		}));

		auto contacts = local->shared<Set<Contact>>();

		return local.ref().storage().store(local->shared<Set<Contact>>(
			contacts.add(local.ref().storage(), contact)));
	});

	return peer.tempStorage().store(ContactAccepted {});
}

void ContactService::handlePairingResult(Context & ctx, Stored<ContactAccepted>)
{
	auto cdata = ctx.local().ref().storage().store(ContactData {
		.prev = {},
		.identity = ctx.peer().identity()->finalOwner().data(),
		.name = std::nullopt,
	});

	Contact contact(shared_ptr<Contact::Priv>(new Contact::Priv {
		.data = { cdata },
	}));

	auto contacts = ctx.local()->shared<Set<Contact>>();

	ctx.local(ctx.local()->shared<Set<Contact>>(
		contacts.add(ctx.local().ref().storage(), contact)));
}

ContactAccepted ContactAccepted::load(const Ref &)
{
	return ContactAccepted {};
}

Ref ContactAccepted::store(const Storage & st) const
{
	vector<Record::Item> items;
	items.emplace_back("accept", "");
	return st.storeObject(Record(std::move(items)));
}