refactor(eventsub): conditionally use Howard's date lib (#6048)

Co-authored-by: Nerixyz <nerixdev@outlook.de>
This commit is contained in:
pajlada
2025-03-09 13:52:16 +01:00
committed by GitHub
parent c98267b433
commit 81ef344fad
11 changed files with 91 additions and 8246 deletions
+3
View File
@@ -52,3 +52,6 @@
[submodule "lib/certify"]
path = lib/certify
url = https://github.com/Chatterino/certify
[submodule "lib/twitch-eventsub-ws/lib/date"]
path = lib/twitch-eventsub-ws/lib/date
url = https://github.com/HowardHinnant/date.git
+1 -1
View File
@@ -41,7 +41,7 @@
- Bugfix: Fixed an issue where commands would sometimes reset if Chatterino was improperly shut down. (#6011)
- Bugfix: Fixed a thick border on Windows 11. (#5836)
- Dev: Subscriptions to PubSub channel points redemption topics now use no auth token, making it continue to work during PubSub shutdown. (#5947)
- Dev: Add initial experimental EventSub support. (#5837, #5895, #5897, #5904, #5910, #5903, #5915, #5916, #5930, #5935, #5932, #5943, #5952, #5953, #5968, #5973, #5974, #5980, #5981, #5985, #5990, #5992, #5993, #5996, #5995, #6000, #6001, #6002, #6003, #6005, #6007, #6010, #6008, #6012, #6013, #6015, #6017, #6027, #6028, #6035, #6036, #6040, #6041)
- Dev: Add initial experimental EventSub support. (#5837, #5895, #5897, #5904, #5910, #5903, #5915, #5916, #5930, #5935, #5932, #5943, #5952, #5953, #5968, #5973, #5974, #5980, #5981, #5985, #5990, #5992, #5993, #5996, #5995, #6000, #6001, #6002, #6003, #6005, #6007, #6010, #6008, #6012, #6013, #6015, #6017, #6027, #6028, #6035, #6036, #6040, #6041, #6048)
- Dev: Remove unneeded platform specifier for toasts. (#5914)
- Dev: Highlight checks now use non-capturing groups for the boundaries. (#5784)
- Dev: Removed unused PubSub whisper code. (#5898)
@@ -3,7 +3,6 @@
#include <boost/json.hpp>
#include <chrono>
#include <sstream>
namespace chatterino::eventsub::lib {
File diff suppressed because it is too large Load Diff
@@ -15,6 +15,7 @@ enum class Kind : int {
InnerRootMissing,
NoMessageHandler,
UnknownVariant,
BadTimeFormat,
};
class ApplicationErrorCategory final : public boost::system::error_category
+6 -2
View File
@@ -48,8 +48,12 @@ add_library(${PROJECT_NAME} STATIC ${SOURCE_FILES})
# Generate source groups for use in IDEs
# source_group(TREE ${CMAKE_SOURCE_DIR} FILES ${SOURCE_FILES})
target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../include")
target_include_directories(${PROJECT_NAME}
PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/../include"
PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}/../lib/date/include"
)
target_link_libraries(${PROJECT_NAME}
PUBLIC
+22 -8
View File
@@ -1,16 +1,22 @@
#include "twitch-eventsub-ws/chrono.hpp"
#include "twitch-eventsub-ws/date.h"
#include "twitch-eventsub-ws/detail/errors.hpp"
#include <sstream>
#if __cpp_lib_chrono >= 201907L
# include <chrono>
using namespace std::chrono;
#else
# include <date/date.h>
using namespace date;
#endif
namespace chatterino::eventsub::lib {
boost::json::result_for<std::chrono::system_clock::time_point,
boost::json::value>::type
tag_invoke(
boost::json::try_value_to_tag<std::chrono::system_clock::time_point>,
const boost::json::value &jvRoot, const AsISO8601 &)
tag_invoke(boost::json::try_value_to_tag<
std::chrono::system_clock::time_point> /*tag*/,
const boost::json::value &jvRoot, const AsISO8601 & /*tag*/)
{
const auto raw = boost::json::try_value_to<std::string>(jvRoot);
if (raw.has_error())
@@ -18,10 +24,18 @@ boost::json::result_for<std::chrono::system_clock::time_point,
return raw.error();
}
std::chrono::system_clock::time_point tp;
std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>
tp;
std::istringstream in{*raw};
in >> date::parse("%FT%H:%M:%12SZ", tp);
in >> parse("%FT%H:%M:%12SZ", tp);
return tp;
if (!in.good())
{
EVENTSUB_BAIL_HERE(error::Kind::BadTimeFormat);
}
return std::chrono::time_point_cast<std::chrono::system_clock::duration>(
tp);
}
} // namespace chatterino::eventsub::lib
+2
View File
@@ -27,6 +27,8 @@ std::string ApplicationErrorCategory::message(int ev) const
return "No message handler found"s;
case Kind::UnknownVariant:
return "Unknown variant value"s;
case Kind::BadTimeFormat:
return "Bad time format"s;
}
assert(false);
@@ -3,6 +3,7 @@ project(twitch-eventsub-ws-test)
set(SOURCES
src/parse.cpp
src/string.cpp
src/chrono.cpp
)
add_executable(${PROJECT_NAME} ${SOURCES})
@@ -0,0 +1,54 @@
#include "twitch-eventsub-ws/errors.hpp"
#include <gtest/gtest.h>
#include <twitch-eventsub-ws/chrono.hpp>
using namespace chatterino::eventsub;
namespace {
using namespace std::chrono;
using namespace std::chrono_literals;
constexpr auto MAY14 =
std::chrono::sys_days{May / 14 / 2024} + 12h + 31min + 47s;
constexpr auto MAY14_PRECISE = MAY14 + 123ms + 456us;
boost::json::result_for<std::chrono::system_clock::time_point,
boost::json::value>::type
buildTP(std::string_view sv)
{
auto xd = boost::json::string(sv);
return boost::json::try_value_to<std::chrono::system_clock::time_point>(
xd, lib::AsISO8601());
}
} // namespace
TEST(Chrono, Success)
{
{
auto oTp = buildTP("2024-05-14T12:31:47Z");
ASSERT_FALSE(oTp.has_error());
ASSERT_EQ(oTp.value(), MAY14);
}
{
std::chrono::system_clock::time_point tp;
auto oTp = buildTP("2024-05-14T12:31:47.123456789Z");
ASSERT_FALSE(oTp.has_error());
ASSERT_EQ(std::chrono::time_point_cast<std::chrono::microseconds>(
oTp.value()),
MAY14_PRECISE);
}
}
TEST(Chrono, Fail)
{
{
auto oTp = buildTP("2024-05-14T12:31:47.123456789123456789Z");
ASSERT_TRUE(oTp.has_error());
ASSERT_EQ(static_cast<lib::error::Kind>(oTp.error().value()),
lib::error::Kind::BadTimeFormat);
}
}