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

#include <erebos/service.h>

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

namespace erebos {

using std::unique_ptr;

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:
	DirectMessageService();
	virtual ~DirectMessageService();

	UUID uuid() const override;
	void handle(Context &) override;

	typedef std::function<void(const DirectMessageThread &, ssize_t, ssize_t)> ThreadWatcher;
	void onUpdate(ThreadWatcher);
	DirectMessageThread thread(const Identity &);

	DirectMessage send(const Identity &, const Peer &, const std::string &);

private:
	struct Priv;
	unique_ptr<Priv> p;
};

}