From 02475e05ccd1aa15c6a6163834f28bfebbef796f Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Mon, 30 Nov 2026 11:47:36 +0100 Subject: RWGPS webhook and such --- server/src/rwgps.cpp | 168 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 134 insertions(+), 34 deletions(-) (limited to 'server/src/rwgps.cpp') diff --git a/server/src/rwgps.cpp b/server/src/rwgps.cpp index b8ebf11..21584a8 100644 --- a/server/src/rwgps.cpp +++ b/server/src/rwgps.cpp @@ -4,6 +4,8 @@ module; #include #include +#include + module routemon:rwgps$impl; import :rwgps; @@ -14,34 +16,6 @@ namespace json = boost::json; namespace routemon::rwgps { -export struct route_summary -{ - std::int64_t id; - std::int64_t user_id; - std::string url; - std::string name; - std::string description; -}; - -struct pagination -{ - std::size_t record_count; - std::size_t page_count; - std::size_t page_size; - std::optional next_page_url; -}; - -struct get_routes_meta -{ - pagination pagination; -}; - -struct get_routes_response -{ - std::vector routes; - get_routes_meta meta; -}; - auto tag_invoke(json::value_to_tag const&, json::value const& jv) -> route_summary { @@ -91,6 +65,133 @@ auto json_value_to_get_routes_response(json::value const& jv) return json::value_to(jv); } +static json::value const json_null = json::value{}; + +auto at_or_null(json::value const& v, std::string_view key) + -> json::value const& +{ + if (v.is_null()) + return json_null; + auto const* obj = v.if_object(); + if (!obj) + throw std::system_error{make_error_code(json::error::not_object)}; + if (auto it = obj->find(key); it != obj->end()) + return it->value(); + return json_null; +} + +auto tag_invoke( + json::value_to_tag const&, json::value const& jv) + -> notification_cause +{ + if (jv == "created") + return notification_cause::created; + if (jv == "updated") + return notification_cause::updated; + if (jv == "deleted") + return notification_cause::deleted; + if (jv == "added") + return notification_cause::added; + if (jv == "removed") + return notification_cause::removed; + return notification_cause::unknown; +} + +auto tag_invoke( + json::value_to_tag const&, json::value const& jv) + -> notification_item_type +{ + if (jv == "trip") + return notification_item_type::trip; + if (jv == "route") + return notification_item_type::route; + if (jv == "event") + return notification_item_type::event; + if (jv == "event_participant") + return notification_item_type::event_participant; + if (jv == "club_member") + return notification_item_type::club_member; + if (jv == "authorization") + return notification_item_type::authorization; + return notification_item_type::unknown; +} + +auto tag_invoke( + json::value_to_tag const&, + json::value const& jv) -> notification_target_collection_type +{ + if (jv == "pinned") + return notification_target_collection_type::pinned; + return notification_target_collection_type::unknown; +} + +auto tag_invoke( + json::value_to_tag const&, + json::value const& jv) -> notification_target_collection +{ + return { + .id = json::value_to(at_or_null(jv, "id")), + .type = json::value_to( + at_or_null(jv, "type")), + .url = json::value_to>(at_or_null(jv, "url")), + }; +} + +auto tag_invoke(json::value_to_tag const&, json::value const& jv) + -> notification +{ + return { + .user_id = json::value_to(at_or_null(jv, "user_id")), + .item_type = + json::value_to(at_or_null(jv, "item_type")), + .item_id = + json::value_to>(at_or_null(jv, "item_id")), + .item_user_id = json::value_to>( + at_or_null(jv, "item_user_id")), + .item_url = + json::value_to>(at_or_null(jv, "item_url")), + .action = json::value_to(at_or_null(jv, "action")), + .collection = json::value_to>( + at_or_null(jv, "collection")), + }; +} + +auto tag_invoke( + json::value_to_tag const&, json::value const& jv) + -> webhook_request +{ + return { + .notifications = json::value_to>( + at_or_null(jv, "notifications")), + }; +} + +credentials::credentials( + std::string&& api_key, std::string&& api_secret, std::string&& auth_token) + : api_key_{std::move(api_key)}, api_secret_{std::move(api_secret)}, + auth_token_{std::move(auth_token)} +{ +} + +credentials::~credentials() +{ + OPENSSL_cleanse(api_key_.data(), api_key_.capacity()); + OPENSSL_cleanse(api_secret_.data(), api_secret_.capacity()); + OPENSSL_cleanse(auth_token_.data(), auth_token_.capacity()); +} + +auto credentials::api_key() const -> std::string const& { return api_key_; } + +auto credentials::api_secret() const -> std::string const& +{ + return api_secret_; +} + +auto credentials::auth_token() const -> std::string const& +{ + return auth_token_; +} + constexpr std::string host = "ridewithgps.com"; // TODO: handle failure appropriately @@ -102,8 +203,8 @@ auto client::get_routes_page(std::size_t page) -> get_routes_response 11, // HTTP 1.1 }; req.set(bhttp::field::host, host); - req.set("x-rwgps-api-key", api_key_); - req.set("x-rwgps-auth-token", auth_token_); + req.set("x-rwgps-api-key", creds_->api_key()); + req.set("x-rwgps-auth-token", creds_->auth_token()); auto rsp = hc_.do_request(req); auto p = json::stream_parser{}; @@ -114,10 +215,9 @@ auto client::get_routes_page(std::size_t page) -> get_routes_response } client::client( - net::io_context& ioc, log::logger const& l, std::string api_key, - std::string auth_token) - : l_{l.sub("rwgps-client")}, hc_{ioc}, api_key_{std::move(api_key)}, - auth_token_{std::move(auth_token)} + net::io_context& ioc, log::logger const& l, + std::shared_ptr creds) + : l_{l.sub("rwgps-client")}, hc_{ioc}, creds_{std::move(creds)} { } -- cgit v1.3.1