#pragma once #include #include #include "misc/advanced_mutex.h" #include "channel/TreeView.h" #include "Definitions.h" #include "Properties.h" #include "PermissionManager.h" namespace ts { class BasicChannel; class BasicChannelTree; namespace ChannelType { enum ChannelType { permanent, semipermanent, temporary }; } struct BasicChannelEntry { BasicChannelEntry* previus{}; std::shared_ptr current = nullptr; BasicChannelEntry* next{}; BasicChannelEntry* children{}; ~BasicChannelEntry() { this->current = nullptr; } }; class BasicChannel : public TreeEntry { public: BasicChannel(ChannelId parentId, ChannelId channelId); BasicChannel(std::shared_ptr parent, ChannelId channelId); virtual ~BasicChannel(); bool hasParent(){ return !!this->parent(); } std::shared_ptr parent(); inline std::string name(){ return properties()[property::CHANNEL_NAME]; } inline ChannelId channelOrder(){ return this->previousChannelId(); } inline Properties& properties() const { return *this->_properties; } ChannelType::ChannelType channelType(); void setChannelType(ChannelType::ChannelType); bool passwordMatch(std::string password, bool hashed = false); bool defaultChannel() { return (*this->_properties)[property::CHANNEL_FLAG_DEFAULT]; } int emptySince(); inline std::chrono::system_clock::time_point createdTimestamp() { return std::chrono::system_clock::time_point() + std::chrono::milliseconds(this->properties()[property::CHANNEL_CREATED_AT].as()); } inline std::shared_ptr permissions(){ return this->_permissions; } virtual void setPermissionManager(const std::shared_ptr&); virtual void setProperties(const std::shared_ptr&); protected: public: ChannelId channelId() const override; ChannelId previousChannelId() const override; void setPreviousChannelId(ChannelId id) override; void setParentChannelId(ChannelId id) override; void setLinkedHandle(const std::weak_ptr &) override; protected: std::weak_ptr _link; std::shared_ptr _properties; std::shared_ptr _permissions; ChannelId _channel_order = 0; ChannelId _channel_id = 0; }; class BasicChannelTree : public TreeView { public: BasicChannelTree(); virtual ~BasicChannelTree(); size_t channel_count() { return this->entry_count(); } std::shared_ptr findLinkedChannel(ChannelId channelId); std::shared_ptr findChannel(ChannelId channelId); std::shared_ptr findChannel(ChannelId channelId, std::deque> avariable); std::shared_ptr findChannel(const std::string &name, const std::shared_ptr &layer); std::shared_ptr findChannelByPath(const std::string& path); //std::deque> topChannels(); std::deque> channels(const std::shared_ptr &root = nullptr, int deep = -1); virtual std::shared_ptr createChannel(ChannelId parentId, ChannelId orderId, const std::string &name); virtual std::deque> delete_channel_root(const std::shared_ptr &); inline bool change_order(const std::shared_ptr& channel, ChannelId channelId) { return this->move_entry(channel, channel->parent(), this->find_entry(channelId)); } inline bool move_channel(const std::shared_ptr& channel, const std::shared_ptr& parent, const std::shared_ptr& order) { return this->move_entry(channel, parent, order); } bool setDefaultChannel(const std::shared_ptr &); std::shared_ptr getDefaultChannel(); void printChannelTree(std::function = [](std::string msg) { std::cout << msg << std::endl; }); protected: virtual ChannelId generateChannelId(); virtual void on_channel_entry_deleted(const std::shared_ptr &); virtual std::shared_ptr allocateChannel(const std::shared_ptr &parent, ChannelId channelId); }; } DEFINE_TRANSFORMS(ts::ChannelType::ChannelType, uint8_t);