TeaSpeakLibrary/src/BasicChannel.h

143 lines
6.7 KiB
C
Raw Normal View History

2019-06-26 16:11:22 -04:00
#pragma once
#include <cstdint>
#include <cstdlib>
#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<BasicChannel> current = nullptr;
BasicChannelEntry* next{};
BasicChannelEntry* children{};
2020-01-23 20:49:59 -05:00
~BasicChannelEntry() {
this->current = nullptr;
}
2019-06-26 16:11:22 -04:00
};
2020-01-23 20:49:59 -05:00
class BasicChannel : public TreeEntry {
2019-06-26 16:11:22 -04:00
public:
BasicChannel(ChannelId parentId, ChannelId channelId);
BasicChannel(std::shared_ptr<BasicChannel> parent, ChannelId channelId);
virtual ~BasicChannel();
bool hasParent(){ return !!this->parent(); }
std::shared_ptr<BasicChannel> 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]; }
2019-09-22 10:57:01 -04:00
int64_t emptySince();
2019-06-26 16:11:22 -04:00
2020-01-23 20:49:59 -05:00
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<int64_t>());
}
ts_always_inline bool permission_require_property_update(const permission::PermissionType& permission) {
return permission == permission::i_icon_id || permission == permission::i_client_needed_talk_power;
}
std::vector<property::ChannelProperties> update_properties_from_permissions();
inline bool permission_granted(const permission::PermissionType& permission, const permission::v2::PermissionFlaggedValue& granted_value, bool require_granted_value) {
auto permission_manager = this->permissions(); /* copy the manager */
assert(permission_manager);
const auto data = permission_manager->permission_value_flagged(permission);
return BasicChannel::permission_granted(data,granted_value, require_granted_value);
}
ts_always_inline bool talk_power_granted(const permission::v2::PermissionFlaggedValue& granted_value) {
return this->permission_granted(permission::i_client_needed_talk_power, granted_value, false);
}
ts_always_inline std::shared_ptr<permission::v2::PermissionManager> permissions(){ return this->_permissions; }
virtual void setPermissionManager(const std::shared_ptr<permission::v2::PermissionManager>&);
virtual void setProperties(const std::shared_ptr<Properties>&);
private:
2020-01-23 20:49:59 -05:00
ts_always_inline
static bool permission_granted(const permission::v2::PermissionFlaggedValue& channel_permission_value, const permission::v2::PermissionFlaggedValue& granted_value, bool require_granted_value) {
if(!channel_permission_value.has_value || channel_permission_value.value == 0) {
return !require_granted_value || granted_value.has_value;
}
if(channel_permission_value.value == -1) {
return granted_value.value == -1;
}
return granted_value.value >= channel_permission_value.value;
}
2019-06-26 16:11:22 -04:00
public:
2020-02-28 05:24:02 -05:00
[[nodiscard]] ChannelId channelId() const override;
[[nodiscard]] ChannelId previousChannelId() const override;
2019-06-26 16:11:22 -04:00
void setPreviousChannelId(ChannelId id) override;
void setParentChannelId(ChannelId id) override;
void setLinkedHandle(const std::weak_ptr<TreeView::LinkedTreeEntry> &) override;
2020-01-23 20:49:59 -05:00
protected:
2019-06-26 16:11:22 -04:00
std::weak_ptr<TreeView::LinkedTreeEntry> _link;
std::shared_ptr<Properties> _properties;
2019-07-17 12:24:43 -04:00
std::shared_ptr<permission::v2::PermissionManager> _permissions;
2019-06-26 16:11:22 -04:00
2020-01-23 20:49:59 -05:00
ChannelId _channel_order = 0;
ChannelId _channel_id = 0;
2019-06-26 16:11:22 -04:00
};
class BasicChannelTree : public TreeView {
public:
BasicChannelTree();
virtual ~BasicChannelTree();
size_t channel_count() { return this->entry_count(); }
2020-01-23 20:49:59 -05:00
std::shared_ptr<LinkedTreeEntry> findLinkedChannel(ChannelId channelId);
2019-06-26 16:11:22 -04:00
std::shared_ptr<BasicChannel> findChannel(ChannelId channelId);
2020-01-23 20:49:59 -05:00
std::shared_ptr<BasicChannel> findChannel(ChannelId channelId, std::deque<std::shared_ptr<BasicChannel>> avariable);
2019-06-26 16:11:22 -04:00
std::shared_ptr<BasicChannel> findChannel(const std::string &name, const std::shared_ptr<BasicChannel> &layer);
std::shared_ptr<BasicChannel> findChannelByPath(const std::string& path);
//std::deque<std::shared_ptr<BasicChannel>> topChannels();
std::deque<std::shared_ptr<BasicChannel>> channels(const std::shared_ptr<BasicChannel> &root = nullptr, int deep = -1);
virtual std::shared_ptr<BasicChannel> createChannel(ChannelId parentId, ChannelId orderId, const std::string &name);
virtual std::deque<std::shared_ptr<ts::BasicChannel>> delete_channel_root(const std::shared_ptr<BasicChannel> &);
inline bool change_order(const std::shared_ptr<BasicChannel>& channel, ChannelId channelId) {
return this->move_entry(channel, channel->parent(), this->find_entry(channelId));
}
inline bool move_channel(const std::shared_ptr<BasicChannel>& channel, const std::shared_ptr<BasicChannel>& parent, const std::shared_ptr<BasicChannel>& order) {
return this->move_entry(channel, parent, order);
}
bool setDefaultChannel(const std::shared_ptr<BasicChannel> &);
std::shared_ptr<BasicChannel> getDefaultChannel();
void printChannelTree(std::function<void(std::string)> = [](std::string msg) { std::cout << msg << std::endl; });
protected:
virtual ChannelId generateChannelId();
virtual void on_channel_entry_deleted(const std::shared_ptr<BasicChannel> &);
virtual std::shared_ptr<BasicChannel> allocateChannel(const std::shared_ptr<BasicChannel> &parent, ChannelId channelId);
};
}
DEFINE_TRANSFORMS(ts::ChannelType::ChannelType, uint8_t);