feat(eventsub): Add support for channel.moderate Warn (#5932)

* fix(eventsub): Be less strict with strings & vectors

String foo from `{"foo":null}` is now accepted, and returns an empty string
std::vector<String> foo from `{"foo":null}` is now accepted, and returns an empty array
This commit is contained in:
pajlada
2025-02-09 12:21:16 +01:00
committed by GitHub
parent 88341517dc
commit b01f50856d
11 changed files with 211 additions and 96 deletions
@@ -1,14 +1,14 @@
{% if field.tag -%}
static_assert(false && "JSON tag support is not implemented for vectors");
{%- endif %}
std::vector<{{field.type_name}}> v{{field.name}};
const auto *jv{{field.name}} = root.if_contains("{{field.json_name}}");
if (jv{{field.name}} == nullptr)
{
{% include 'error-missing-field.tmpl' indent content %}
if (jv{{field.name}} != nullptr && !jv{{field.name}}->is_null()) {
auto {{field.name}} = boost::json::try_value_to<std::vector<{{field.type_name}}>>(*jv{{field.name}});
if ({{field.name}}.has_error())
{
{% include 'error-failed-to-deserialize.tmpl' indent content %}
} else {
v{{field.name}} = std::move({{field.name}}.value());
}
}
auto {{field.name}} = boost::json::try_value_to<std::vector<{{field.type_name}}>>(*jv{{field.name}});
if ({{field.name}}.has_error())
{
{% include 'error-failed-to-deserialize.tmpl' indent content %}
}
@@ -1 +1 @@
.{{field.name}} = std::move({{field.name}}.value()),
.{{field.name}} = std::move(v{{field.name}}),
@@ -346,13 +346,13 @@ struct DenyUnbanRequest : public UnbanRequest {
struct Warn {
static constexpr std::string_view TAG = "warn";
std::string userID;
std::string userLogin;
std::string userName;
std::string reason;
String userID;
String userLogin;
String userName;
String reason;
// TODO: Verify we handle null for this as an empty vector
std::vector<std::string> chatRulesCited;
std::vector<String> chatRulesCited;
};
struct Clear {
@@ -175,23 +175,27 @@ boost::json::result_for<Emote, boost::json::value>::type tag_invoke(
return ownerID.error();
}
std::vector<std::string> vformat;
const auto *jvformat = root.if_contains("format");
if (jvformat == nullptr)
if (jvformat != nullptr && !jvformat->is_null())
{
EVENTSUB_BAIL_HERE(error::Kind::FieldMissing);
}
auto format =
boost::json::try_value_to<std::vector<std::string>>(*jvformat);
if (format.has_error())
{
return format.error();
auto format =
boost::json::try_value_to<std::vector<std::string>>(*jvformat);
if (format.has_error())
{
return format.error();
}
else
{
vformat = std::move(format.value());
}
}
return Emote{
.id = std::move(id.value()),
.emoteSetID = std::move(emoteSetID.value()),
.ownerID = std::move(ownerID.value()),
.format = std::move(format.value()),
.format = std::move(vformat),
};
}
@@ -358,21 +362,26 @@ boost::json::result_for<Message, boost::json::value>::type tag_invoke(
return text.error();
}
std::vector<MessageFragment> vfragments;
const auto *jvfragments = root.if_contains("fragments");
if (jvfragments == nullptr)
if (jvfragments != nullptr && !jvfragments->is_null())
{
EVENTSUB_BAIL_HERE(error::Kind::FieldMissing);
}
auto fragments =
boost::json::try_value_to<std::vector<MessageFragment>>(*jvfragments);
if (fragments.has_error())
{
return fragments.error();
auto fragments =
boost::json::try_value_to<std::vector<MessageFragment>>(
*jvfragments);
if (fragments.has_error())
{
return fragments.error();
}
else
{
vfragments = std::move(fragments.value());
}
}
return Message{
.text = std::move(text.value()),
.fragments = std::move(fragments.value()),
.fragments = std::move(vfragments),
};
}
@@ -663,17 +672,20 @@ boost::json::result_for<Event, boost::json::value>::type tag_invoke(
return color.error();
}
std::vector<Badge> vbadges;
const auto *jvbadges = root.if_contains("badges");
if (jvbadges == nullptr)
if (jvbadges != nullptr && !jvbadges->is_null())
{
EVENTSUB_BAIL_HERE(error::Kind::FieldMissing);
auto badges = boost::json::try_value_to<std::vector<Badge>>(*jvbadges);
if (badges.has_error())
{
return badges.error();
}
else
{
vbadges = std::move(badges.value());
}
}
auto badges = boost::json::try_value_to<std::vector<Badge>>(*jvbadges);
if (badges.has_error())
{
return badges.error();
}
const auto *jvmessageID = root.if_contains("message_id");
if (jvmessageID == nullptr)
{
@@ -768,7 +780,7 @@ boost::json::result_for<Event, boost::json::value>::type tag_invoke(
.chatterUserLogin = std::move(chatterUserLogin.value()),
.chatterUserName = std::move(chatterUserName.value()),
.color = std::move(color.value()),
.badges = std::move(badges.value()),
.badges = std::move(vbadges),
.messageID = std::move(messageID.value()),
.messageType = std::move(messageType.value()),
.message = std::move(message.value()),
@@ -175,23 +175,27 @@ boost::json::result_for<Emote, boost::json::value>::type tag_invoke(
return ownerID.error();
}
std::vector<std::string> vformat;
const auto *jvformat = root.if_contains("format");
if (jvformat == nullptr)
if (jvformat != nullptr && !jvformat->is_null())
{
EVENTSUB_BAIL_HERE(error::Kind::FieldMissing);
}
auto format =
boost::json::try_value_to<std::vector<std::string>>(*jvformat);
if (format.has_error())
{
return format.error();
auto format =
boost::json::try_value_to<std::vector<std::string>>(*jvformat);
if (format.has_error())
{
return format.error();
}
else
{
vformat = std::move(format.value());
}
}
return Emote{
.id = std::move(id.value()),
.emoteSetID = std::move(emoteSetID.value()),
.ownerID = std::move(ownerID.value()),
.format = std::move(format.value()),
.format = std::move(vformat),
};
}
@@ -1246,21 +1250,26 @@ boost::json::result_for<Message, boost::json::value>::type tag_invoke(
return text.error();
}
std::vector<MessageFragment> vfragments;
const auto *jvfragments = root.if_contains("fragments");
if (jvfragments == nullptr)
if (jvfragments != nullptr && !jvfragments->is_null())
{
EVENTSUB_BAIL_HERE(error::Kind::FieldMissing);
}
auto fragments =
boost::json::try_value_to<std::vector<MessageFragment>>(*jvfragments);
if (fragments.has_error())
{
return fragments.error();
auto fragments =
boost::json::try_value_to<std::vector<MessageFragment>>(
*jvfragments);
if (fragments.has_error())
{
return fragments.error();
}
else
{
vfragments = std::move(fragments.value());
}
}
return Message{
.text = std::move(text.value()),
.fragments = std::move(fragments.value()),
.fragments = std::move(vfragments),
};
}
@@ -1389,17 +1398,20 @@ boost::json::result_for<Event, boost::json::value>::type tag_invoke(
return color.error();
}
std::vector<Badge> vbadges;
const auto *jvbadges = root.if_contains("badges");
if (jvbadges == nullptr)
if (jvbadges != nullptr && !jvbadges->is_null())
{
EVENTSUB_BAIL_HERE(error::Kind::FieldMissing);
auto badges = boost::json::try_value_to<std::vector<Badge>>(*jvbadges);
if (badges.has_error())
{
return badges.error();
}
else
{
vbadges = std::move(badges.value());
}
}
auto badges = boost::json::try_value_to<std::vector<Badge>>(*jvbadges);
if (badges.has_error())
{
return badges.error();
}
const auto *jvsystemMessage = root.if_contains("system_message");
if (jvsystemMessage == nullptr)
{
@@ -1649,7 +1661,7 @@ boost::json::result_for<Event, boost::json::value>::type tag_invoke(
.chatterUserName = std::move(chatterUserName.value()),
.chatterIsAnonymous = chatterIsAnonymous.value(),
.color = std::move(color.value()),
.badges = std::move(badges.value()),
.badges = std::move(vbadges),
.systemMessage = std::move(systemMessage.value()),
.messageID = std::move(messageID.value()),
.message = std::move(message.value()),
@@ -858,17 +858,21 @@ boost::json::result_for<AutomodTerms, boost::json::value>::type tag_invoke(
return list.error();
}
std::vector<std::string> vterms;
const auto *jvterms = root.if_contains("terms");
if (jvterms == nullptr)
if (jvterms != nullptr && !jvterms->is_null())
{
EVENTSUB_BAIL_HERE(error::Kind::FieldMissing);
auto terms =
boost::json::try_value_to<std::vector<std::string>>(*jvterms);
if (terms.has_error())
{
return terms.error();
}
else
{
vterms = std::move(terms.value());
}
}
auto terms = boost::json::try_value_to<std::vector<std::string>>(*jvterms);
if (terms.has_error())
{
return terms.error();
}
static_assert(std::is_trivially_copyable_v<std::remove_reference_t<
decltype(std::declval<AutomodTerms>().fromAutomod)>>);
const auto *jvfromAutomod = root.if_contains("from_automod");
@@ -887,7 +891,7 @@ boost::json::result_for<AutomodTerms, boost::json::value>::type tag_invoke(
return AutomodTerms{
.action = std::move(action.value()),
.list = std::move(list.value()),
.terms = std::move(terms.value()),
.terms = std::move(vterms),
.fromAutomod = fromAutomod.value(),
};
}
@@ -1037,7 +1041,7 @@ boost::json::result_for<Warn, boost::json::value>::type tag_invoke(
EVENTSUB_BAIL_HERE(error::Kind::FieldMissing);
}
auto userID = boost::json::try_value_to<std::string>(*jvuserID);
auto userID = boost::json::try_value_to<String>(*jvuserID);
if (userID.has_error())
{
@@ -1050,7 +1054,7 @@ boost::json::result_for<Warn, boost::json::value>::type tag_invoke(
EVENTSUB_BAIL_HERE(error::Kind::FieldMissing);
}
auto userLogin = boost::json::try_value_to<std::string>(*jvuserLogin);
auto userLogin = boost::json::try_value_to<String>(*jvuserLogin);
if (userLogin.has_error())
{
@@ -1063,7 +1067,7 @@ boost::json::result_for<Warn, boost::json::value>::type tag_invoke(
EVENTSUB_BAIL_HERE(error::Kind::FieldMissing);
}
auto userName = boost::json::try_value_to<std::string>(*jvuserName);
auto userName = boost::json::try_value_to<String>(*jvuserName);
if (userName.has_error())
{
@@ -1076,23 +1080,27 @@ boost::json::result_for<Warn, boost::json::value>::type tag_invoke(
EVENTSUB_BAIL_HERE(error::Kind::FieldMissing);
}
auto reason = boost::json::try_value_to<std::string>(*jvreason);
auto reason = boost::json::try_value_to<String>(*jvreason);
if (reason.has_error())
{
return reason.error();
}
std::vector<chatterino::eventsub::lib::String> vchatRulesCited;
const auto *jvchatRulesCited = root.if_contains("chat_rules_cited");
if (jvchatRulesCited == nullptr)
if (jvchatRulesCited != nullptr && !jvchatRulesCited->is_null())
{
EVENTSUB_BAIL_HERE(error::Kind::FieldMissing);
}
auto chatRulesCited =
boost::json::try_value_to<std::vector<std::string>>(*jvchatRulesCited);
if (chatRulesCited.has_error())
{
return chatRulesCited.error();
auto chatRulesCited = boost::json::try_value_to<
std::vector<chatterino::eventsub::lib::String>>(*jvchatRulesCited);
if (chatRulesCited.has_error())
{
return chatRulesCited.error();
}
else
{
vchatRulesCited = std::move(chatRulesCited.value());
}
}
return Warn{
@@ -1100,7 +1108,7 @@ boost::json::result_for<Warn, boost::json::value>::type tag_invoke(
.userLogin = std::move(userLogin.value()),
.userName = std::move(userName.value()),
.reason = std::move(reason.value()),
.chatRulesCited = std::move(chatRulesCited.value()),
.chatRulesCited = std::move(vchatRulesCited),
};
}
+6
View File
@@ -11,6 +11,12 @@ boost::json::result_for<String, boost::json::value>::type tag_invoke(
boost::json::try_value_to_tag<String> /*tag*/,
const boost::json::value &jvRoot)
{
if (jvRoot.is_null())
{
// We treat null "strings" as empty strings
return String("");
}
auto v = boost::json::try_value_to<std::string>(jvRoot);
if (v.has_error())
{