Stop two servers taking turns claiming a contact's last sighting

A contact on two servers at once was marked seen by each in turn: a sighting
announced a contact change, every connection re-applied its contacts, and
each re-application recorded the contact on its own server — another change,
round and round, rewriting contacts.txt every time. Re-applying now only marks
a contact that was never seen; the last sighting moves when the client appears.

Sightings no longer write the file on their own either: they reach it in one
save half a minute later, or with the next edit's save, and are flushed when
the client closes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-30 16:08:36 +00:00
co-authored by Claude Opus 5.5
parent 1dda20d1b2
commit 9f2e5087ce
4 changed files with 62 additions and 7 deletions
@@ -80,6 +80,28 @@ class ContactStoreTest {
assertEquals("other:9987", a.lastSeenServerAddress);
}
@Test
void sightingsWaitForTheNextSave() throws Exception {
File file = new File(dir, "s.txt");
ContactStore store = new ContactStore(file);
store.add("a=", "A", ContactType.NEUTRAL, new ContactDefaults());
long written = file.lastModified();
String before = Files.readString(file.toPath(), StandardCharsets.UTF_8);
store.markSeen("a=", "Server", "host:9987");
assertEquals(before, Files.readString(file.toPath(), StandardCharsets.UTF_8));
store.flush();
assertTrue(Files.readString(file.toPath(), StandardCharsets.UTF_8).contains("LastSeenServerAddress=host:9987"));
// An edit's save takes a waiting sighting along; the flush then has nothing left to write.
store.markSeen("a=", "Other", "other:9987");
store.put(store.find("a="));
assertTrue(Files.readString(file.toPath(), StandardCharsets.UTF_8).contains("LastSeenServerAddress=other:9987"));
assertTrue(file.setLastModified(written));
store.flush();
assertEquals(written, file.lastModified());
}
@Test
void mergeAddsNewAndOverwritesExisting() {
ContactStore store = new ContactStore(new File(dir, "m.txt"));