From 06f30543730c372226c398c11b3de0213d711d13 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Wed, 8 Aug 2018 23:17:10 +0200 Subject: Add support for discord --- src/Channel.cpp | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 83 insertions(+), 3 deletions(-) (limited to 'src/Channel.cpp') diff --git a/src/Channel.cpp b/src/Channel.cpp index 75d805c..d13c476 100644 --- a/src/Channel.cpp +++ b/src/Channel.cpp @@ -17,12 +17,25 @@ namespace dchat messageBoard(this), localUser(_localUser ? _localUser : new OfflineUser("You")) { + bridgeServices.push_back(new DiscordService()); addUserLocally(localUser); //addLocalMessage(u8"[emoji](https://discordemoji.com/assets/emoji/PepeDab.gif) deaf [emoji](https://discordemoji.com/assets/emoji/COGGERS.gif)", &systemUser, 0, odhtdb::Hash()); //addLocalMessage(u8"[emoji](https://discordemoji.com/assets/emoji/PepeDab.gif)[emoji](https://discordemoji.com/assets/emoji/COGGERS.gif)", &systemUser, 0, odhtdb::Hash()); //addLocalMessage(u8"pepedab https://discordemoji.com/assets/emoji/PepeDab.gif coggers https://discordemoji.com/assets/emoji/COGGERS.gif check out this url http://www.grandtournation.com/6808/start-date-of-the-grand-tour-season-3-confirmed-mark-your-calendars/ owo", &systemUser, 0, odhtdb::Hash()); - // addLocalMessage(u8"ht clic", &systemUser, 0, odhtdb::Hash()); + //addLocalMessage(u8"ht clic", &systemUser, 0, odhtdb::Hash()); + + auto binds = Chatbar::getBinds(); + vector suggestionsStr; + suggestionsStr.reserve(binds.size()); + for(auto &bind : binds) + { + string suggestion = bind.first; + suggestion += " "; + suggestion += bind.second; + suggestionsStr.emplace_back(move(suggestion)); + } + suggestions.show(suggestionsStr); if(database) { @@ -67,6 +80,11 @@ namespace dchat Channel::~Channel() { + for(BridgeService *bridgeService : bridgeServices) + { + delete bridgeService; + } + if(database) { database->cancelNodeListener(pingKey, pingListener); @@ -78,6 +96,11 @@ namespace dchat { delete user; } + + for(auto &discordUserIt : discordUserById) + { + delete discordUserIt.second; + } } User* Channel::getLocalUser() @@ -100,7 +123,7 @@ namespace dchat return name; } - const vector Channel::getUsers() const + const vector& Channel::getUsers() const { return users; } @@ -137,6 +160,31 @@ namespace dchat } messageBoard.addMessage(new Message(owner, msg, timestampSeconds), id); } + + void Channel::addLocalDiscordMessage(const string &discordUserName, u64 discordUserId, const string &msg, User *owner, u64 timestampSeconds, const odhtdb::Hash &id) + { + assert(owner); + if(timestampSeconds == 0) + { + timestampSeconds = time(NULL); + } + + OnlineDiscordUser *discordUser = nullptr; + auto discordUserIt = discordUserById.find(discordUserId); + if(discordUserIt == discordUserById.end()) + { + discordUser = new OnlineDiscordUser(discordUserName, discordUserId, owner); + discordUserById[discordUserId] = discordUser; + } + else + { + // TODO: What if several users bridge same chat? the same discord user id could belong to different owners. + // Dchat channels should only allow one user to bridge data from one discord channel. Bridging data between multiple discord channels to + // one dchat channel should be allowed. + discordUser = discordUserIt->second; + } + messageBoard.addMessage(new Message(discordUser, msg, timestampSeconds), id); + } void Channel::addSystemMessage(const string &msg, bool plainText) { @@ -159,6 +207,21 @@ namespace dchat else addLocalMessage(msg, localUser, 0, odhtdb::Hash()); } + + void Channel::addDiscordMessage(const string &discordUserName, u64 discordUserId, const string &msg) + { + assert(database && localUser->type == User::Type::ONLINE_LOCAL_USER); + auto onlineLocalUser = static_cast(localUser); + + sibs::SafeSerializer serializer; + serializer.add(ChannelDataType::ADD_DISCORD_MESSAGE); + serializer.add(discordUserId); + serializer.add((u8)discordUserName.size()); + serializer.add((const u8*)discordUserName.data(), discordUserName.size()); + serializer.add((const u8*)msg.data(), msg.size()); + + database->addData(databaseNodeInfo, onlineLocalUser->keyPair, odhtdb::DataView(serializer.getBuffer().data(), serializer.getBuffer().size())); + } void Channel::deleteLocalMessage(const odhtdb::Hash &id, const odhtdb::Signature::PublicKey &requestedByUser) { @@ -282,6 +345,12 @@ namespace dchat database->addData(databaseNodeInfo, onlineLocalUser->keyPair, odhtdb::DataView(serializer.getBuffer().data(), serializer.getBuffer().size())); } } + + int Channel::getUserLowestPermissionLevel(OnlineUser *user) const + { + if(!database) return -1; + return database->getUserLowestPermissionLevel(*databaseNodeInfo.getRequestHash(), user->getPublicKey()); + } void Channel::processEvent(const sf::Event &event, Cache &cache) { @@ -293,6 +362,7 @@ namespace dchat { messageBoard.draw(window, cache); chatbar.draw(window, cache); + //suggestions.draw(window, cache); } void Channel::update() @@ -300,7 +370,7 @@ namespace dchat if(database && localUser->type == User::Type::ONLINE_LOCAL_USER && pingTimer.getElapsedTime().asMilliseconds() > 5000) { pingTimer.restart(); - sendPing(database->getSyncedTimestampUtc().seconds); + //sendPing(database->getSyncedTimestampUtc().seconds); } } @@ -327,6 +397,16 @@ namespace dchat return 0; return database->getSyncedTimestampUtc().seconds; } + + const vector& Channel::getBridgeServices() const + { + return bridgeServices; + } + + DiscordService* Channel::getDiscordService() + { + return (DiscordService*)bridgeServices[0]; + } void Channel::setCurrent(Channel *channel) { -- cgit v1.2.3