diff options
Diffstat (limited to 'src/Channel.cpp')
-rw-r--r-- | src/Channel.cpp | 77 |
1 files changed, 66 insertions, 11 deletions
diff --git a/src/Channel.cpp b/src/Channel.cpp index f02b97a..ef252f6 100644 --- a/src/Channel.cpp +++ b/src/Channel.cpp @@ -1,61 +1,75 @@ #include "../include/Channel.hpp" #include "../include/UsersSidePanel.hpp" #include "../include/ChannelSidePanel.hpp" +#include <odhtdb/User.hpp> +#include <odhtdb/Database.hpp> #include <cstring> using namespace std; namespace dchat { - Channel::Channel(const std::string &_name) : + 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)), - localOfflineUser("You"), - name(_name) + localUser(_localUser ? _localUser : new OfflineUser("You")) { + addUser(localUser); { - Message *message = new Message(&localOfflineUser, u8"hello, worldåäö1![emoji](https://discordemoji.com/assets/emoji/playtime.png)"); + Message *message = new Message(&systemUser, u8"hello, worldåäö1![emoji](https://discordemoji.com/assets/emoji/playtime.png)"); messageBoard.addMessage(message); } { - Message *message = new Message(&localOfflineUser, u8"hello, world2![emoji](https://discordemoji.com/assets/emoji/Feels3DMan.gif)"); + Message *message = new Message(&systemUser, u8"hello, world2![emoji](https://discordemoji.com/assets/emoji/Feels3DMan.gif)"); messageBoard.addMessage(message); } { - Message *message = new Message(&localOfflineUser, 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."); + 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(&localOfflineUser, u8"Lorem ipsumdolorsitamet,consecteturadipiscingelit,seddoeiusmodtemporincididuntutaboreetdoloremagnaaliqua.Utenimadminimveniam"); + Message *message = new Message(&systemUser, u8"Lorem ipsumdolorsitamet,consecteturadipiscingelit,seddoeiusmodtemporincididuntutaboreetdoloremagnaaliqua.Utenimadminimveniam"); messageBoard.addMessage(message); } { - Message *message = new Message(&localOfflineUser, u8"xddd"); + Message *message = new Message(&systemUser, u8"xddd"); messageBoard.addMessage(message); } { - Message *message = new Message(&localOfflineUser, u8"[emoji](https://discordemoji.com/assets/emoji/SlowbroDumb.png)"); + Message *message = new Message(&systemUser, u8"[emoji](https://discordemoji.com/assets/emoji/SlowbroDumb.png)"); messageBoard.addMessage(message); } { - Message *message = new Message(&localOfflineUser, u8"Message after big emoji"); + 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 &localOfflineUser; + return localUser; } MessageBoard& Channel::getMessageBoard() @@ -68,6 +82,47 @@ namespace dchat 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); |