Page MenuHomePhorge

power-levels-desc.cpp
No OneTemporary

Size
2 KB
Referenced Files
None
Subscribers
None

power-levels-desc.cpp

/*
* This file is part of libkazv.
* SPDX-FileCopyrightText: 2020-2023 tusooa <tusooa@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <libkazv-config.hpp>
#include <event.hpp>
#include "validator.hpp"
#include "power-levels-desc.hpp"
namespace Kazv
{
static std::pair<bool, json> numericValidator(const json &j)
{
if (j.is_number()) {
return {true, json(j.template get<long long>())};
} else if (j.is_string()) {
auto str = j.template get<std::string>();
try {
std::size_t pos = 0;
auto num = std::stoll(str, &pos);
if (pos != str.size()) { // not entirely a number
return {false, json()};
}
return {true, json(num)};
} catch (const std::logic_error &) {
return {false, json()};
}
} else {
return {false, json()};
}
}
// https://spec.matrix.org/v1.8/client-server-api/#mroompower_levels
static const auto numericFields = immer::map<std::string, long long>{
{"ban", 50},
{"events_default", 0},
{"invite", 0},
{"kick", 50},
{"redact", 50},
{"state_default", 50},
{"users_default", 0},
};
struct PowerLevelsDesc::Private
{
Private(const Event &e)
: original(e)
, normalized(normalize(e))
{}
Event normalize(const Event &e)
{
auto content = e.content().get();
auto ret = json{
{"type", "m.power_levels"},
{"state_key", ""},
{"content", json::object()},
};
// cast numeric fields
for (const auto &[key, defaultValue] : numericFields) {
ret["content"][key] = defaultValue;
cast(ret["content"], content, key, numericValidator);
}
// cast events map
cast(ret["content"], content, "events", identValidate(&json::is_object));
// cast notifications map
cast(ret["content"], content, "notifications", identValidate(&json::is_object));
// cast users map
cast(ret["content"], content, "users", identValidate(&json::is_object));
return Event(ret);
}
Event original;
Event normalized;
};
PowerLevelsDesc::PowerLevelsDesc(const Event &e)
: m_d(new Private(e))
{}
PowerLevelsDesc::~PowerLevelsDesc() = default;
KAZV_DEFINE_COPYABLE_UNIQUE_PTR(PowerLevelsDesc, m_d);
bool PowerLevelsDesc::canSendMessage(std::string userId, std::string eventType) const
{
return true;
}
bool PowerLevelsDesc::canSendState(std::string userId, std::string eventType) const
{
return true;
}
Event PowerLevelsDesc::normalizedEvent() const
{
return m_d ? m_d->normalized : Event();
}
}

File Metadata

Mime Type
text/x-c++
Expires
Thu, Oct 2, 2:17 AM (19 h, 3 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
486911
Default Alt Text
power-levels-desc.cpp (2 KB)

Event Timeline