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
|
#include "../include/Channel.hpp"
#include <odhtdb/User.hpp>
#include <odhtdb/Database.hpp>
#include <odhtdb/bin2hex.hpp>
#include <cstring>
#include <sibs/SafeSerializer.hpp>
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"))
{
addUserLocally(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/PepeDab.gif)");
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;
}
SystemUser* Channel::getSystemUser()
{
return &systemUser;
}
MessageBoard& Channel::getMessageBoard()
{
return messageBoard;
}
const string& Channel::getName() const
{
return name;
}
const vector<User*> Channel::getUsers() const
{
return users;
}
OnlineUser* Channel::getUserByPublicKey(const odhtdb::Signature::PublicKey &publicKey)
{
auto userIt = publicKeyOnlineUsersMap.find(publicKey);
if(userIt != publicKeyOnlineUsersMap.end())
return userIt->second;
return nullptr;
}
const odhtdb::DatabaseNode& Channel::getNodeInfo() const
{
return databaseNodeInfo;
}
void Channel::addLocalMessage(const std::string &msg, User *owner, u64 timestampSeconds)
{
assert(owner);
messageBoard.addMessage(new Message(owner, msg, timestampSeconds));
}
void Channel::addMessage(const std::string &msg)
{
if(database && localUser->type == User::Type::ONLINE)
{
auto localOnlineUser = static_cast<OnlineUser*>(localUser);
assert(localOnlineUser->databaseUser->getType() == odhtdb::User::Type::LOCAL);
sibs::SafeSerializer serializer;
serializer.add(ChannelDataType::MESSAGE);
serializer.add((const u8*)msg.data(), msg.size());
database->addData(databaseNodeInfo, static_cast<const odhtdb::LocalUser*>(localOnlineUser->databaseUser), odhtdb::DataView(serializer.getBuffer().data(), serializer.getBuffer().size()));
database->commit();
}
else
addLocalMessage(msg, localUser, 0);
}
void Channel::addUserLocally(User *user)
{
users.push_back(user);
if(user->type == User::Type::ONLINE)
{
auto onlineUser = static_cast<OnlineUser*>(user);
publicKeyOnlineUsersMap[onlineUser->databaseUser->getPublicKey()] = onlineUser;
}
}
bool Channel::addUser(const odhtdb::Signature::PublicKey &userId, const string &groupId)
{
assert(database);
if(!database || localUser->type != User::Type::ONLINE)
return false;
if(groupId.size() != odhtdb::GROUP_ID_LENGTH)
{
fprintf(stderr, "Group id is wrong size. Expected to be %u bytes, was %u byte(s)\n", odhtdb::GROUP_ID_LENGTH, groupId.size());
return false;
}
auto localOnlineUser = static_cast<OnlineUser*>(localUser);
assert(localOnlineUser->databaseUser->getType() == odhtdb::User::Type::LOCAL);
uint8_t groupIdRaw[odhtdb::GROUP_ID_LENGTH];
memcpy(groupIdRaw, groupId.data(), groupId.size());
auto groupToAddUserTo = database->getStorage().getGroupById(*databaseNodeInfo.getRequestHash(), groupIdRaw);
if(!groupToAddUserTo)
{
fprintf(stderr, "Group with id %s does not exist in channel %s\n", odhtdb::bin2hex(groupId.c_str(), groupId.size()).c_str(), databaseNodeInfo.getRequestHash()->toString().c_str());
return false;
}
database->addUser(databaseNodeInfo, static_cast<const odhtdb::LocalUser*>(localOnlineUser->databaseUser), "noname", userId, groupToAddUserTo);
return true;
}
void Channel::replaceLocalUser(User *newLocalUser)
{
for(vector<User*>::iterator it = users.begin(); it != users.end(); ++it)
{
if(*it == localUser)
{
users.erase(it);
delete localUser;
break;
}
}
localUser = newLocalUser;
addUserLocally(newLocalUser);
}
void Channel::changeNick(const std::string &newNick)
{
if(database && localUser->type == User::Type::ONLINE)
{
auto localOnlineUser = static_cast<OnlineUser*>(localUser);
assert(localOnlineUser->databaseUser->getType() == odhtdb::User::Type::LOCAL);
sibs::SafeSerializer serializer;
serializer.add(ChannelDataType::NICKNAME_CHANGE);
serializer.add((u8)newNick.size());
serializer.add((const u8*)newNick.data(), newNick.size());
database->addData(databaseNodeInfo, static_cast<const odhtdb::LocalUser*>(localOnlineUser->databaseUser), odhtdb::DataView(serializer.getBuffer().data(), serializer.getBuffer().size()));
database->commit();
}
}
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;
}
}
|