Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/current_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub struct CurrentUser {
profile_event: Option<Event>,
follow_list_event: Option<Event>,
follows: HashSet<PublicKey>,
stored_follow_list_event: Option<Event>,
}

impl CurrentUser {
Expand All @@ -15,6 +16,7 @@ impl CurrentUser {
profile_event: None,
follow_list_event: None,
follows: HashSet::new(),
stored_follow_list_event: None,
}
}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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();
Expand Down
18 changes: 13 additions & 5 deletions src/current_user_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ pub struct CachedUser {
requested_at: u64,
pub profile_event: Option<Event>,
pub follow_list_event: Option<Event>,
// 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
Expand All @@ -30,8 +34,9 @@ pub fn write(current_user: &CurrentUser) {

fn read_from(directory: &Path, public_key: &PublicKey, now: Timestamp) -> Option<CachedUser> {
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,
Expand All @@ -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.
Expand All @@ -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;
Expand Down Expand Up @@ -148,6 +154,7 @@ mod tests {
write_to(&cache.directory, &current_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()
Expand All @@ -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(
Expand All @@ -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]
Expand Down Expand Up @@ -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());
Expand Down
5 changes: 3 additions & 2 deletions src/verification_publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
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?,
Expand Down
11 changes: 11 additions & 0 deletions src/verification_reporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading