diff --git a/src/current_user.rs b/src/current_user.rs index 3446d93..2058bb5 100644 --- a/src/current_user.rs +++ b/src/current_user.rs @@ -6,6 +6,7 @@ pub struct CurrentUser { profile_event: Option, follow_list_event: Option, follows: HashSet, + stored_follow_list_event: Option, } impl CurrentUser { @@ -15,6 +16,7 @@ impl CurrentUser { profile_event: None, follow_list_event: None, follows: HashSet::new(), + stored_follow_list_event: None, } } @@ -43,12 +45,27 @@ impl CurrentUser { return false; } + // like the web page, a stored list newer than the one a relay returns is + // kept, since the relay's may still include a key that's been unfollowed. + let event = match self.stored_follow_list_event.take() { + Some(stored) if stored.created_at > event.created_at => stored, + _ => event, + }; + self.follows = event.tags.public_keys().collect(); self.follow_list_event = Some(event); true } + // a follow list from an outdated cache. like the web page, it's only used + // once a relay returns a follow list, so one no relay returns is dropped. + pub fn set_stored_follow_list(&mut self, event: Event) { + if self.is_newest(None, &event, Kind::ContactList) { + self.stored_follow_list_event = Some(event); + } + } + pub fn has_trusted(&self, public_key: &PublicKey) -> bool { self.public_key == *public_key || self.follows.contains(public_key) } @@ -95,6 +112,46 @@ mod tests { assert!(!current_user.has_trusted(&older_follow)); } + #[test] + fn keeps_a_stored_follow_list_newer_than_a_relays() { + let keys = Keys::generate(); + let newer_follow = Keys::generate().public_key(); + let older_follow = Keys::generate().public_key(); + let mut current_user = CurrentUser::new(keys.public_key()); + + current_user.set_stored_follow_list(build_follow_list(&keys, 2, &[newer_follow])); + assert!(current_user.set_follow_list(build_follow_list(&keys, 1, &[older_follow]))); + + assert!(current_user.has_trusted(&newer_follow)); + assert!(!current_user.has_trusted(&older_follow)); + } + + #[test] + fn keeps_a_relays_follow_list_newer_than_the_stored_one() { + let keys = Keys::generate(); + let newer_follow = Keys::generate().public_key(); + let older_follow = Keys::generate().public_key(); + let mut current_user = CurrentUser::new(keys.public_key()); + + current_user.set_stored_follow_list(build_follow_list(&keys, 1, &[older_follow])); + assert!(current_user.set_follow_list(build_follow_list(&keys, 2, &[newer_follow]))); + + assert!(current_user.has_trusted(&newer_follow)); + assert!(!current_user.has_trusted(&older_follow)); + } + + #[test] + fn doesnt_trust_a_stored_follow_list_until_a_relay_returns_one() { + let keys = Keys::generate(); + let followed = Keys::generate().public_key(); + let mut current_user = CurrentUser::new(keys.public_key()); + + current_user.set_stored_follow_list(build_follow_list(&keys, 1, &[followed])); + + assert!(!current_user.has_trusted(&followed)); + assert_eq!(current_user.follow_list_event(), None); + } + #[test] fn ignores_someone_elses_follow_list() { let followed = Keys::generate().public_key(); diff --git a/src/current_user_cache.rs b/src/current_user_cache.rs index fb16d0e..c99160a 100644 --- a/src/current_user_cache.rs +++ b/src/current_user_cache.rs @@ -14,6 +14,10 @@ pub struct CachedUser { requested_at: u64, pub profile_event: Option, pub follow_list_event: Option, + // whether it was requested within the hour. a stale one is still read, so + // its follow list can keep an older one a relay returns from replacing it. + #[serde(skip)] + pub is_fresh: bool, } // the cache only saves fetching, so one that can't be read or written is @@ -30,8 +34,9 @@ pub fn write(current_user: &CurrentUser) { fn read_from(directory: &Path, public_key: &PublicKey, now: Timestamp) -> Option { let json = fs::read_to_string(cache_path(directory, public_key)).ok()?; - let cached_user: CachedUser = serde_json::from_str(&json).ok()?; - let is_fresh = now.as_secs().saturating_sub(cached_user.requested_at) < REFRESH_AFTER_SECONDS; + let mut cached_user: CachedUser = serde_json::from_str(&json).ok()?; + cached_user.is_fresh = + now.as_secs().saturating_sub(cached_user.requested_at) < REFRESH_AFTER_SECONDS; let is_valid = is_valid_event( cached_user.profile_event.as_ref(), public_key, @@ -45,7 +50,7 @@ fn read_from(directory: &Path, public_key: &PublicKey, now: Timestamp) -> Option // a profile is required to connect, so a cache without one is refetched. let has_profile = cached_user.profile_event.is_some(); - (is_fresh && is_valid && has_profile).then_some(cached_user) + (is_valid && has_profile).then_some(cached_user) } // the file could have been edited, so its events are checked like a relay's. @@ -60,6 +65,7 @@ fn write_to(directory: &Path, current_user: &CurrentUser, now: Timestamp) { requested_at: now.as_secs(), profile_event: current_user.profile_event().cloned(), follow_list_event: current_user.follow_list_event().cloned(), + is_fresh: true, }; let Ok(json) = serde_json::to_string(&cached_user) else { return; @@ -148,6 +154,7 @@ mod tests { write_to(&cache.directory, ¤t_user, Timestamp::from_secs(NOW)); let cached_user = cache.read(NOW + 60).unwrap(); + assert!(cached_user.is_fresh); assert_eq!( cached_user.profile_event.as_ref(), current_user.profile_event() @@ -159,7 +166,7 @@ mod tests { } #[test] - fn ignores_a_cache_an_hour_old() { + fn reads_a_cache_an_hour_old_as_stale() { let cache = TestCache::new(); write_to( @@ -168,7 +175,7 @@ mod tests { Timestamp::from_secs(NOW), ); - assert!(cache.read(NOW + REFRESH_AFTER_SECONDS).is_none()); + assert!(!cache.read(NOW + REFRESH_AFTER_SECONDS).unwrap().is_fresh); } #[test] @@ -208,6 +215,7 @@ mod tests { requested_at: NOW, profile_event: Some(build_profile(&cache.keys, 1, "alice")), follow_list_event: Some(build_follow_list(&Keys::generate(), 1, &[])), + is_fresh: true, }); assert!(cache.read(NOW).is_none()); diff --git a/src/verification_publisher.rs b/src/verification_publisher.rs index 5cff906..fd5395d 100644 --- a/src/verification_publisher.rs +++ b/src/verification_publisher.rs @@ -86,8 +86,9 @@ pub async fn publish( // a fresh cached profile saves a lookup. a fetched one isn't cached, since the // cache also holds the follow list, which isn't fetched here. async fn fetch_profile_name(public_key: PublicKey, relay_config: &RelayConfig) -> Option { - let cached_profile = - current_user_cache::read(&public_key).and_then(|cached| cached.profile_event); + let cached_profile = current_user_cache::read(&public_key) + .filter(|cached| cached.is_fresh) + .and_then(|cached| cached.profile_event); let profile = match cached_profile { Some(profile) => profile, None => profile_fetcher::fetch(public_key, &relay_config.user_relay_urls).await?, diff --git a/src/verification_reporter.rs b/src/verification_reporter.rs index b27532e..3ee38b2 100644 --- a/src/verification_reporter.rs +++ b/src/verification_reporter.rs @@ -112,12 +112,23 @@ fn restore_cached_user( let Some(CachedUser { profile_event, follow_list_event, + is_fresh, .. }) = current_user_cache::read(&public_key) else { return Some(public_key); }; + if !is_fresh { + if let (Some(current_user), Some(follow_list_event)) = + (verifications.current_user.as_mut(), follow_list_event) + { + current_user.set_stored_follow_list(follow_list_event); + } + + return Some(public_key); + } + let updates = profile_event .map(Update::Profile) .into_iter()