Allow multiple identities to have the same contact. #224

This commit is contained in:
akwizgran
2016-01-28 15:57:22 +00:00
parent ce9a81ff98
commit 0fd8a67ee0
4 changed files with 16 additions and 15 deletions

View File

@@ -155,11 +155,13 @@ interface Database<T> {
void addVisibility(T txn, ContactId c, GroupId g) throws DbException; void addVisibility(T txn, ContactId c, GroupId g) throws DbException;
/** /**
* Returns true if the database contains the given contact. * Returns true if the database contains the given contact for the given
* local pseudonym.
* <p> * <p>
* Locking: read. * Locking: read.
*/ */
boolean containsContact(T txn, AuthorId a) throws DbException; boolean containsContact(T txn, AuthorId remote, AuthorId local)
throws DbException;
/** /**
* Returns true if the database contains the given contact. * Returns true if the database contains the given contact.

View File

@@ -144,10 +144,10 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
try { try {
T txn = db.startTransaction(); T txn = db.startTransaction();
try { try {
if (db.containsContact(txn, remote.getId()))
throw new ContactExistsException();
if (!db.containsLocalAuthor(txn, local)) if (!db.containsLocalAuthor(txn, local))
throw new NoSuchLocalAuthorException(); throw new NoSuchLocalAuthorException();
if (db.containsContact(txn, remote.getId(), local))
throw new ContactExistsException();
ContactId c = db.addContact(txn, remote, local); ContactId c = db.addContact(txn, remote, local);
db.commitTransaction(txn); db.commitTransaction(txn);
return c; return c;

View File

@@ -871,14 +871,16 @@ abstract class JdbcDatabase implements Database<Connection> {
} }
} }
public boolean containsContact(Connection txn, AuthorId a) public boolean containsContact(Connection txn, AuthorId remote,
throws DbException { AuthorId local) throws DbException {
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs = null;
try { try {
String sql = "SELECT NULL FROM contacts WHERE authorId = ?"; String sql = "SELECT NULL FROM contacts"
+ " WHERE authorId = ? AND localAuthorId = ?";
ps = txn.prepareStatement(sql); ps = txn.prepareStatement(sql);
ps.setBytes(1, a.getBytes()); ps.setBytes(1, remote.getBytes());
ps.setBytes(2, local.getBytes());
rs = ps.executeQuery(); rs = ps.executeQuery();
boolean found = rs.next(); boolean found = rs.next();
if (rs.next()) throw new DbStateException(); if (rs.next()) throw new DbStateException();

View File

@@ -132,10 +132,10 @@ public class DatabaseComponentImplTest extends BriarTestCase {
will(returnValue(false)); will(returnValue(false));
oneOf(database).addLocalAuthor(txn, localAuthor); oneOf(database).addLocalAuthor(txn, localAuthor);
// addContact() // addContact()
oneOf(database).containsContact(txn, authorId);
will(returnValue(false));
oneOf(database).containsLocalAuthor(txn, localAuthorId); oneOf(database).containsLocalAuthor(txn, localAuthorId);
will(returnValue(true)); will(returnValue(true));
oneOf(database).containsContact(txn, authorId, localAuthorId);
will(returnValue(false));
oneOf(database).addContact(txn, author, localAuthorId); oneOf(database).addContact(txn, author, localAuthorId);
will(returnValue(contactId)); will(returnValue(contactId));
// getContacts() // getContacts()
@@ -447,9 +447,6 @@ public class DatabaseComponentImplTest extends BriarTestCase {
exactly(3).of(database).containsLocalAuthor(txn, localAuthorId); exactly(3).of(database).containsLocalAuthor(txn, localAuthorId);
will(returnValue(false)); will(returnValue(false));
exactly(3).of(database).abortTransaction(txn); exactly(3).of(database).abortTransaction(txn);
// This is needed for addContact() to proceed
exactly(1).of(database).containsContact(txn, authorId);
will(returnValue(false));
}}); }});
DatabaseComponent db = createDatabaseComponent(database, eventBus, DatabaseComponent db = createDatabaseComponent(database, eventBus,
shutdown); shutdown);
@@ -624,10 +621,10 @@ public class DatabaseComponentImplTest extends BriarTestCase {
// addContact() // addContact()
oneOf(database).startTransaction(); oneOf(database).startTransaction();
will(returnValue(txn)); will(returnValue(txn));
oneOf(database).containsContact(txn, authorId);
will(returnValue(false));
oneOf(database).containsLocalAuthor(txn, localAuthorId); oneOf(database).containsLocalAuthor(txn, localAuthorId);
will(returnValue(true)); will(returnValue(true));
oneOf(database).containsContact(txn, authorId, localAuthorId);
will(returnValue(false));
oneOf(database).addContact(txn, author, localAuthorId); oneOf(database).addContact(txn, author, localAuthorId);
will(returnValue(contactId)); will(returnValue(contactId));
oneOf(database).commitTransaction(txn); oneOf(database).commitTransaction(txn);