nostr_storage.rs 1013 B

12345678910111213141516171819202122232425262728293031323334353637
  1. use std::fs;
  2. use std::path::Path;
  3. use anyhow::Result;
  4. use cdk::nuts::PublicKey;
  5. use cdk::util::hex;
  6. /// Stores the last checked time for a nostr key in a file
  7. pub async fn store_nostr_last_checked(
  8. work_dir: &Path,
  9. verifying_key: &PublicKey,
  10. last_checked: u32,
  11. ) -> Result<()> {
  12. let key_hex = hex::encode(verifying_key.to_bytes());
  13. let file_path = work_dir.join(format!("nostr_last_checked_{}", key_hex));
  14. fs::write(file_path, last_checked.to_string())?;
  15. Ok(())
  16. }
  17. /// Gets the last checked time for a nostr key from a file
  18. pub async fn get_nostr_last_checked(
  19. work_dir: &Path,
  20. verifying_key: &PublicKey,
  21. ) -> Result<Option<u32>> {
  22. let key_hex = hex::encode(verifying_key.to_bytes());
  23. let file_path = work_dir.join(format!("nostr_last_checked_{}", key_hex));
  24. match fs::read_to_string(file_path) {
  25. Ok(content) => {
  26. let timestamp = content.trim().parse::<u32>()?;
  27. Ok(Some(timestamp))
  28. }
  29. Err(_) => Ok(None),
  30. }
  31. }