Skip to content

Commit

Permalink
Don't create a new history entry when updating a read marker due to `…
Browse files Browse the repository at this point in the history
…MARKREAD`; update on disk instead.
  • Loading branch information
andymandias committed Sep 24, 2024
1 parent 9208f1d commit bd717e2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
34 changes: 21 additions & 13 deletions data/src/history/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ pub enum Message {
history::Kind,
Result<history::Metadata, history::Error>,
),
UpdateReadMarker(
server::Server,
history::Kind,
history::ReadMarker,
Result<(), history::Error>,
),
Closed(
server::Server,
history::Kind,
Expand Down Expand Up @@ -110,6 +116,14 @@ impl Manager {
Message::UpdatePartial(server, kind, Err(error)) => {
log::warn!("failed to load history metadata for {kind} on {server}: {error}");
}
Message::UpdateReadMarker(server, kind, read_marker, Ok(_)) => {
log::debug!("updated read marker for {kind} on {server} to {read_marker}");
}
Message::UpdateReadMarker(server, kind, read_marker, Err(error)) => {
log::warn!(
"failed to update read marker for {kind} on {server} to {read_marker}: {error}"
);
}
}

None
Expand Down Expand Up @@ -719,20 +733,14 @@ impl Data {

None
}
hash_map::Entry::Vacant(entry) => {
entry
.insert(History::partial(server.clone(), kind.clone()))
.update_read_marker(read_marker);
hash_map::Entry::Vacant(_) => Some(
async move {
let updated = history::metadata::update(&server, &kind, &read_marker).await;

Some(
async move {
let loaded = history::metadata::load(server.clone(), kind.clone()).await;

Message::UpdatePartial(server, kind, loaded)
}
.boxed(),
)
}
Message::UpdateReadMarker(server, kind, read_marker, updated)
}
.boxed(),
),
}
}

Expand Down
26 changes: 26 additions & 0 deletions data/src/history/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,32 @@ pub async fn save(
Ok(())
}

pub async fn update(
server: &server::Server,
kind: &Kind,
read_marker: &ReadMarker,
) -> Result<(), Error> {
let metadata = load(server.clone(), kind.clone()).await?;

if metadata
.read_marker
.is_some_and(|metadata_read_marker| metadata_read_marker >= *read_marker)
{
return Ok(());
}

let bytes = serde_json::to_vec(&Metadata {
read_marker: Some(*read_marker),
last_triggers_unread: metadata.last_triggers_unread,
})?;

let path = path(server, kind).await?;

fs::write(path, &bytes).await?;

Ok(())
}

async fn path(server: &server::Server, kind: &Kind) -> Result<PathBuf, Error> {
let dir = dir_path().await?;

Expand Down

0 comments on commit bd717e2

Please sign in to comment.