mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-12 02:39:05 +01:00
Rename event, only broadcast it when adding a new contact.
This commit is contained in:
@@ -3,27 +3,28 @@ package org.briarproject.bramble.api.mailbox.event;
|
||||
import org.briarproject.bramble.api.contact.ContactId;
|
||||
import org.briarproject.bramble.api.event.Event;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxUpdate;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxUpdateManager;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
|
||||
import javax.annotation.concurrent.Immutable;
|
||||
|
||||
/**
|
||||
* An event that is broadcast when a mailbox update is sent to a contact.
|
||||
* An event that is broadcast when the first mailbox update is sent to a
|
||||
* newly added contact, which happens in the same transaction in which the
|
||||
* contact is added.
|
||||
* <p>
|
||||
* Note that this event is not broadcast when a mailbox is paired or
|
||||
* unpaired, although updates are sent to all contacts in those situations.
|
||||
*
|
||||
* @see MailboxPairedEvent
|
||||
* @see MailboxUnpairedEvent
|
||||
* This event is not broadcast when the first mailbox update is sent to an
|
||||
* existing contact when setting up the
|
||||
* {@link MailboxUpdateManager mailbox update client}.
|
||||
*/
|
||||
@Immutable
|
||||
@NotNullByDefault
|
||||
public class MailboxUpdateSentEvent extends Event {
|
||||
public class MailboxUpdateSentToNewContactEvent extends Event {
|
||||
|
||||
private final ContactId contactId;
|
||||
private final MailboxUpdate mailboxUpdate;
|
||||
|
||||
public MailboxUpdateSentEvent(ContactId contactId,
|
||||
public MailboxUpdateSentToNewContactEvent(ContactId contactId,
|
||||
MailboxUpdate mailboxUpdate) {
|
||||
this.contactId = contactId;
|
||||
this.mailboxUpdate = mailboxUpdate;
|
||||
@@ -21,7 +21,7 @@ import org.briarproject.bramble.api.mailbox.MailboxUpdateWithMailbox;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxVersion;
|
||||
import org.briarproject.bramble.api.mailbox.event.MailboxPairedEvent;
|
||||
import org.briarproject.bramble.api.mailbox.event.MailboxUnpairedEvent;
|
||||
import org.briarproject.bramble.api.mailbox.event.MailboxUpdateSentEvent;
|
||||
import org.briarproject.bramble.api.mailbox.event.MailboxUpdateSentToNewContactEvent;
|
||||
import org.briarproject.bramble.api.mailbox.event.OwnMailboxConnectionStatusEvent;
|
||||
import org.briarproject.bramble.api.mailbox.event.RemoteMailboxUpdateEvent;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
@@ -269,9 +269,10 @@ class MailboxClientManager implements Service, EventListener {
|
||||
LOG.info("Mailbox unpaired");
|
||||
MailboxUnpairedEvent m = (MailboxUnpairedEvent) e;
|
||||
onMailboxUnpaired(m.getLocalUpdates());
|
||||
} else if (e instanceof MailboxUpdateSentEvent) {
|
||||
} else if (e instanceof MailboxUpdateSentToNewContactEvent) {
|
||||
LOG.info("Contact added");
|
||||
MailboxUpdateSentEvent m = (MailboxUpdateSentEvent) e;
|
||||
MailboxUpdateSentToNewContactEvent
|
||||
m = (MailboxUpdateSentToNewContactEvent) e;
|
||||
onContactAdded(m.getContactId(), m.getMailboxUpdate());
|
||||
} else if (e instanceof ContactRemovedEvent) {
|
||||
LOG.info("Contact removed");
|
||||
|
||||
@@ -27,7 +27,7 @@ import org.briarproject.bramble.api.mailbox.MailboxUpdateWithMailbox;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxVersion;
|
||||
import org.briarproject.bramble.api.mailbox.event.MailboxPairedEvent;
|
||||
import org.briarproject.bramble.api.mailbox.event.MailboxUnpairedEvent;
|
||||
import org.briarproject.bramble.api.mailbox.event.MailboxUpdateSentEvent;
|
||||
import org.briarproject.bramble.api.mailbox.event.MailboxUpdateSentToNewContactEvent;
|
||||
import org.briarproject.bramble.api.mailbox.event.RemoteMailboxUpdateEvent;
|
||||
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
|
||||
import org.briarproject.bramble.api.sync.Group;
|
||||
@@ -115,13 +115,12 @@ class MailboxUpdateManagerImpl implements MailboxUpdateManager,
|
||||
}
|
||||
Group g = getContactGroup(c);
|
||||
storeMessageReplaceLatest(txn, g.getId(), updated);
|
||||
txn.attach(new MailboxUpdateSentEvent(c.getId(), updated));
|
||||
}
|
||||
} else {
|
||||
db.addGroup(txn, localGroup);
|
||||
// Set things up for any pre-existing contacts
|
||||
for (Contact c : db.getContacts(txn)) {
|
||||
addingContact(txn, c);
|
||||
addingContact(txn, c, false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,6 +136,17 @@ class MailboxUpdateManagerImpl implements MailboxUpdateManager,
|
||||
|
||||
@Override
|
||||
public void addingContact(Transaction txn, Contact c) throws DbException {
|
||||
addingContact(txn, c, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param attachEvent True if a {@link MailboxUpdateSentToNewContactEvent}
|
||||
* should be attached to the transaction. We should only do this when
|
||||
* adding a new contact, not when setting up this client for an existing
|
||||
* contact.
|
||||
*/
|
||||
private void addingContact(Transaction txn, Contact c, boolean attachEvent)
|
||||
throws DbException {
|
||||
// Create a group to share with the contact
|
||||
Group g = getContactGroup(c);
|
||||
db.addGroup(txn, g);
|
||||
@@ -157,7 +167,9 @@ class MailboxUpdateManagerImpl implements MailboxUpdateManager,
|
||||
// Not paired, but we still want to get our clientSupports sent
|
||||
u = sendUpdateNoMailbox(txn, c);
|
||||
}
|
||||
txn.attach(new MailboxUpdateSentEvent(c.getId(), u));
|
||||
if (attachEvent) {
|
||||
txn.attach(new MailboxUpdateSentToNewContactEvent(c.getId(), u));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -19,7 +19,7 @@ import org.briarproject.bramble.api.mailbox.MailboxUpdateWithMailbox;
|
||||
import org.briarproject.bramble.api.mailbox.MailboxVersion;
|
||||
import org.briarproject.bramble.api.mailbox.event.MailboxPairedEvent;
|
||||
import org.briarproject.bramble.api.mailbox.event.MailboxUnpairedEvent;
|
||||
import org.briarproject.bramble.api.mailbox.event.MailboxUpdateSentEvent;
|
||||
import org.briarproject.bramble.api.mailbox.event.MailboxUpdateSentToNewContactEvent;
|
||||
import org.briarproject.bramble.api.mailbox.event.RemoteMailboxUpdateEvent;
|
||||
import org.briarproject.bramble.api.sync.Group;
|
||||
import org.briarproject.bramble.api.sync.GroupId;
|
||||
@@ -187,8 +187,7 @@ public class MailboxUpdateManagerImplTest extends BrambleMockTestCase {
|
||||
MailboxUpdateManagerImpl t = createInstance(someClientSupportsList);
|
||||
t.onDatabaseOpened(txn);
|
||||
|
||||
MailboxUpdateSentEvent e = getEvent(txn, MailboxUpdateSentEvent.class);
|
||||
assertNoMailboxPropertiesSent(e, someClientSupportsList);
|
||||
assertFalse(hasEvent(txn, MailboxUpdateSentToNewContactEvent.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -243,8 +242,7 @@ public class MailboxUpdateManagerImplTest extends BrambleMockTestCase {
|
||||
MailboxUpdateManagerImpl t = createInstance(someClientSupportsList);
|
||||
t.onDatabaseOpened(txn);
|
||||
|
||||
MailboxUpdateSentEvent e = getEvent(txn, MailboxUpdateSentEvent.class);
|
||||
assertMailboxPropertiesSent(e, someClientSupportsList);
|
||||
assertFalse(hasEvent(txn, MailboxUpdateSentToNewContactEvent.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -295,8 +293,7 @@ public class MailboxUpdateManagerImplTest extends BrambleMockTestCase {
|
||||
MailboxUpdateManagerImpl t = createInstance(someClientSupportsList);
|
||||
t.onDatabaseOpened(txn1);
|
||||
|
||||
MailboxUpdateSentEvent e = getEvent(txn1, MailboxUpdateSentEvent.class);
|
||||
assertNoMailboxPropertiesSent(e, someClientSupportsList);
|
||||
assertFalse(hasEvent(txn1, MailboxUpdateSentToNewContactEvent.class));
|
||||
|
||||
context.checking(new Expectations() {{
|
||||
oneOf(db).containsGroup(txn2, localGroup.getId());
|
||||
@@ -311,7 +308,7 @@ public class MailboxUpdateManagerImplTest extends BrambleMockTestCase {
|
||||
t = createInstance(someClientSupportsList);
|
||||
t.onDatabaseOpened(txn2);
|
||||
|
||||
assertFalse(hasEvent(txn2, MailboxUpdateSentEvent.class));
|
||||
assertFalse(hasEvent(txn2, MailboxUpdateSentToNewContactEvent.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -363,9 +360,7 @@ public class MailboxUpdateManagerImplTest extends BrambleMockTestCase {
|
||||
MailboxUpdateManagerImpl t = createInstance(someClientSupportsList);
|
||||
t.onDatabaseOpened(txn1);
|
||||
|
||||
MailboxUpdateSentEvent e1 =
|
||||
getEvent(txn1, MailboxUpdateSentEvent.class);
|
||||
assertNoMailboxPropertiesSent(e1, someClientSupportsList);
|
||||
assertFalse(hasEvent(txn1, MailboxUpdateSentToNewContactEvent.class));
|
||||
|
||||
BdfDictionary metaDictionary = BdfDictionary.of(
|
||||
new BdfEntry(MSG_KEY_VERSION, 1),
|
||||
@@ -427,9 +422,7 @@ public class MailboxUpdateManagerImplTest extends BrambleMockTestCase {
|
||||
t = createInstance(newerClientSupportsList);
|
||||
t.onDatabaseOpened(txn2);
|
||||
|
||||
MailboxUpdateSentEvent e2 =
|
||||
getEvent(txn2, MailboxUpdateSentEvent.class);
|
||||
assertNoMailboxPropertiesSent(e2, newerClientSupportsList);
|
||||
assertFalse(hasEvent(txn2, MailboxUpdateSentToNewContactEvent.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -466,7 +459,8 @@ public class MailboxUpdateManagerImplTest extends BrambleMockTestCase {
|
||||
MailboxUpdateManagerImpl t = createInstance(someClientSupportsList);
|
||||
t.addingContact(txn, contact);
|
||||
|
||||
MailboxUpdateSentEvent e = getEvent(txn, MailboxUpdateSentEvent.class);
|
||||
MailboxUpdateSentToNewContactEvent
|
||||
e = getEvent(txn, MailboxUpdateSentToNewContactEvent.class);
|
||||
assertNoMailboxPropertiesSent(e, someClientSupportsList);
|
||||
}
|
||||
|
||||
@@ -510,7 +504,8 @@ public class MailboxUpdateManagerImplTest extends BrambleMockTestCase {
|
||||
MailboxUpdateManagerImpl t = createInstance(someClientSupportsList);
|
||||
t.addingContact(txn, contact);
|
||||
|
||||
MailboxUpdateSentEvent e = getEvent(txn, MailboxUpdateSentEvent.class);
|
||||
MailboxUpdateSentToNewContactEvent
|
||||
e = getEvent(txn, MailboxUpdateSentToNewContactEvent.class);
|
||||
assertMailboxPropertiesSent(e, someClientSupportsList);
|
||||
}
|
||||
|
||||
@@ -722,7 +717,7 @@ public class MailboxUpdateManagerImplTest extends BrambleMockTestCase {
|
||||
assertEquals(updateWithMailbox.getMailboxProperties(),
|
||||
u.getMailboxProperties());
|
||||
|
||||
assertFalse(hasEvent(txn, MailboxUpdateSentEvent.class));
|
||||
assertFalse(hasEvent(txn, MailboxUpdateSentToNewContactEvent.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -765,7 +760,7 @@ public class MailboxUpdateManagerImplTest extends BrambleMockTestCase {
|
||||
MailboxUpdate u = localUpdates.get(contact.getId());
|
||||
assertFalse(u.hasMailbox());
|
||||
|
||||
assertFalse(hasEvent(txn, MailboxUpdateSentEvent.class));
|
||||
assertFalse(hasEvent(txn, MailboxUpdateSentToNewContactEvent.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -948,7 +943,8 @@ public class MailboxUpdateManagerImplTest extends BrambleMockTestCase {
|
||||
}});
|
||||
}
|
||||
|
||||
private void assertNoMailboxPropertiesSent(MailboxUpdateSentEvent e,
|
||||
private void assertNoMailboxPropertiesSent(
|
||||
MailboxUpdateSentToNewContactEvent e,
|
||||
List<MailboxVersion> clientSupports) {
|
||||
assertEquals(contact.getId(), e.getContactId());
|
||||
MailboxUpdate u = e.getMailboxUpdate();
|
||||
@@ -956,7 +952,8 @@ public class MailboxUpdateManagerImplTest extends BrambleMockTestCase {
|
||||
assertFalse(u.hasMailbox());
|
||||
}
|
||||
|
||||
private void assertMailboxPropertiesSent(MailboxUpdateSentEvent e,
|
||||
private void assertMailboxPropertiesSent(
|
||||
MailboxUpdateSentToNewContactEvent e,
|
||||
List<MailboxVersion> clientSupports) {
|
||||
assertEquals(contact.getId(), e.getContactId());
|
||||
MailboxUpdate u = e.getMailboxUpdate();
|
||||
|
||||
Reference in New Issue
Block a user