diff options
author | Michael Jurkoic <mjjurkoic@gmail.com> | 2024-03-12 09:34:51 -0500 |
---|---|---|
committer | Michael Jurkoic <mjjurkoic@gmail.com> | 2024-03-12 09:34:51 -0500 |
commit | c564313f7e97f2fd7db98c2acca187c809a40a8c (patch) | |
tree | 71a5420340b981df146c76cce0a1db9693980c4f /include/nostr.hpp | |
parent | 760d5d9adab13edc090f64437415b41b229481f8 (diff) |
Add a filters struct for relay queries
Diffstat (limited to 'include/nostr.hpp')
-rw-r--r-- | include/nostr.hpp | 33 |
1 files changed, 33 insertions, 0 deletions
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<std::string> RelayList; +typedef std::unordered_map<std::string, std::vector<std::string>> 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<std::string> ids; ///< Event IDs. + std::vector<std::string> authors; ///< Event author npubs. + std::vector<int> 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: |