Verified Commit 10d9d78c authored by Sebastian's avatar Sebastian
Browse files

Broadcast ContactAliasChangedEvent to update contact list

parent bebf3bbc
Pipeline #6376 passed with stages
in 8 minutes and 23 seconds
package org.briarproject.bramble.api.contact.event;
import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.event.Event;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
/**
* An event that is broadcast when the alias for a contact changed.
*/
@Immutable
@NotNullByDefault
public class ContactAliasChangedEvent extends Event {
private final ContactId contactId;
@Nullable
private final String alias;
public ContactAliasChangedEvent(ContactId contactId,
@Nullable String alias) {
this.contactId = contactId;
this.alias = alias;
}
public ContactId getContactId() {
return contactId;
}
@Nullable
public String getAlias() {
return alias;
}
}
......@@ -5,6 +5,7 @@ import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.contact.PendingContact;
import org.briarproject.bramble.api.contact.PendingContactId;
import org.briarproject.bramble.api.contact.event.ContactAddedEvent;
import org.briarproject.bramble.api.contact.event.ContactAliasChangedEvent;
import org.briarproject.bramble.api.contact.event.ContactRemovedEvent;
import org.briarproject.bramble.api.contact.event.ContactVerifiedEvent;
import org.briarproject.bramble.api.contact.event.PendingContactAddedEvent;
......@@ -970,6 +971,7 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
T txn = unbox(transaction);
if (!db.containsContact(txn, c))
throw new NoSuchContactException();
transaction.attach(new ContactAliasChangedEvent(c, alias));
db.setContactAlias(txn, c, alias);
}
......
......@@ -48,6 +48,10 @@ public class ContactListAdapter extends
if (c1.isConnected() != c2.isConnected()) {
return false;
}
if (!NullSafety.equals(c1.getContact().getAlias(),
c2.getContact().getAlias())) {
return false;
}
return NullSafety.equals(c1.getAuthorInfo().getAvatarHeader(),
c2.getAuthorInfo().getAvatarHeader());
}
......
......@@ -7,6 +7,7 @@ import org.briarproject.briar.api.client.MessageTracker.GroupCount;
import org.briarproject.briar.api.conversation.ConversationMessageHeader;
import org.briarproject.briar.api.identity.AuthorInfo;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
@Immutable
......@@ -45,12 +46,24 @@ public class ContactListItem extends ContactItem
Math.max(h.getTimestamp(), item.timestamp));
}
/**
* Creates a new copy of the given item with a new alias set.
*/
ContactListItem(ContactListItem item, @Nullable String alias) {
this(update(item.getContact(), alias), item.getAuthorInfo(),
item.isConnected(), item.empty, item.unread, item.timestamp);
}
private static Contact update(Contact c, @Nullable String alias) {
return new Contact(c.getId(), c.getAuthor(), c.getLocalAuthorId(),
alias, c.getHandshakePublicKey(), c.isVerified());
}
/**
* Creates a new copy of the given item with a new avatar
* referenced by the given attachment header.
*/
ContactListItem(ContactListItem item,
AttachmentHeader attachmentHeader) {
ContactListItem(ContactListItem item, AttachmentHeader attachmentHeader) {
this(item.getContact(), new AuthorInfo(item.getAuthorInfo().getStatus(),
item.getAuthorInfo().getAlias(), attachmentHeader),
item.isConnected(), item.empty, item.unread, item.timestamp);
......
......@@ -7,6 +7,7 @@ import org.briarproject.bramble.api.contact.Contact;
import org.briarproject.bramble.api.contact.ContactId;
import org.briarproject.bramble.api.contact.ContactManager;
import org.briarproject.bramble.api.contact.event.ContactAddedEvent;
import org.briarproject.bramble.api.contact.event.ContactAliasChangedEvent;
import org.briarproject.bramble.api.contact.event.ContactRemovedEvent;
import org.briarproject.bramble.api.db.DatabaseExecutor;
import org.briarproject.bramble.api.db.DbException;
......@@ -141,6 +142,10 @@ public class ContactsViewModel extends DbViewModel implements EventListener {
AvatarUpdatedEvent a = (AvatarUpdatedEvent) e;
updateItem(a.getContactId(), item -> new ContactListItem(item,
a.getAttachmentHeader()), false);
} else if (e instanceof ContactAliasChangedEvent) {
ContactAliasChangedEvent c = (ContactAliasChangedEvent) e;
updateItem(c.getContactId(),
item -> new ContactListItem(item, c.getAlias()), false);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment