Add a new event that is broadcasted when a contact is verified

Also, don't support unverifying contacts.
This commit is contained in:
Torsten Grote
2016-08-19 12:41:10 -03:00
parent e690bcb3cc
commit c3a14d9275
5 changed files with 30 additions and 13 deletions

View File

@@ -424,10 +424,9 @@ public interface DatabaseComponent {
void removeTransport(Transaction txn, TransportId t) throws DbException;
/**
* Marks the given contact as verified or unverified.
* Marks the given contact as verified.
*/
void setContactVerified(Transaction txn, ContactId c, boolean verified)
throws DbException;
void setContactVerified(Transaction txn, ContactId c) throws DbException;
/**
* Marks the given contact as active or inactive.

View File

@@ -0,0 +1,18 @@
package org.briarproject.api.event;
import org.briarproject.api.contact.ContactId;
/** An event that is broadcast when a contact is verified. */
public class ContactVerifiedEvent extends Event {
private final ContactId contactId;
public ContactVerifiedEvent(ContactId contactId) {
this.contactId = contactId;
}
public ContactId getContactId() {
return contactId;
}
}

View File

@@ -576,10 +576,9 @@ interface Database<T> {
void resetExpiryTime(T txn, ContactId c, MessageId m) throws DbException;
/**
* Marks the given contact as verified or unverified.
* Marks the given contact as verified.
*/
void setContactVerified(T txn, ContactId c, boolean verified)
throws DbException;
void setContactVerified(T txn, ContactId c) throws DbException;
/**
* Marks the given contact as active or inactive.

View File

@@ -17,6 +17,7 @@ import org.briarproject.api.db.Transaction;
import org.briarproject.api.event.ContactAddedEvent;
import org.briarproject.api.event.ContactRemovedEvent;
import org.briarproject.api.event.ContactStatusChangedEvent;
import org.briarproject.api.event.ContactVerifiedEvent;
import org.briarproject.api.event.Event;
import org.briarproject.api.event.EventBus;
import org.briarproject.api.event.GroupAddedEvent;
@@ -683,14 +684,14 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
db.removeTransport(txn, t);
}
public void setContactVerified(Transaction transaction, ContactId c,
boolean verified) throws DbException {
public void setContactVerified(Transaction transaction, ContactId c)
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));
db.setContactVerified(txn, c);
transaction.attach(new ContactVerifiedEvent(c));
}
public void setContactActive(Transaction transaction, ContactId c,

View File

@@ -2249,13 +2249,13 @@ abstract class JdbcDatabase implements Database<Connection> {
}
}
public void setContactVerified(Connection txn, ContactId c,
boolean verified) throws DbException {
public void setContactVerified(Connection txn, ContactId c)
throws DbException {
PreparedStatement ps = null;
try {
String sql = "UPDATE contacts SET verified = ? WHERE contactId = ?";
ps = txn.prepareStatement(sql);
ps.setBoolean(1, verified);
ps.setBoolean(1, true);
ps.setInt(2, c.getInt());
int affected = ps.executeUpdate();
if (affected < 0 || affected > 1) throw new DbStateException();