Add introduced contacts as UNVERIFIED

Closes #580
This commit is contained in:
Torsten Grote
2016-08-10 12:35:33 -03:00
parent 70b311db13
commit e690bcb3cc
22 changed files with 201 additions and 126 deletions

View File

@@ -63,8 +63,8 @@ interface Database<T> {
* Stores a contact associated with the given local and remote pseudonyms,
* and returns an ID for the contact.
*/
ContactId addContact(T txn, Author remote, AuthorId local, boolean active)
throws DbException;
ContactId addContact(T txn, Author remote, AuthorId local, boolean verified,
boolean active) throws DbException;
/**
* Stores a group.
@@ -575,6 +575,12 @@ interface Database<T> {
*/
void resetExpiryTime(T txn, ContactId c, MessageId m) throws DbException;
/**
* Marks the given contact as verified or unverified.
*/
void setContactVerified(T txn, ContactId c, boolean verified)
throws DbException;
/**
* Marks the given contact as active or inactive.
*/

View File

@@ -152,7 +152,8 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
}
public ContactId addContact(Transaction transaction, Author remote,
AuthorId local, boolean active) throws DbException {
AuthorId local, boolean verified, boolean active)
throws DbException {
if (transaction.isReadOnly()) throw new IllegalArgumentException();
T txn = unbox(transaction);
if (!db.containsLocalAuthor(txn, local))
@@ -161,7 +162,7 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
throw new ContactExistsException();
if (db.containsContact(txn, remote.getId(), local))
throw new ContactExistsException();
ContactId c = db.addContact(txn, remote, local, active);
ContactId c = db.addContact(txn, remote, local, verified, active);
transaction.attach(new ContactAddedEvent(c, active));
if (active) transaction.attach(new ContactStatusChangedEvent(c, true));
return c;
@@ -682,6 +683,16 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
db.removeTransport(txn, t);
}
public void setContactVerified(Transaction transaction, ContactId c,
boolean verified) throws DbException {
if (transaction.isReadOnly()) throw new IllegalArgumentException();
T txn = unbox(transaction);
if (!db.containsContact(txn, c))
throw new NoSuchContactException();
db.setContactVerified(txn, c, verified);
transaction.attach(new ContactStatusChangedEvent(c, verified));
}
public void setContactActive(Transaction transaction, ContactId c,
boolean active) throws DbException {
if (transaction.isReadOnly()) throw new IllegalArgumentException();

View File

@@ -67,8 +67,8 @@ import static org.briarproject.db.ExponentialBackoff.calculateExpiry;
*/
abstract class JdbcDatabase implements Database<Connection> {
private static final int SCHEMA_VERSION = 25;
private static final int MIN_SCHEMA_VERSION = 25;
private static final int SCHEMA_VERSION = 26;
private static final int MIN_SCHEMA_VERSION = 26;
private static final String CREATE_SETTINGS =
"CREATE TABLE settings"
@@ -93,6 +93,7 @@ abstract class JdbcDatabase implements Database<Connection> {
+ " name VARCHAR NOT NULL,"
+ " publicKey BINARY NOT NULL,"
+ " localAuthorId HASH NOT NULL,"
+ " verified BOOLEAN NOT NULL,"
+ " active BOOLEAN NOT NULL,"
+ " PRIMARY KEY (contactId),"
+ " FOREIGN KEY (localAuthorId)"
@@ -456,20 +457,21 @@ abstract class JdbcDatabase implements Database<Connection> {
}
public ContactId addContact(Connection txn, Author remote, AuthorId local,
boolean active) throws DbException {
boolean verified, boolean active) throws DbException {
PreparedStatement ps = null;
ResultSet rs = null;
try {
// Create a contact row
String sql = "INSERT INTO contacts"
+ " (authorId, name, publicKey, localAuthorId, active)"
+ " VALUES (?, ?, ?, ?, ?)";
+ " (authorId, name, publicKey, localAuthorId, verified, active)"
+ " VALUES (?, ?, ?, ?, ?, ?)";
ps = txn.prepareStatement(sql);
ps.setBytes(1, remote.getId().getBytes());
ps.setString(2, remote.getName());
ps.setBytes(3, remote.getPublicKey());
ps.setBytes(4, local.getBytes());
ps.setBoolean(5, active);
ps.setBoolean(5, verified);
ps.setBoolean(6, active);
int affected = ps.executeUpdate();
if (affected != 1) throw new DbStateException();
ps.close();
@@ -961,7 +963,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ResultSet rs = null;
try {
String sql = "SELECT authorId, name, publicKey,"
+ " localAuthorId, active"
+ " localAuthorId, verified, active"
+ " FROM contacts"
+ " WHERE contactId = ?";
ps = txn.prepareStatement(sql);
@@ -972,11 +974,12 @@ abstract class JdbcDatabase implements Database<Connection> {
String name = rs.getString(2);
byte[] publicKey = rs.getBytes(3);
AuthorId localAuthorId = new AuthorId(rs.getBytes(4));
boolean active = rs.getBoolean(5);
boolean verified = rs.getBoolean(5);
boolean active = rs.getBoolean(6);
rs.close();
ps.close();
Author author = new Author(authorId, name, publicKey);
return new Contact(c, author, localAuthorId, active);
return new Contact(c, author, localAuthorId, verified, active);
} catch (SQLException e) {
tryToClose(rs);
tryToClose(ps);
@@ -990,7 +993,7 @@ abstract class JdbcDatabase implements Database<Connection> {
ResultSet rs = null;
try {
String sql = "SELECT contactId, authorId, name, publicKey,"
+ " localAuthorId, active"
+ " localAuthorId, verified, active"
+ " FROM contacts";
ps = txn.prepareStatement(sql);
rs = ps.executeQuery();
@@ -1002,9 +1005,10 @@ abstract class JdbcDatabase implements Database<Connection> {
byte[] publicKey = rs.getBytes(4);
Author author = new Author(authorId, name, publicKey);
AuthorId localAuthorId = new AuthorId(rs.getBytes(5));
boolean active = rs.getBoolean(6);
boolean verified = rs.getBoolean(6);
boolean active = rs.getBoolean(7);
contacts.add(new Contact(contactId, author, localAuthorId,
active));
verified, active));
}
rs.close();
ps.close();
@@ -2245,6 +2249,23 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public void setContactVerified(Connection txn, ContactId c,
boolean verified) throws DbException {
PreparedStatement ps = null;
try {
String sql = "UPDATE contacts SET verified = ? WHERE contactId = ?";
ps = txn.prepareStatement(sql);
ps.setBoolean(1, verified);
ps.setInt(2, c.getInt());
int affected = ps.executeUpdate();
if (affected < 0 || affected > 1) throw new DbStateException();
ps.close();
} catch (SQLException e) {
tryToClose(ps);
throw new DbException(e);
}
}
public void setContactActive(Connection txn, ContactId c, boolean active)
throws DbException {
PreparedStatement ps = null;