Contact aliases: address review comments

This commit is contained in:
Torsten Grote
2018-10-31 12:05:33 -03:00
parent ecf417c93b
commit 3fe7aae97e
8 changed files with 44 additions and 24 deletions

View File

@@ -96,6 +96,12 @@ public interface ContactManager {
void setContactActive(Transaction txn, ContactId c, boolean active) void setContactActive(Transaction txn, ContactId c, boolean active)
throws DbException; throws DbException;
/**
* Sets an alias name for the contact or unsets it if alias is null.
*/
void setContactAlias(Transaction txn, ContactId c, @Nullable String alias)
throws DbException;
/** /**
* Sets an alias name for the contact or unsets it if alias is null. * Sets an alias name for the contact or unsets it if alias is null.
*/ */

View File

@@ -163,14 +163,20 @@ class ContactManagerImpl implements ContactManager {
} }
@Override @Override
public void setContactAlias(ContactId c, @Nullable String alias) public void setContactAlias(Transaction txn, ContactId c,
throws DbException { @Nullable String alias) throws DbException {
if (alias != null) { if (alias != null) {
int aliasLength = toUtf8(alias).length; int aliasLength = toUtf8(alias).length;
if (aliasLength == 0 || aliasLength > MAX_AUTHOR_NAME_LENGTH) if (aliasLength == 0 || aliasLength > MAX_AUTHOR_NAME_LENGTH)
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
db.transaction(false, txn -> db.setContactAlias(txn, c, alias)); db.setContactAlias(txn, c, alias);
}
@Override
public void setContactAlias(ContactId c, @Nullable String alias)
throws DbException {
db.transaction(false, txn -> setContactAlias(txn, c, alias));
} }
@Override @Override

View File

@@ -204,7 +204,8 @@ public class ContactManagerImplTest extends BrambleMockTestCase {
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void testSetContactAliasTooLong() throws Exception { public void testSetContactAliasTooLong() throws Exception {
contactManager.setContactAlias(contactId, Transaction txn = new Transaction(null, false);
contactManager.setContactAlias(txn, contactId,
getRandomString(MAX_AUTHOR_NAME_LENGTH + 1)); getRandomString(MAX_AUTHOR_NAME_LENGTH + 1));
} }

View File

@@ -10,7 +10,6 @@ import org.briarproject.briar.android.threaded.BaseThreadItemViewHolder;
import org.briarproject.briar.android.threaded.ThreadItemAdapter.ThreadItemListener; import org.briarproject.briar.android.threaded.ThreadItemAdapter.ThreadItemListener;
import static org.briarproject.bramble.api.identity.AuthorInfo.Status.OURSELVES; import static org.briarproject.bramble.api.identity.AuthorInfo.Status.OURSELVES;
import static org.briarproject.briar.android.util.UiUtils.getContactDisplayName;
@UiThread @UiThread
@NotNullByDefault @NotNullByDefault
@@ -37,8 +36,7 @@ class JoinMessageItemViewHolder
if (item.isInitial()) { if (item.isInitial()) {
textView.setText(R.string.groups_member_created_you); textView.setText(R.string.groups_member_created_you);
} else { } else {
String name = getContactDisplayName(item.getAuthor(), String name = item.getAuthorName();
item.getAuthorInfo().getAlias());
textView.setText(getContext() textView.setText(getContext()
.getString(R.string.groups_member_joined, name)); .getString(R.string.groups_member_joined, name));
} }
@@ -46,8 +44,7 @@ class JoinMessageItemViewHolder
private void bind(JoinMessageItem item) { private void bind(JoinMessageItem item) {
Context ctx = getContext(); Context ctx = getContext();
String name = getContactDisplayName(item.getAuthor(), String name = item.getAuthorName();
item.getAuthorInfo().getAlias());
if (item.isInitial()) { if (item.isInitial()) {
textView.setText( textView.setText(

View File

@@ -10,6 +10,7 @@ import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe; import javax.annotation.concurrent.NotThreadSafe;
import static org.briarproject.briar.android.threaded.ThreadItemAdapter.UNDEFINED; import static org.briarproject.briar.android.threaded.ThreadItemAdapter.UNDEFINED;
import static org.briarproject.briar.android.util.UiUtils.getContactDisplayName;
@NotThreadSafe @NotThreadSafe
@NotNullByDefault @NotNullByDefault
@@ -70,6 +71,13 @@ public abstract class ThreadItem implements MessageNode {
return authorInfo; return authorInfo;
} }
/**
* Returns the author's name, with an alias if one exists.
*/
public String getAuthorName() {
return getContactDisplayName(author, authorInfo.getAlias());
}
@Override @Override
public void setLevel(int level) { public void setLevel(int level) {
this.level = level; this.level = level;

View File

@@ -141,7 +141,7 @@ public class UiUtils {
} }
public static Spanned getSpanned(@Nullable String s) { public static Spanned getSpanned(@Nullable String s) {
// TODO move to HtmlCompat // TODO move to HtmlCompat #1435
// https://commonsware.com/blog/2018/05/29/at-last-htmlcompat.html // https://commonsware.com/blog/2018/05/29/at-last-htmlcompat.html
return Html.fromHtml(s); return Html.fromHtml(s);
} }

View File

@@ -421,19 +421,14 @@ class IntroductionManagerImpl extends ConversationClientImpl
if (ss == null) throw new AssertionError(); if (ss == null) throw new AssertionError();
MessageType type = meta.getMessageType(); MessageType type = meta.getMessageType();
if (type == REQUEST) { if (type == REQUEST) {
messages.add( messages.add(parseInvitationRequest(txn, contactGroupId, m,
parseInvitationRequest(txn, contactGroupId, m, meta, status, ss.bdfSession, authorInfos));
meta, status, ss.bdfSession, authorInfos));
} else if (type == ACCEPT) { } else if (type == ACCEPT) {
messages.add( messages.add(parseInvitationResponse(txn, contactGroupId, m,
parseInvitationResponse(txn, contactGroupId, m, meta, status, ss.bdfSession, authorInfos, true));
meta, status, ss.bdfSession, authorInfos,
true));
} else if (type == DECLINE) { } else if (type == DECLINE) {
messages.add( messages.add(parseInvitationResponse(txn, contactGroupId, m,
parseInvitationResponse(txn, contactGroupId, m, meta, status, ss.bdfSession, authorInfos, false));
meta, status, ss.bdfSession, authorInfos,
false));
} }
} }
return messages; return messages;

View File

@@ -174,6 +174,10 @@ public class TestDataCreatorImpl implements TestDataCreator {
ContactId contactId = contactManager ContactId contactId = contactManager
.addContact(txn, author, localAuthorId, secretKey, .addContact(txn, author, localAuthorId, secretKey,
timestamp, true, verified, true); timestamp, true, verified, true);
if (random.nextBoolean()) {
contactManager
.setContactAlias(txn, contactId, getRandomAuthorName());
}
transportPropertyManager.addRemoteProperties(txn, contactId, props); transportPropertyManager.addRemoteProperties(txn, contactId, props);
contact = db.getContact(txn, contactId); contact = db.getContact(txn, contactId);
db.commitTransaction(txn); db.commitTransaction(txn);
@@ -202,10 +206,13 @@ public class TestDataCreatorImpl implements TestDataCreator {
return authorFactory.createLocalAuthor(name, publicKey, privateKey); return authorFactory.createLocalAuthor(name, publicKey, privateKey);
} }
private LocalAuthor getRandomAuthor() { private String getRandomAuthorName() {
int i = random.nextInt(AUTHOR_NAMES.length); int i = random.nextInt(AUTHOR_NAMES.length);
String authorName = AUTHOR_NAMES[i]; return AUTHOR_NAMES[i];
return getAuthor(authorName); }
private LocalAuthor getRandomAuthor() {
return getAuthor(getRandomAuthorName());
} }
private SecretKey getSecretKey() { private SecretKey getSecretKey() {