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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
#include "message.h"
#include <erebos/network.h>
using namespace erebos;
using std::nullopt;
using std::scoped_lock;
using std::unique_lock;
static const UUID myUUID("c702076c-4928-4415-8b6b-3e839eafcb0d");
static vector<DirectMessageThread> threadList;
static mutex threadLock;
DirectMessageThread DirectMessageThread::Priv::getThreadLocked(const Identity & peer)
{
for (const auto & t : threadList)
if (t.p->peer.sameAs(peer))
return t;
DirectMessageThread t(new DirectMessageThread::Priv {
.peer = peer,
.head = {},
});
threadList.push_back(t);
return t;
}
DirectMessageThread DirectMessageThread::Priv::updateThreadLocked(const Identity & peer, vector<Stored<DirectMessageData>> && head)
{
DirectMessageThread nt(new DirectMessageThread::Priv {
.peer = peer,
.head = std::move(head),
});
for (auto & t : threadList)
if (t.p->peer.sameAs(peer)) {
t = nt;
return nt;
}
threadList.push_back(nt);
return nt;
}
DirectMessage::DirectMessage(Priv * p):
p(p)
{}
DirectMessageData DirectMessageData::load(const Ref & ref)
{
auto rec = ref->asRecord();
if (!rec)
return DirectMessageData();
vector<Stored<DirectMessageData>> prev;
for (auto p : rec->items("PREV"))
if (const auto & x = p.as<DirectMessageData>())
prev.push_back(*x);
auto fref = rec->item("from").asRef();
return DirectMessageData {
.prev = std::move(prev),
.from = fref ? Identity::load(*fref) : nullopt,
.time = *rec->item("time").asDate(),
.text = rec->item("text").asText().value(),
};
}
Ref DirectMessageData::store(const Storage & st) const
{
vector<Record::Item> items;
for (const auto & prev : prev)
items.emplace_back("PREV", prev.ref());
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 optional<Identity> & DirectMessage::from() const
{
return p->data->from;
}
const optional<ZonedTime> & DirectMessage::time() const
{
return p->data->time;
}
string DirectMessage::text() const
{
if (p->data->text)
return p->data->text.value();
return "";
}
DirectMessageThread::DirectMessageThread(Priv * p):
p(p)
{}
DirectMessageThread::Iterator::Iterator(Priv * p):
p(p)
{}
DirectMessageThread::Iterator::Iterator(const Iterator & other):
Iterator(new Priv(*other.p))
{}
DirectMessageThread::Iterator::~Iterator() = default;
DirectMessageThread::Iterator & DirectMessageThread::Iterator::operator=(const Iterator & other)
{
p.reset(new Priv(*other.p));
return *this;
}
DirectMessageThread::Iterator & DirectMessageThread::Iterator::operator++()
{
if (p->current)
for (const auto & m : p->current->p->data->prev)
p->next.push_back(m);
if (p->next.empty()) {
p->current.reset();
} else {
filterAncestors(p->next);
auto ncur = p->next[0];
for (const auto & m : p->next)
if (!ncur->time || (m->time && m->time->time >= ncur->time->time))
ncur = m;
p->current.emplace(DirectMessage(new DirectMessage::Priv {
.data = ncur,
}));
p->next.erase(std::remove(p->next.begin(), p->next.end(), p->current->p->data));
}
return *this;
}
DirectMessage DirectMessageThread::Iterator::operator*() const
{
return *p->current;
}
bool DirectMessageThread::Iterator::operator==(const Iterator & other) const
{
if (p->current && other.p->current)
return p->current->p->data == other.p->current->p->data;
return bool(p->current) == bool(other.p->current);
}
bool DirectMessageThread::Iterator::operator!=(const Iterator & other) const
{
return !(*this == other);
}
DirectMessageThread::Iterator DirectMessageThread::begin() const
{
return ++Iterator(new Iterator::Priv {
.current = {},
.next = p->head,
});
}
DirectMessageThread::Iterator DirectMessageThread::end() const
{
return Iterator(new Iterator::Priv {
.current = {},
.next = {},
});
}
size_t DirectMessageThread::size() const
{
size_t c = 0;
for (auto it = begin(); it != end(); ++it)
c++;
return c;
}
DirectMessage DirectMessageThread::at(size_t i) const
{
return *std::next(begin(), i);
}
const Identity & DirectMessageThread::peer() const
{
return p->peer;
}
DirectMessageService::DirectMessageService():
p(new Priv)
{}
DirectMessageService::~DirectMessageService() = default;
UUID DirectMessageService::uuid() const
{
return myUUID;
}
void DirectMessageService::handle(Context & ctx)
{
auto pid = ctx.peer().identity();
if (!pid)
return;
auto powner = pid->finalOwner();
unique_lock lock(threadLock);
vector<Stored<DirectMessageData>> head(DirectMessageThread::Priv::getThreadLocked(powner).p->head);
head.push_back(Stored<DirectMessageData>::load(ctx.ref()));
filterAncestors(head);
auto dmt = DirectMessageThread::Priv::updateThreadLocked(powner, std::move(head));
lock.unlock();
for (const auto & w : p->watchers)
w(dmt, -1, -1);
}
void DirectMessageService::onUpdate(ThreadWatcher w)
{
scoped_lock l(p->watcherLock);
p->watchers.push_back(w);
}
DirectMessageThread DirectMessageService::thread(const Identity & peer)
{
scoped_lock lock(threadLock);
return DirectMessageThread::Priv::getThreadLocked(peer.finalOwner());
}
DirectMessage DirectMessageService::send(const Identity & from, const Peer & peer, const string & text)
{
auto pid = peer.identity();
if (!pid)
throw std::runtime_error("Peer without known identity");
auto powner = pid->finalOwner();
scoped_lock lock(threadLock);
auto msg = from.ref()->storage().store(DirectMessageData {
.prev = DirectMessageThread::Priv::getThreadLocked(powner).p->head,
.from = from.finalOwner(),
.time = ZonedTime::now(),
.text = text,
});
DirectMessageThread::Priv::updateThreadLocked(powner, { msg });
peer.send(myUUID, msg.ref());
return DirectMessage(new DirectMessage::Priv {
.data = msg,
});
}
|