blob: 194044cba0258f01be777a532123ac30ae700905 (
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
|
#pragma once
#include <erebos/merge.h>
#include <erebos/service.h>
#include <chrono>
#include <functional>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
namespace erebos {
using std::mutex;
using std::unique_ptr;
class Contact;
class Identity;
struct DirectMessageState;
class DirectMessage
{
public:
const std::optional<Identity> & from() const;
const std::optional<struct ZonedTime> & time() const;
std::string text() const;
private:
friend class DirectMessageThread;
friend class DirectMessageService;
struct Priv;
DirectMessage(Priv *);
std::shared_ptr<Priv> p;
};
class DirectMessageThread
{
public:
class Iterator
{
struct Priv;
Iterator(Priv *);
public:
using iterator_category = std::forward_iterator_tag;
using value_type = DirectMessage;
using difference_type = ssize_t;
using pointer = const DirectMessage *;
using reference = const DirectMessage &;
Iterator(const Iterator &);
~Iterator();
Iterator & operator=(const Iterator &);
Iterator & operator++();
value_type operator*() const;
bool operator==(const Iterator &) const;
bool operator!=(const Iterator &) const;
private:
friend DirectMessageThread;
std::unique_ptr<Priv> p;
};
Iterator begin() const;
Iterator end() const;
size_t size() const;
DirectMessage at(size_t) const;
const Identity & peer() const;
private:
friend class DirectMessageService;
friend class DirectMessageThreads;
struct Priv;
DirectMessageThread(Priv *);
std::shared_ptr<Priv> p;
};
class DirectMessageThreads
{
public:
DirectMessageThreads();
DirectMessageThreads(Stored<DirectMessageState>);
DirectMessageThreads(vector<Stored<DirectMessageState>>);
static DirectMessageThreads load(const vector<Ref> & refs);
vector<Ref> store() const;
vector<Stored<DirectMessageState>> data() const;
bool operator==(const DirectMessageThreads &) const;
bool operator!=(const DirectMessageThreads &) const;
DirectMessageThread thread(const Identity &) const;
private:
vector<Stored<DirectMessageState>> state;
friend class DirectMessageService;
};
DECLARE_SHARED_TYPE(DirectMessageThreads)
template<> struct Mergeable<DirectMessageThreads>
{
using Component = DirectMessageState;
static vector<Stored<DirectMessageState>> components(const DirectMessageThreads &);
static Contact merge(vector<Stored<DirectMessageState>>);
};
class DirectMessageService : public Service
{
public:
using ThreadWatcher = std::function<void(const DirectMessageThread &, ssize_t, ssize_t)>;
class Config
{
public:
Config & onUpdate(ThreadWatcher);
private:
friend class DirectMessageService;
vector<ThreadWatcher> watchers;
};
DirectMessageService(Config &&, const Server &);
virtual ~DirectMessageService();
UUID uuid() const override;
void handle(Context &) override;
DirectMessageThread thread(const Identity &);
DirectMessage send(const Identity &, const std::string &);
DirectMessage send(const Contact &, const std::string &);
DirectMessage send(const Peer &, const std::string &);
private:
void updateHandler(const DirectMessageThreads &);
const Config config;
const Server & server;
vector<Stored<DirectMessageState>> prevState;
mutex stateMutex;
Watched<DirectMessageThreads> watched;
};
}
|