From c564313f7e97f2fd7db98c2acca187c809a40a8c Mon Sep 17 00:00:00 2001 From: Michael Jurkoic Date: Tue, 12 Mar 2024 09:34:51 -0500 Subject: Add a filters struct for relay queries --- include/nostr.hpp | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'include') diff --git a/include/nostr.hpp b/include/nostr.hpp index ec8d1a8..ce25446 100644 --- a/include/nostr.hpp +++ b/include/nostr.hpp @@ -15,6 +15,7 @@ namespace nostr { typedef std::vector RelayList; +typedef std::unordered_map> TagMap; // TODO: Add null checking to seralization and deserialization methods. /** @@ -55,6 +56,38 @@ private: void validate(); }; +/** + * @brief A set of filters for querying Nostr relays. + * @remark The `limit` field should always be included to keep the response size reasonable. The + * `since` field is not required, and the `until` field will default to the present. At least one + * of the other fields must be set for a valid filter. + */ +struct Filters +{ + std::vector ids; ///< Event IDs. + std::vector authors; ///< Event author npubs. + std::vector kinds; ///< Kind numbers. + TagMap tags; ///< Tag names mapped to lists of tag values. + std::time_t since; ///< Unix timestamp. Matching events must be newer than this. + std::time_t until; ///< Unix timestamp. Matching events must be older than this. + int limit; ///< The maximum number of events the relay should return on the initial query. + + /** + * @brief Serializes the filters to a JSON object. + * @returns A stringified JSON object representing the filters. + * @throws `std::invalid_argument` if the filter object is invalid. + */ + std::string serialize(); + +private: + /** + * @brief Validates the filters. + * @throws `std::invalid_argument` if the filter object is invalid. + * @remark The `until` field defaults to the present if it is not already set. + */ + void validate(); +}; + class NostrService { public: -- cgit