aboutsummaryrefslogtreecommitdiff
path: root/include/Channel.hpp
blob: af6cbf28922ee66b84bd683ce2147c729d878bf3 (plain)
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
#pragma once

#include "MessageBoard.hpp"
#include "Chatbar.hpp"
#include "User.hpp"
#include "Channel.hpp"
#include "types.hpp"
#include <vector>
#include <SFML/System/Clock.hpp>
#include <odhtdb/DatabaseNode.hpp>
#include <odhtdb/Signature.hpp>
#include <odhtdb/Group.hpp>
#include <odhtdb/Hash.hpp>
#include <odhtdb/DhtKey.hpp>
#include <future>

namespace odhtdb
{
    class Database;
}

namespace dchat
{
    enum class ChannelDataType : u8
    {
        ADD_MESSAGE,
        EDIT_MESSAGE,
        DELETE_MESSAGE,
        NICKNAME_CHANGE,
        CHANGE_AVATAR,
        CHANGE_CHANNEL_NAME
    };
    
    class Channel
    {
    public:
        Channel(const std::string &name, const odhtdb::DatabaseNode &databaseNodeInfo = odhtdb::DatabaseNode(), User *localUser = nullptr, odhtdb::Database *database = nullptr);
        virtual ~Channel();
        Channel(const Channel& other) = delete;
        Channel& operator = (const Channel &other) = delete;
        
        User* getLocalUser();
        SystemUser* getSystemUser();
        MessageBoard& getMessageBoard();
        
        const std::string& getName() const;
        const std::vector<User*> getUsers() const;
        OnlineUser* getUserByPublicKey(const odhtdb::Signature::PublicKey &publicKey);
        const odhtdb::DatabaseNode& getNodeInfo() const;
        Message* getLatestMessage();
        
        // 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 addSystemMessage(const std::string &msg, bool plainText = true);
        void addMessage(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);
        
        void addUserLocally(User *user);
        bool addUser(const odhtdb::Signature::PublicKey &userId, const odhtdb::DataView &groupId);
        void replaceLocalUser(OnlineLocalUser *newOnlineLocalUser);
        void changeNick(const std::string &newNick);
        void setAvatar(const std::string &newAvatarUrl);
        void setNameLocally(const std::string &name);
        void setName(const std::string &name);
        
        void processEvent(const sf::Event &event, Cache &cache);
        void draw(sf::RenderWindow &window, Cache &cache);
        
        void update();
        // Returns 0 if we are offline
        u32 getSyncedTimestampUtcInSec();
        
        static void setCurrent(Channel *channel);
        static Channel* getCurrent();
    private:
        void sendPing(u32 pingTimestampSec);
    protected:
        odhtdb::Database *database;
        odhtdb::DatabaseNode databaseNodeInfo;
        std::string name;
        MessageBoard messageBoard;
        Chatbar chatbar;
        User *localUser;
        SystemUser systemUser;
        std::vector<User*> users;
        odhtdb::Signature::MapPublicKey<OnlineUser*> publicKeyOnlineUsersMap;
        dht::InfoHash pingKey;
        std::future<size_t> pingListener;
        sf::Clock pingTimer;
    };
}