Fixed server group client list

This commit is contained in:
WolverinDEV 2020-08-01 16:02:03 +02:00
parent 8c3d756842
commit e49b091b92
5 changed files with 62 additions and 35 deletions

View File

@ -618,41 +618,65 @@ bool GroupManager::isClientCached(const ClientDbId& client_database_id) {
return this->resolve_cached_client(client_database_id) == nullptr;
}
constexpr static auto kGroupMemberListQuery{R"(
SELECT
assignedGroups.cldbid,
clients_server.client_unique_id,
clients_server.client_nickname,
assignedGroups.channelId,
assignedGroups.until
FROM assignedGroups
INNER JOIN clients_server ON
clients_server.client_database_id = assignedGroups.cldbid AND clients_server.server_id = :sid
WHERE assignedGroups.`serverId` = :sid AND `groupId` = :gid;
)"};
typedef std::vector<std::shared_ptr<GroupMember>> ResList;
std::vector<std::shared_ptr<GroupMember>> GroupManager::listGroupMembers(std::shared_ptr<Group> group, bool names) { //TODO juse inner join only on names = true
std::deque<GroupMember> GroupManager::listGroupMembers(std::shared_ptr<Group> group, bool names) {
if(!isLocalGroup(group)){
if(this->root) return this->root->listGroupMembers(group, names);
if(this->root)
return this->root->listGroupMembers(group, names);
return {};
}
ResList result;
sql::command(this->sql,
"SELECT assignedGroups.cldbid, assignedGroups.channelId, assignedGroups.until, clients.clientUid, clients.lastName FROM assignedGroups INNER JOIN clients ON (clients.cldbid = assignedGroups.cldbid AND clients.serverId = assignedGroups.serverId) WHERE assignedGroups.`serverId` = :sid AND `groupId` = :gid;",
variable{":sid", this->getServerId()}, variable{":gid", group->groupId()})
.query([&](ResList* list, int columnCount, char** values, char** columnName){
std::shared_ptr<GroupMember> member = std::make_shared<GroupMember>();
member->displayName = "undefined";
member->uid = "undefined";
for(int index = 0; index < columnCount; index++){
if(values[index] == nullptr) {
logError(this->getServerId(), string() + "Invalid value at " + columnName[index]);
continue;
}
if(strcmp(columnName[index], "cldbid") == 0)
member->cldbId = stoll(values[index]);
else if(strcmp(columnName[index], "until") == 0)
member->until = time_point<system_clock>() + milliseconds(stoll(values[index]));
else if(strcmp(columnName[index], "clientUid") == 0)
member->uid = values[index];
else if(strcmp(columnName[index], "lastName") == 0)
member->displayName = values[index];
else if(strcmp(columnName[index], "channelId") == 0)
member->channelId = stoll(values[index]);
else cerr << "Invalid column name " << columnName[index] << endl;
std::deque<GroupMember> result{};
size_t set_index{0};
sql::command{this->sql, std::string{kGroupMemberListQuery}, variable{":sid", this->getServerId()}, variable{":gid", group->groupId()}}
.query([&](int length, std::string* values, std::string* names) {
set_index++;
auto index{0};
try {
auto& member = result.emplace_back();
assert(names[index] == "cldbid");
member.cldbId = std::stoull(values[index++]);
assert(names[index] == "client_unique_id");
member.uid = values[index++];
assert(names[index] == "client_nickname");
member.displayName = values[index++];
assert(names[index] == "channelId");
member.channelId = std::stoull(values[index++]);
assert(names[index] == "until");
member.until = std::chrono::system_clock::time_point{} + std::chrono::milliseconds{std::stoll(values[index++])};
assert(index == length);
} catch (std::exception& ex) {
result.pop_back();
logError(this->getServerId(), "Failed to parse client group assignment for group {}: {}. Set index: {}, Column: {}",
group->groupId(),
ex.what(),
set_index - 1,
index - 1
);
return;
}
list->push_back(member);
return 0;
}, &result);
});
return result;
}

View File

@ -198,7 +198,7 @@ namespace ts {
bool renameGroup(std::shared_ptr<Group>, std::string);
bool deleteGroup(std::shared_ptr<Group>);
bool deleteAllGroups();
std::vector<std::shared_ptr<GroupMember>> listGroupMembers(std::shared_ptr<Group>, bool names = false);
std::deque<GroupMember> listGroupMembers(std::shared_ptr<Group>, bool names = false);
std::vector<std::shared_ptr<GroupAssignment>> listGroupAssignments(ClientDbId client);
void cleanupAssignments(ClientDbId);

View File

@ -578,11 +578,14 @@ command_result ConnectedClient::handleCommandServerGroupClientList(Command &cmd)
notify["sgid"] = cmd["sgid"].as<GroupId>();
int index = 0;
for (const auto &clientEntry : groupManager->listGroupMembers(serverGroup)) {
notify[index]["cldbid"] = clientEntry->cldbId;
notify[index]["client_nickname"] = clientEntry->displayName;
notify[index]["client_unique_identifier"] = clientEntry->uid;
notify[index]["cldbid"] = clientEntry.cldbId;
notify[index]["client_nickname"] = clientEntry.displayName;
notify[index]["client_unique_identifier"] = clientEntry.uid;
index++;
}
if(index == 0)
return ts::command_result{error::database_empty_result};
this->sendCommand(notify);
return command_result{error::ok};
}

View File

@ -133,7 +133,7 @@ CryptSetupHandler::CommandResult CryptSetupHandler::handleCommandClientInitIv(co
std::string clientOmega = base64::decode(cmd.value("omega")); //The identity public key
std::string ip = cmd.value("ip");
bool ot = cmd.has_key("ot") ? cmd.value_as<bool>("ot") : false;
bool ot = cmd.has_key("ot") && cmd.value_as<bool>("ot");
{
this->remote_key = std::shared_ptr<ecc_key>(new ecc_key{}, [](ecc_key* key){

View File

@ -179,7 +179,7 @@ std::shared_ptr<BanRecord> BanManager::findBanExact(ts::ServerId server_id, cons
}
std::deque<std::shared_ptr<BanRecord>> BanManager::listBans(ServerId sid) {
auto command = sql::command(this->sql, "SELECT `bannedClients`.*, clients.`clientUid` AS `invUid`, clients.`lastName` AS `invName` FROM `bannedClients` INNER JOIN clients ON clients.cldbid = bannedClients.invokerDbId AND clients.serverId = bannedClients.serverId WHERE bannedClients.`serverId` = :sid" UNTIL_SQL,
auto command = sql::command(this->sql, "SELECT `bannedClients`.*, clients_server.`client_unique_id` AS `invUid`, clients_server.`client_nickname` AS `invName` FROM `bannedClients` INNER JOIN clients_server ON clients_server.client_database_id = bannedClients.invokerDbId AND clients_server.server_id = :sid WHERE bannedClients.`serverId` = :sid" UNTIL_SQL,
variable{":time", duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count()},
variable{":sid", sid});
return resolveBansByQuery(command);