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
|
#include "../include/Channel.hpp"
#include <odhtdb/User.hpp>
#include <odhtdb/Database.hpp>
#include <cstring>
using namespace std;
namespace dchat
{
static Channel *currentChannel;
Channel::Channel(const string &_name, const odhtdb::DatabaseNode &_databaseNodeInfo, User *_localUser, odhtdb::Database *_database) :
database(_database),
databaseNodeInfo(_databaseNodeInfo),
name(_name),
messageBoard(sf::Vector2u(1.0f, 1.0f)),
localUser(_localUser ? _localUser : new OfflineUser("You"))
{
addUser(localUser);
{
Message *message = new Message(&systemUser, u8"hello, worldåäö1![emoji](https://discordemoji.com/assets/emoji/playtime.png)");
messageBoard.addMessage(message);
}
{
Message *message = new Message(&systemUser, u8"hello, world2![emoji](https://discordemoji.com/assets/emoji/Feels3DMan.gif)");
messageBoard.addMessage(message);
}
{
Message *message = new Message(&systemUser, u8"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
messageBoard.addMessage(message);
}
{
Message *message = new Message(&systemUser, u8"Lorem ipsumdolorsitamet,consecteturadipiscingelit,seddoeiusmodtemporincididuntutaboreetdoloremagnaaliqua.Utenimadminimveniam");
messageBoard.addMessage(message);
}
{
Message *message = new Message(&systemUser, u8"xddd");
messageBoard.addMessage(message);
}
{
Message *message = new Message(&systemUser, u8"[emoji](https://discordemoji.com/assets/emoji/SlowbroDumb.png)");
messageBoard.addMessage(message);
}
{
Message *message = new Message(&systemUser, u8"Message after big emoji");
messageBoard.addMessage(message);
}
if(database)
database->seed(databaseNodeInfo);
}
Channel::~Channel()
{
if(database)
database->stopSeeding(*databaseNodeInfo.getRequestHash());
for(User *user : users)
{
delete user;
}
}
User* Channel::getLocalUser()
{
return localUser;
}
MessageBoard& Channel::getMessageBoard()
{
return messageBoard;
}
const string& Channel::getName() const
{
return name;
}
const vector<User*> Channel::getUsers() const
{
return users;
}
User* Channel::getUserByPublicKey(const odhtdb::Signature::PublicKey &publicKey)
{
auto userIt = publicKeyOnlineUsersMap.find(publicKey);
if(userIt != publicKeyOnlineUsersMap.end())
return userIt->second;
return nullptr;
}
void Channel::addLocalMessage(const std::string &msg, User *owner)
{
assert(owner);
messageBoard.addMessage(new Message(owner, msg));
}
void Channel::addMessage(const std::string &msg)
{
messageBoard.addMessage(new Message(localUser, msg));
if(database && localUser->type == User::Type::ONLINE)
{
auto onlineUser = static_cast<OnlineUser*>(localUser);
assert(onlineUser->databaseUser->getType() == odhtdb::User::Type::LOCAL);
database->addData(databaseNodeInfo, static_cast<const odhtdb::LocalUser*>(onlineUser->databaseUser), odhtdb::DataView((void*)msg.data(), msg.size()));
database->commit();
}
}
void Channel::addUser(User *user)
{
users.push_back(user);
if(user->type == User::Type::ONLINE)
{
auto onlineUser = static_cast<OnlineUser*>(user);
publicKeyOnlineUsersMap[onlineUser->databaseUser->getPublicKey()] = onlineUser;
}
}
void Channel::processEvent(const sf::Event &event)
{
chatbar.processEvent(event, this);
messageBoard.processEvent(event);
}
void Channel::draw(sf::RenderWindow &window, Cache &cache)
{
messageBoard.draw(window, cache);
chatbar.draw(window);
}
void Channel::setCurrent(Channel *channel)
{
currentChannel = channel;
}
Channel* Channel::getCurrent()
{
return currentChannel;
}
}
|