Skip to content

Commit

Permalink
fix(sync): Prevent post cache from growing indefinitely
Browse files Browse the repository at this point in the history
  • Loading branch information
klausi committed Feb 16, 2019
1 parent e6b0d71 commit 753375e
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,7 @@ pub fn filter_posted_before(posts: StatusUpdates) -> StatusUpdates {
}

let cache_file = "post_cache.json";
let mut cache: HashSet<String> = match File::open(cache_file) {
Ok(mut file) => {
let mut json = String::new();
file.read_to_string(&mut json).unwrap();
serde_json::from_str(&json).unwrap()
}
Err(_) => HashSet::new(),
};
let mut cache = read_post_cache(cache_file);
let mut filtered_posts = StatusUpdates {
tweets: Vec::new(),
toots: Vec::new(),
Expand Down Expand Up @@ -244,6 +237,30 @@ pub fn filter_posted_before(posts: StatusUpdates) -> StatusUpdates {
filtered_posts
}

// Read the JSON encoded cache file from disk or provide en empty default cache.
fn read_post_cache(cache_file: &str) -> HashSet<String> {
match File::open(cache_file) {
Ok(mut file) => {
let mut json = String::new();
let _ = file.read_to_string(&mut json);
match serde_json::from_str::<HashSet<String>>(&json) {
Ok(cache) => {
// If the cache has more than 50 items already then empty it to not
// accumulate too many items and allow posting the same text at a
// later date.
if cache.len() > 50 {
HashSet::new()
} else {
cache
}
},
Err(_) => HashSet::new(),
}
}
Err(_) => HashSet::new(),
}
}

// Returns a list of direct links to attachments for download.
fn tweet_get_attachments(tweet: &Tweet) -> Vec<NewMedia> {
let mut links = Vec::new();
Expand Down

0 comments on commit 753375e

Please sign in to comment.