summaryrefslogtreecommitdiff
path: root/include/erebos/message.h
blob: 011007a8837771b9d1982d997525bf1b5fc1949a (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
#pragma once

#include <erebos/service.h>

#include <chrono>
#include <functional>
#include <memory>
#include <optional>
#include <string>

namespace erebos {

using std::unique_ptr;

class Contact;
class Identity;

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;
	struct Priv;
	DirectMessageThread(Priv *);
	std::shared_ptr<Priv> p;
};

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:
	const Config config;
	const Server & server;
};

}