From c3a14d92754e5757e3af90ee3cd1685b2d5f720b Mon Sep 17 00:00:00 2001 From: Torsten Grote <t@grobox.de> Date: Fri, 19 Aug 2016 12:41:10 -0300 Subject: [PATCH] Add a new event that is broadcasted when a contact is verified Also, don't support unverifying contacts. --- .../briarproject/api/db/DatabaseComponent.java | 5 ++--- .../api/event/ContactVerifiedEvent.java | 18 ++++++++++++++++++ .../src/org/briarproject/db/Database.java | 5 ++--- .../briarproject/db/DatabaseComponentImpl.java | 9 +++++---- .../src/org/briarproject/db/JdbcDatabase.java | 6 +++--- 5 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 briar-api/src/org/briarproject/api/event/ContactVerifiedEvent.java diff --git a/briar-api/src/org/briarproject/api/db/DatabaseComponent.java b/briar-api/src/org/briarproject/api/db/DatabaseComponent.java index b5e6be934d..735f64a21f 100644 --- a/briar-api/src/org/briarproject/api/db/DatabaseComponent.java +++ b/briar-api/src/org/briarproject/api/db/DatabaseComponent.java @@ -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. diff --git a/briar-api/src/org/briarproject/api/event/ContactVerifiedEvent.java b/briar-api/src/org/briarproject/api/event/ContactVerifiedEvent.java new file mode 100644 index 0000000000..2507dee4ef --- /dev/null +++ b/briar-api/src/org/briarproject/api/event/ContactVerifiedEvent.java @@ -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; + } + +} diff --git a/briar-core/src/org/briarproject/db/Database.java b/briar-core/src/org/briarproject/db/Database.java index 2fa8ced868..b0be3b20bf 100644 --- a/briar-core/src/org/briarproject/db/Database.java +++ b/briar-core/src/org/briarproject/db/Database.java @@ -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. diff --git a/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java b/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java index e2ec256969..1039a69e07 100644 --- a/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java +++ b/briar-core/src/org/briarproject/db/DatabaseComponentImpl.java @@ -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, diff --git a/briar-core/src/org/briarproject/db/JdbcDatabase.java b/briar-core/src/org/briarproject/db/JdbcDatabase.java index 9e27e91b67..640bc019db 100644 --- a/briar-core/src/org/briarproject/db/JdbcDatabase.java +++ b/briar-core/src/org/briarproject/db/JdbcDatabase.java @@ -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(); -- GitLab