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/database.cpp | 116 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 109 insertions(+), 7 deletions(-) (limited to 'server/src/database.cpp') diff --git a/server/src/database.cpp b/server/src/database.cpp index 54e5f34..c1f9ab2 100644 --- a/server/src/database.cpp +++ b/server/src/database.cpp @@ -4,23 +4,125 @@ import :database; namespace routemon::database { -constexpr std::int64_t expected_database_version = 1; +namespace schema { -auto open(std::string const& filename) -> std::shared_ptr +constexpr std::int64_t expected_version = 2; + +auto try_to_string(rwgps_notification_cause c) -> std::optional +{ + switch (c) + { + case rwgps_notification_cause::unknown: + return std::nullopt; + case rwgps_notification_cause::created: + return "created"; + case rwgps_notification_cause::updated: + return "updated"; + case rwgps_notification_cause::deleted: + return "deleted"; + case rwgps_notification_cause::added: + return "added"; + case rwgps_notification_cause::removed: + return "removed"; + } +} + +auto try_to_string(rwgps_notification_item_type t) -> std::optional +{ + switch (t) + { + case rwgps_notification_item_type::unknown: + return std::nullopt; + case rwgps_notification_item_type::trip: + return "trip"; + case rwgps_notification_item_type::route: + return "route"; + case rwgps_notification_item_type::event: + return "event"; + case rwgps_notification_item_type::event_participant: + return "event_participant"; + case rwgps_notification_item_type::club_member: + return "club_member"; + case rwgps_notification_item_type::authorization: + return "authorization"; + } +} + +auto try_to_string(rwgps_notification_target_collection_type t) + -> std::optional +{ + switch (t) + { + case rwgps_notification_target_collection_type::unknown: + return std::nullopt; + case rwgps_notification_target_collection_type::pinned: + return "pinned"; + } +}; + +} // namespace schema + +auto connection::insert_rwgps_notifications( + std::vector + >>& notifs) -> void +{ + auto tx = sqlite3::tx{dbc_}; + for (auto& [notif, mtarget_collection] : notifs) + { + tx.query( + "INSERT INTO rwgps_notification " + " (rwgps_timestamp, user_id, item_type, item_id, " + " item_user_id, item_url, action_) " + "VALUES (:rwgps_timestamp, :user_id, :item_type, :item_id, " + " :item_user_id, :item_url, :action) " + "RETURNING id", + [¬if](auto& s) + { + auto const ts = time::iso8601_secs(notif.rwgps_timestamp); + s.bind(":rwgps_timestamp", std::string_view{ts}); + s.bind(":user_id", notif.user_id); + s.bind(":item_type", try_to_string(notif.item_type)); + s.bind(":item_id", notif.item_id); + s.bind(":item_user_id", notif.item_user_id); + s.bind(":item_url", notif.item_url); + s.bind(":action", try_to_string(notif.action)); + }) + .scan_row_max1(notif.id); + + if (mtarget_collection) + { + tx.exec( + "INSERT INTO rwgps_notification_target_collection " + " (notification_id, id, type_, url) " + "VALUES (:notification_id, :id, :type, :url)", + [¬if, &mtarget_collection](auto& s) + { + s.bind(":notification_id", notif.id); + s.bind(":id", mtarget_collection->id); + s.bind(":type", try_to_string(mtarget_collection->type)); + s.bind(":url", mtarget_collection->url); + }); + } + } +} + +auto open(util::zstring_view filename) -> std::shared_ptr { auto dbc = sqlite3::open(filename); try { - auto version = std::optional{}; - dbc.query("SELECT version FROM migration;").scan_single(version); - if (!version) + auto mversion = std::optional{}; + dbc.query("SELECT version FROM migration;").scan_row_max1(mversion); + if (!mversion) throw std::runtime_error{"failed to fetch database migration version"}; - if (version != expected_database_version) + if (mversion != schema::expected_version) { throw std::runtime_error{std::format( "database migration version ({}) does not match expected " "version ({}), consider running migrations", - *version, expected_database_version)}; + *mversion, schema::expected_version)}; } } catch (std::exception const& e) -- cgit v1.3.1