Updating the conversation stuff

This commit is contained in:
WolverinDEV 2019-07-21 17:02:32 +02:00
parent 345da8394b
commit c720a80bc0
5 changed files with 55 additions and 11 deletions

View File

@ -227,7 +227,7 @@ bool TSServer::initialize(bool test_properties) {
this->properties()[property::VIRTUALSERVER_FILEBASE] = serverInstance->getFileServer()->server_file_base(self.lock()); this->properties()[property::VIRTUALSERVER_FILEBASE] = serverInstance->getFileServer()->server_file_base(self.lock());
/* lets cleanup the conversations for not existent channels */ /* lets cleanup the conversations for not existent channels */
this->_conversation_manager->cleanup_channels(); this->_conversation_manager->synchronize_channels();
return true; return true;
} }

View File

@ -1395,6 +1395,15 @@ CommandResult ConnectedClient::handleCommandChannelCreate(Command &cmd) {
auto& tree_lock = this->server ? this->server->channel_tree_lock : serverInstance->getChannelTreeLock(); auto& tree_lock = this->server ? this->server->channel_tree_lock : serverInstance->getChannelTreeLock();
unique_lock tree_channel_lock(tree_lock); unique_lock tree_channel_lock(tree_lock);
if(cmd[0].has("channel_conversation_history_length")) {
auto value = cmd["channel_conversation_history_length"].as<int64_t>();
if(value == 0) {
CACHED_PERM_CHECK(permission::b_channel_create_modify_conversation_history_unlimited, 1);
} else {
CACHED_PERM_CHECK(permission::i_channel_create_modify_conversation_history_length, 1);
}
}
{ {
auto delete_delay = cmd[0].has("channel_delete_delay") ? cmd["channel_delete_delay"].as<permission::PermissionValue>() : 0UL; auto delete_delay = cmd[0].has("channel_delete_delay") ? cmd["channel_delete_delay"].as<permission::PermissionValue>() : 0UL;
if(delete_delay == 0) { if(delete_delay == 0) {
@ -1785,6 +1794,13 @@ CommandResult ConnectedClient::handleCommandChannelEdit(Command &cmd) {
} else if (key == "channel_password" || key == "channel_flag_password") { } else if (key == "channel_password" || key == "channel_flag_password") {
CHANNEL_PERM_TEST(permission::b_channel_modify_password, 1, true); CHANNEL_PERM_TEST(permission::b_channel_modify_password, 1, true);
update_password = true; update_password = true;
} else if (key == "channel_conversation_history_length") {
auto value = cmd["channel_conversation_history_length"].as<int64_t>();
if(value == 0) {
CHANNEL_PERM_TEST(permission::b_channel_create_modify_conversation_history_unlimited, 1, true);
} else {
CHANNEL_PERM_TEST(permission::i_channel_create_modify_conversation_history_length, 1, true);
}
} else { } else {
logCritical( logCritical(
this->getServerId(), this->getServerId(),
@ -2066,6 +2082,14 @@ CommandResult ConnectedClient::handleCommandChannelEdit(Command &cmd) {
} }
} }
} }
} else if(*key == property::CHANNEL_CONVERSATION_HISTORY_LENGTH) {
//channel_conversation_history_length
auto conversation_manager = this->server->conversation_manager();
if(conversation_manager) {
auto conversation = conversation_manager->get(channel->channelId());
if(conversation)
conversation->set_history_length(cmd[key->name]);
}
} }
channel->properties()[key] = cmd[key->name].string(); channel->properties()[key] = cmd[key->name].string();
@ -7314,8 +7338,10 @@ CommandResult ConnectedClient::handleCommandConversationHistory(ts::Command &com
size_t length = 0; size_t length = 0;
bool merge = command.hasParm("merge"); bool merge = command.hasParm("merge");
for(auto& message : messages) { for(auto& message : messages) {
if(index == 0) if(index == 0) {
notify[index]["cid"] = conversation_id; notify[index]["cid"] = conversation_id;
notify[index]["flag_volatile"] = conversation->volatile_only();
}
notify[index]["timestamp"] = duration_cast<milliseconds>(message->message_timestamp.time_since_epoch()).count(); notify[index]["timestamp"] = duration_cast<milliseconds>(message->message_timestamp.time_since_epoch()).count();
notify[index]["sender_database_id"] = message->sender_database_id; notify[index]["sender_database_id"] = message->sender_database_id;
@ -7415,6 +7441,7 @@ CommandResult ConnectedClient::handleCommandConversationFetch(ts::Command &cmd)
result_bulk["timestamp"] = duration_cast<milliseconds>(conversation->last_message().time_since_epoch()).count(); result_bulk["timestamp"] = duration_cast<milliseconds>(conversation->last_message().time_since_epoch()).count();
else else
result_bulk["timestamp"] = 0; result_bulk["timestamp"] = 0;
result_bulk["flag_volatile"] = conversation->volatile_only();
} }
if(result_index == 0) if(result_index == 0)
return {ErrorType::DBEmpty}; return {ErrorType::DBEmpty};

View File

@ -16,10 +16,8 @@ using namespace std::chrono;
using namespace ts; using namespace ts;
using namespace ts::server; using namespace ts::server;
extern ts::server::InstanceHandler* serverInstance;
constexpr unsigned int string_hash(const char* str, int h = 0) { constexpr unsigned int string_hash(const char* str, int h = 0) {
return !str[h] ? 5381 : (string_hash(str, h + 1) * 33) ^ str[h]; return !str[h] ? 5381 : (string_hash(str, h + 1U) * 33U) ^ str[h];
} }
CommandResult QueryClient::handleCommand(Command& cmd) { CommandResult QueryClient::handleCommand(Command& cmd) {

View File

@ -1253,8 +1253,26 @@ void ConversationManager::delete_conversation(ts::ChannelId channel_id) {
} }
} }
void ConversationManager::cleanup_channels() { void ConversationManager::synchronize_channels() {
//TODO: Check if the channel for the conversation still exists! auto ref_server = this->ref_server();
if(!ref_server) {
logWarning(ref_server->getServerId(), "[Conversations][{}] Failed to synchronize conversations (server expired)");
return;
}
auto chan_tree = ref_server->getChannelTree();
deque<shared_ptr<Conversation>> channels{this->_conversations};
for(auto& channel : channels) {
auto schannel = chan_tree->findChannel(channel->channel_id());
if(!schannel) {
logMessage(ref_server->getServerId(), "[Conversations][{}] Deleting conversation because channel does not exists anymore.", channel->channel_id());
this->delete_conversation(channel->channel_id());
continue;
}
auto history_size = schannel->properties()[property::CHANNEL_CONVERSATION_HISTORY_LENGTH].as<size_t>();
channel->set_history_length(history_size);
}
} }
void ConversationManager::cleanup_cache() { void ConversationManager::cleanup_cache() {

View File

@ -143,11 +143,11 @@ namespace ts {
inline ChannelId channel_id() { return this->_channel_id; } inline ChannelId channel_id() { return this->_channel_id; }
/* if for some reason we're not able to open the file then we're in volatile mode */ /* if for some reason we're not able to open the file then we're in volatile mode */
inline bool volatile_only() { return this->_volatile; } inline bool volatile_only() { return this->_volatile || this->_history_length < 0; }
void cleanup_cache(); void cleanup_cache();
//void set_history_length(ssize_t /* save length */); void set_history_length(ssize_t length) { this->_history_length = length; }
//ssize_t history_length(); ssize_t history_length() { return this->_history_length; }
inline std::chrono::system_clock::time_point last_message() { return this->_last_message_timestamp; } inline std::chrono::system_clock::time_point last_message() { return this->_last_message_timestamp; }
void register_message(ClientDbId sender_database_id, const std::string& sender_unique_id, const std::string& sender_name, const std::string& message); void register_message(ClientDbId sender_database_id, const std::string& sender_unique_id, const std::string& sender_name, const std::string& message);
@ -210,6 +210,7 @@ namespace ts {
FILE* file_handle = nullptr; FILE* file_handle = nullptr;
ChannelId _channel_id; ChannelId _channel_id;
ssize_t _history_length = 0;
bool _volatile = false; bool _volatile = false;
std::chrono::system_clock::time_point _last_message_timestamp; std::chrono::system_clock::time_point _last_message_timestamp;
@ -221,7 +222,7 @@ namespace ts {
virtual ~ConversationManager(); virtual ~ConversationManager();
void initialize(const std::shared_ptr<ConversationManager>& _this); void initialize(const std::shared_ptr<ConversationManager>& _this);
void cleanup_channels(); void synchronize_channels();
void cleanup_cache(); void cleanup_cache();
bool conversation_exists(ChannelId /* channel */); bool conversation_exists(ChannelId /* channel */);