aboutsummaryrefslogtreecommitdiff
path: root/bridge
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2018-08-08 23:17:10 +0200
committerdec05eba <dec05eba@protonmail.com>2018-08-08 23:17:14 +0200
commit06f30543730c372226c398c11b3de0213d711d13 (patch)
treed6229ff224a9e7e5457c856747c2f8726e7a3868 /bridge
parent26edc563cb0ba1a9fb35507e7f32d2d43a845e68 (diff)
Add support for discord
Diffstat (limited to 'bridge')
-rw-r--r--bridge/BridgeService.cpp41
-rw-r--r--bridge/BridgeService.hpp41
-rw-r--r--bridge/DiscordService.cpp43
-rw-r--r--bridge/DiscordService.hpp28
4 files changed, 153 insertions, 0 deletions
diff --git a/bridge/BridgeService.cpp b/bridge/BridgeService.cpp
new file mode 100644
index 0000000..0b52643
--- /dev/null
+++ b/bridge/BridgeService.cpp
@@ -0,0 +1,41 @@
+#include "BridgeService.hpp"
+
+namespace dchat
+{
+ BridgeServiceUser::BridgeServiceUser(Type _type, const std::string &_name) :
+ User(User::Type::OTHER),
+ type(_type),
+ name(_name)
+ {
+
+ }
+
+ BridgeService::BridgeService()
+ {
+
+ }
+
+ BridgeService::~BridgeService()
+ {
+ for(User *user : users)
+ {
+ delete user;
+ }
+ }
+
+ bool BridgeService::addUser(BridgeServiceUser *user)
+ {
+ for(User *existingUser : users)
+ {
+ if(static_cast<BridgeServiceUser*>(existingUser)->equals(user))
+ return false;
+ }
+ users.push_back(user);
+ return true;
+ }
+
+ const std::vector<User*>& BridgeService::getUsers() const
+ {
+ return users;
+ }
+} \ No newline at end of file
diff --git a/bridge/BridgeService.hpp b/bridge/BridgeService.hpp
new file mode 100644
index 0000000..e536c81
--- /dev/null
+++ b/bridge/BridgeService.hpp
@@ -0,0 +1,41 @@
+#pragma once
+
+#include <vector>
+#include <string>
+#include "../include/User.hpp"
+
+namespace dchat
+{
+ class BridgeServiceUser : public User
+ {
+ public:
+ enum class Type
+ {
+ DISCORD
+ };
+
+ BridgeServiceUser(Type type, const std::string &name);
+ virtual ~BridgeServiceUser(){}
+
+ virtual const std::string& getName() const override { return name; }
+ virtual bool isConnected(i64 timestampUtcSec) const override { return true; }
+
+ virtual bool equals(BridgeServiceUser *other) const = 0;
+
+ const Type type;
+ private:
+ std::string name;
+ };
+
+ class BridgeService
+ {
+ public:
+ BridgeService();
+ virtual ~BridgeService();
+
+ bool addUser(BridgeServiceUser *user);
+ const std::vector<User*>& getUsers() const;
+ protected:
+ std::vector<User*> users;
+ };
+} \ No newline at end of file
diff --git a/bridge/DiscordService.cpp b/bridge/DiscordService.cpp
new file mode 100644
index 0000000..a4546a8
--- /dev/null
+++ b/bridge/DiscordService.cpp
@@ -0,0 +1,43 @@
+#include "DiscordService.hpp"
+
+namespace dchat
+{
+ DiscordServiceUser::DiscordServiceUser(const std::string &name, const u64 _id, bool _connected) :
+ BridgeServiceUser(Type::DISCORD, name),
+ id(_id),
+ connected(_connected)
+ {
+
+ }
+
+ bool DiscordServiceUser::equals(BridgeServiceUser *other) const
+ {
+ return other && type == other->type && id == static_cast<DiscordServiceUser*>(other)->id;
+ }
+
+ bool DiscordService::removeUser(u64 discordUserId)
+ {
+ for(auto it = users.begin(), end = users.end(); it != end; ++it)
+ {
+ BridgeServiceUser *serviceUser = static_cast<BridgeServiceUser*>(*it);
+ if(serviceUser->type == BridgeServiceUser::Type::DISCORD && static_cast<DiscordServiceUser*>(serviceUser)->id == discordUserId)
+ {
+ delete serviceUser;
+ users.erase(it);
+ return true;
+ }
+ }
+ return false;
+ }
+
+ DiscordServiceUser* DiscordService::getUserById(u64 discordUserId)
+ {
+ for(User *user : users)
+ {
+ BridgeServiceUser *serviceUser = static_cast<BridgeServiceUser*>(user);
+ if(serviceUser->type == BridgeServiceUser::Type::DISCORD && static_cast<DiscordServiceUser*>(serviceUser)->id == discordUserId)
+ return static_cast<DiscordServiceUser*>(serviceUser);
+ }
+ return nullptr;
+ }
+} \ No newline at end of file
diff --git a/bridge/DiscordService.hpp b/bridge/DiscordService.hpp
new file mode 100644
index 0000000..c778c41
--- /dev/null
+++ b/bridge/DiscordService.hpp
@@ -0,0 +1,28 @@
+#pragma once
+
+#include "BridgeService.hpp"
+
+namespace dchat
+{
+ class DiscordServiceUser : public BridgeServiceUser
+ {
+ public:
+ DiscordServiceUser(const std::string &name, const u64 id, bool connected);
+ virtual ~DiscordServiceUser(){}
+
+ virtual bool isConnected(i64 timestampUtcSec) const override { return connected; }
+ bool equals(BridgeServiceUser *other) const override;
+
+ const u64 id;
+ bool connected;
+ };
+
+ class DiscordService : public BridgeService
+ {
+ public:
+ virtual ~DiscordService(){}
+
+ bool removeUser(u64 discordUserId);
+ DiscordServiceUser* getUserById(u64 discordUserId);
+ };
+} \ No newline at end of file