diff options
| author | Rutger Broekhoff | 2026-11-30 11:47:36 +0100 |
|---|---|---|
| committer | Rutger Broekhoff | 2026-11-30 11:47:36 +0100 |
| commit | 02475e05ccd1aa15c6a6163834f28bfebbef796f (patch) | |
| tree | 6bc6f453f07dc76f763310e1e5eade9a3e1115b7 /server/src/api.cpp | |
| parent | 5732f6c14618f50ffaf315b93795340219a67c66 (diff) | |
| download | routemon-02475e05ccd1aa15c6a6163834f28bfebbef796f.tar.gz routemon-02475e05ccd1aa15c6a6163834f28bfebbef796f.zip | |
RWGPS webhook and such
Diffstat (limited to 'server/src/api.cpp')
| -rw-r--r-- | server/src/api.cpp | 104 |
1 files changed, 101 insertions, 3 deletions
diff --git a/server/src/api.cpp b/server/src/api.cpp index 9a51690..8a986d4 100644 --- a/server/src/api.cpp +++ b/server/src/api.cpp | |||
| @@ -99,8 +99,10 @@ auto tag_invoke(json::value_from_tag, json::value& jv, sysinfo const& info) | |||
| 99 | }; | 99 | }; |
| 100 | } | 100 | } |
| 101 | 101 | ||
| 102 | handler::handler(log::logger const& l, datex2::situation_publication pub) | 102 | handler::handler( |
| 103 | : l_{l.sub("handler")}, pub_{std::move(pub)} | 103 | log::logger const& l, std::shared_ptr<database::connection> dbc, |
| 104 | datex2::situation_publication pub) | ||
| 105 | : l_{l.sub("handler")}, dbc_{std::move(dbc)}, pub_{std::move(pub)} | ||
| 104 | { | 106 | { |
| 105 | auto const nsit = pub_.situations.size(); | 107 | auto const nsit = pub_.situations.size(); |
| 106 | auto progress_percent = | 108 | auto progress_percent = |
| @@ -145,7 +147,8 @@ handler::handler(log::logger const& l, datex2::situation_publication pub) | |||
| 145 | } | 147 | } |
| 146 | 148 | ||
| 147 | auto handler::process_gpx( | 149 | auto handler::process_gpx( |
| 148 | trace::id trace_id, gpx::file&& gpx_file, time::period_seq check_periods) | 150 | trace::id trace_id, gpx::file&& gpx_file, |
| 151 | time::period_seq<std::chrono::minutes> const& check_periods) | ||
| 149 | -> std::optional<process_gpx_result> | 152 | -> std::optional<process_gpx_result> |
| 150 | { | 153 | { |
| 151 | auto l = l_.with("trace_id", trace_id.as_string()); | 154 | auto l = l_.with("trace_id", trace_id.as_string()); |
| @@ -282,4 +285,99 @@ auto handler::sysinfo() -> struct sysinfo | |||
| 282 | }; | 285 | }; |
| 283 | } | 286 | } |
| 284 | 287 | ||
| 288 | auto rwgps_notification_cause_to_db(rwgps::notification_cause c) -> database::schema::rwgps_notification_cause | ||
| 289 | { | ||
| 290 | switch (c) | ||
| 291 | { | ||
| 292 | case rwgps::notification_cause::unknown: | ||
| 293 | return database::schema::rwgps_notification_cause::unknown; | ||
| 294 | case rwgps::notification_cause::created: | ||
| 295 | return database::schema::rwgps_notification_cause::created; | ||
| 296 | case rwgps::notification_cause::updated: | ||
| 297 | return database::schema::rwgps_notification_cause::updated; | ||
| 298 | case rwgps::notification_cause::deleted: | ||
| 299 | return database::schema::rwgps_notification_cause::deleted; | ||
| 300 | case rwgps::notification_cause::added: | ||
| 301 | return database::schema::rwgps_notification_cause::added; | ||
| 302 | case rwgps::notification_cause::removed: | ||
| 303 | return database::schema::rwgps_notification_cause::removed; | ||
| 304 | } | ||
| 305 | } | ||
| 306 | |||
| 307 | auto rwgps_notification_item_type_to_db(rwgps::notification_item_type t) | ||
| 308 | -> database::schema::rwgps_notification_item_type | ||
| 309 | { | ||
| 310 | switch (t) | ||
| 311 | { | ||
| 312 | case rwgps::notification_item_type::unknown: | ||
| 313 | return database::schema::rwgps_notification_item_type::unknown; | ||
| 314 | case rwgps::notification_item_type::trip: | ||
| 315 | return database::schema::rwgps_notification_item_type::trip; | ||
| 316 | case rwgps::notification_item_type::route: | ||
| 317 | return database::schema::rwgps_notification_item_type::route; | ||
| 318 | case rwgps::notification_item_type::event: | ||
| 319 | return database::schema::rwgps_notification_item_type::event; | ||
| 320 | case rwgps::notification_item_type::event_participant: | ||
| 321 | return database::schema::rwgps_notification_item_type::event_participant; | ||
| 322 | case rwgps::notification_item_type::club_member: | ||
| 323 | return database::schema::rwgps_notification_item_type::club_member; | ||
| 324 | case rwgps::notification_item_type::authorization: | ||
| 325 | return database::schema::rwgps_notification_item_type::authorization; | ||
| 326 | } | ||
| 327 | } | ||
| 328 | |||
| 329 | auto rwgps_notification_target_collection_type_to_db( | ||
| 330 | rwgps::notification_target_collection_type t) | ||
| 331 | -> database::schema::rwgps_notification_target_collection_type | ||
| 332 | { | ||
| 333 | switch (t) | ||
| 334 | { | ||
| 335 | case rwgps::notification_target_collection_type::unknown: | ||
| 336 | return database::schema::rwgps_notification_target_collection_type::unknown; | ||
| 337 | case rwgps::notification_target_collection_type::pinned: | ||
| 338 | return database::schema::rwgps_notification_target_collection_type::pinned; | ||
| 339 | } | ||
| 340 | } | ||
| 341 | |||
| 342 | auto handler::rwgps_webhook( | ||
| 343 | time::timestamp<std::chrono::seconds> rwgps_ts, rwgps::webhook_request req) | ||
| 344 | -> void | ||
| 345 | { | ||
| 346 | l_.info("Handling (valid) RWGPS webhook request"); | ||
| 347 | |||
| 348 | auto notifs = std::vector<std::pair< | ||
| 349 | database::schema::rwgps_notification, | ||
| 350 | std::optional<database::schema::rwgps_notification_target_collection> | ||
| 351 | >>{}; | ||
| 352 | |||
| 353 | for (auto const& notif : req.notifications) | ||
| 354 | { | ||
| 355 | auto db_notif = database::schema::rwgps_notification{ | ||
| 356 | .rwgps_timestamp = rwgps_ts, | ||
| 357 | .user_id = notif.user_id, | ||
| 358 | .item_type = rwgps_notification_item_type_to_db(notif.item_type), | ||
| 359 | .item_id = notif.item_id, | ||
| 360 | .item_user_id = notif.item_user_id, | ||
| 361 | .item_url = notif.item_url, | ||
| 362 | .action = rwgps_notification_cause_to_db(notif.action), | ||
| 363 | }; | ||
| 364 | |||
| 365 | auto mdb_collection = notif.collection.transform( | ||
| 366 | [](auto const& collection) | ||
| 367 | -> database::schema::rwgps_notification_target_collection | ||
| 368 | { | ||
| 369 | return { | ||
| 370 | .id = collection.id, | ||
| 371 | .type = rwgps_notification_target_collection_type_to_db( | ||
| 372 | collection.type), | ||
| 373 | .url = collection.url, | ||
| 374 | }; | ||
| 375 | }); | ||
| 376 | |||
| 377 | notifs.emplace_back(db_notif, mdb_collection); | ||
| 378 | } | ||
| 379 | |||
| 380 | dbc_->insert_rwgps_notifications(notifs); | ||
| 381 | } | ||
| 382 | |||
| 285 | } // namespace routemon::api | 383 | } // namespace routemon::api |