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 --- include/Channel.hpp | 21 +++++++++++++++++++-- include/Chatbar.hpp | 2 ++ include/MessageBoard.hpp | 3 ++- include/Rpc.hpp | 21 +++++++++++++++++++++ include/Suggestions.hpp | 19 +++++++++++++++++++ include/User.hpp | 15 +++++++++++++++ 6 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 include/Rpc.hpp create mode 100644 include/Suggestions.hpp (limited to 'include') diff --git a/include/Channel.hpp b/include/Channel.hpp index af6cbf2..663b163 100644 --- a/include/Channel.hpp +++ b/include/Channel.hpp @@ -5,7 +5,10 @@ #include "User.hpp" #include "Channel.hpp" #include "types.hpp" +#include "Suggestions.hpp" +#include "../bridge/DiscordService.hpp" #include +#include #include #include #include @@ -28,7 +31,9 @@ namespace dchat DELETE_MESSAGE, NICKNAME_CHANGE, CHANGE_AVATAR, - CHANGE_CHANNEL_NAME + CHANGE_CHANNEL_NAME, + + ADD_DISCORD_MESSAGE }; class Channel @@ -44,7 +49,7 @@ namespace dchat MessageBoard& getMessageBoard(); const std::string& getName() const; - const std::vector getUsers() const; + const std::vector& getUsers() const; OnlineUser* getUserByPublicKey(const odhtdb::Signature::PublicKey &publicKey); const odhtdb::DatabaseNode& getNodeInfo() const; Message* getLatestMessage(); @@ -52,8 +57,10 @@ namespace dchat // If timestamp is 0, then current time is used void addLocalMessage(const std::string &msg, User *owner, u64 timestampSeconds = 0); void addLocalMessage(const std::string &msg, User *owner, u64 timestampSeconds, const odhtdb::Hash &id); + void addLocalDiscordMessage(const std::string &discordUserName, u64 discordUserId, const std::string &msg, User *owner, u64 timestampSeconds, const odhtdb::Hash &id); void addSystemMessage(const std::string &msg, bool plainText = true); void addMessage(const std::string &msg); + void addDiscordMessage(const std::string &discordUserName, u64 discordUserId, const std::string &msg); void deleteLocalMessage(const odhtdb::Hash &id, const odhtdb::Signature::PublicKey &requestedByUser); void deleteMessage(const odhtdb::Hash &id, const odhtdb::Signature::PublicKey &requestedByUser); @@ -64,6 +71,9 @@ namespace dchat void setAvatar(const std::string &newAvatarUrl); void setNameLocally(const std::string &name); void setName(const std::string &name); + + // Returns -1 on failure + int getUserLowestPermissionLevel(OnlineUser *user) const; void processEvent(const sf::Event &event, Cache &cache); void draw(sf::RenderWindow &window, Cache &cache); @@ -71,6 +81,9 @@ namespace dchat void update(); // Returns 0 if we are offline u32 getSyncedTimestampUtcInSec(); + + const std::vector& getBridgeServices() const; + DiscordService* getDiscordService(); static void setCurrent(Channel *channel); static Channel* getCurrent(); @@ -82,12 +95,16 @@ namespace dchat std::string name; MessageBoard messageBoard; Chatbar chatbar; + Suggestions suggestions; User *localUser; SystemUser systemUser; std::vector users; + std::unordered_map discordUserById; odhtdb::Signature::MapPublicKey publicKeyOnlineUsersMap; dht::InfoHash pingKey; std::future pingListener; sf::Clock pingTimer; + + std::vector bridgeServices; }; } diff --git a/include/Chatbar.hpp b/include/Chatbar.hpp index df492ac..10bbebd 100644 --- a/include/Chatbar.hpp +++ b/include/Chatbar.hpp @@ -24,6 +24,8 @@ namespace dchat void processEvent(const sf::Event &event, Cache &cache, Channel *channel); void draw(sf::RenderWindow &window, Cache &cache); + static sf::Vector2f getInputPosition(sf::RenderWindow &window); + static sf::Vector2f getInputSize(sf::RenderWindow &window); static float getHeight(); static bool addBind(const std::string &key, const std::string &value, bool updateFile = true); diff --git a/include/MessageBoard.hpp b/include/MessageBoard.hpp index fdde8c7..1d1f523 100644 --- a/include/MessageBoard.hpp +++ b/include/MessageBoard.hpp @@ -27,11 +27,12 @@ namespace dchat void draw(sf::RenderWindow &window, Cache &cache); Message* getLatestMessage(); + const std::vector& getMessages() const; private: usize findPositionToInsertMessageByTimestamp(Message *message); void updateStaticContentTexture(const sf::Vector2u &newSize); - void addMessage(Message *message, const odhtdb::Hash &id); + bool addMessage(Message *message, const odhtdb::Hash &id); void deleteMessage(const odhtdb::Hash &id, const odhtdb::Signature::PublicKey &requestedByUser); void drawDefault(sf::RenderWindow &window, Cache &cache); diff --git a/include/Rpc.hpp b/include/Rpc.hpp new file mode 100644 index 0000000..c8b47e0 --- /dev/null +++ b/include/Rpc.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include +#include "types.hpp" +#include + +namespace dchat +{ + using RpcRecvCallbackFunc = std::function; + + class Rpc + { + public: + Rpc(u16 port); + void recv(RpcRecvCallbackFunc recvCallbackFunc); + bool send(const void *data, const usize size); + private: + zmq::context_t context; + zmq::socket_t socket; + }; +} \ No newline at end of file diff --git a/include/Suggestions.hpp b/include/Suggestions.hpp new file mode 100644 index 0000000..56f3afa --- /dev/null +++ b/include/Suggestions.hpp @@ -0,0 +1,19 @@ +#pragma once + +#include "Text.hpp" +#include "Cache.hpp" +#include +#include +#include + +namespace dchat +{ + class Suggestions + { + public: + void show(const std::vector &texts); + void draw(sf::RenderWindow &window, Cache &cache); + private: + std::vector> texts; + }; +} \ No newline at end of file diff --git a/include/User.hpp b/include/User.hpp index c2874c2..7e99c60 100644 --- a/include/User.hpp +++ b/include/User.hpp @@ -11,8 +11,10 @@ namespace dchat public: enum class Type { + OTHER, ONLINE_REMOTE_USER, ONLINE_LOCAL_USER, + ONLINE_DISCORD_USER, OFFLINE, SYSTEM }; @@ -21,6 +23,8 @@ namespace dchat virtual ~User(){} virtual const std::string& getName() const = 0; virtual bool isOnlineUser() const { return false; } + + virtual bool isConnected(i64 timestampUtcSec) const { return true; } const Type type; std::string avatarUrl; @@ -35,6 +39,7 @@ namespace dchat virtual const std::string& getName() const override; virtual const odhtdb::Signature::PublicKey& getPublicKey() const = 0; + bool isConnected(i64 timestampUtcSec) const override; bool isOnlineUser() const override { return true; } std::string name; @@ -58,6 +63,16 @@ namespace dchat const odhtdb::Signature::KeyPair keyPair; }; + + class OnlineDiscordUser : public OnlineUser + { + public: + OnlineDiscordUser(const std::string &discordUserName, u64 discordUserId, User *bridgeOwner); + virtual const odhtdb::Signature::PublicKey& getPublicKey() const override; + + u64 discordUserId; + User *bridgeOwner; + }; class OfflineUser : public User { -- cgit v1.2.3