mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 12:49:55 +01:00
Added active flag to contacts.
This commit is contained in:
@@ -13,7 +13,9 @@ import org.briarproject.api.identity.AuthorId;
|
||||
import org.briarproject.api.identity.IdentityManager.RemoveIdentityHook;
|
||||
import org.briarproject.api.identity.LocalAuthor;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
@@ -41,12 +43,12 @@ class ContactManagerImpl implements ContactManager, RemoveIdentityHook {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ContactId addContact(Author remote, AuthorId local)
|
||||
public ContactId addContact(Author remote, AuthorId local, boolean active)
|
||||
throws DbException {
|
||||
ContactId c;
|
||||
Transaction txn = db.startTransaction();
|
||||
try {
|
||||
c = db.addContact(txn, remote, local);
|
||||
c = db.addContact(txn, remote, local, active);
|
||||
Contact contact = db.getContact(txn, c);
|
||||
for (AddContactHook hook : addHooks)
|
||||
hook.addingContact(txn, contact);
|
||||
@@ -71,7 +73,7 @@ class ContactManagerImpl implements ContactManager, RemoveIdentityHook {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Contact> getContacts() throws DbException {
|
||||
public Collection<Contact> getActiveContacts() throws DbException {
|
||||
Collection<Contact> contacts;
|
||||
Transaction txn = db.startTransaction();
|
||||
try {
|
||||
@@ -80,7 +82,9 @@ class ContactManagerImpl implements ContactManager, RemoveIdentityHook {
|
||||
} finally {
|
||||
db.endTransaction(txn);
|
||||
}
|
||||
return contacts;
|
||||
List<Contact> active = new ArrayList<Contact>(contacts.size());
|
||||
for (Contact c : contacts) if (c.isActive()) active.add(c);
|
||||
return Collections.unmodifiableList(active);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -94,6 +98,18 @@ class ContactManagerImpl implements ContactManager, RemoveIdentityHook {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContactActive(ContactId c, boolean active)
|
||||
throws DbException {
|
||||
Transaction txn = db.startTransaction();
|
||||
try {
|
||||
db.setContactActive(txn, c, active);
|
||||
txn.setComplete();
|
||||
} finally {
|
||||
db.endTransaction(txn);
|
||||
}
|
||||
}
|
||||
|
||||
private void removeContact(Transaction txn, ContactId c)
|
||||
throws DbException {
|
||||
Contact contact = db.getContact(txn, c);
|
||||
|
||||
@@ -64,7 +64,7 @@ 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)
|
||||
ContactId addContact(T txn, Author remote, AuthorId local, boolean active)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
@@ -446,6 +446,12 @@ interface Database<T> {
|
||||
*/
|
||||
void resetExpiryTime(T txn, ContactId c, MessageId m) throws DbException;
|
||||
|
||||
/**
|
||||
* Marks the given contact as active or inactive.
|
||||
*/
|
||||
void setContactActive(T txn, ContactId c, boolean active)
|
||||
throws DbException;
|
||||
|
||||
/**
|
||||
* Marks the given message as shared or unshared.
|
||||
*/
|
||||
|
||||
@@ -16,6 +16,7 @@ import org.briarproject.api.db.NoSuchTransportException;
|
||||
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.Event;
|
||||
import org.briarproject.api.event.EventBus;
|
||||
import org.briarproject.api.event.GroupAddedEvent;
|
||||
@@ -136,13 +137,13 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
}
|
||||
|
||||
public ContactId addContact(Transaction transaction, Author remote,
|
||||
AuthorId local) throws DbException {
|
||||
AuthorId local, boolean active) throws DbException {
|
||||
T txn = unbox(transaction);
|
||||
if (!db.containsLocalAuthor(txn, local))
|
||||
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, active);
|
||||
transaction.attach(new ContactAddedEvent(c));
|
||||
return c;
|
||||
}
|
||||
@@ -580,6 +581,15 @@ class DatabaseComponentImpl<T> implements DatabaseComponent {
|
||||
transaction.attach(new TransportRemovedEvent(t));
|
||||
}
|
||||
|
||||
public void setContactActive(Transaction transaction, ContactId c,
|
||||
boolean active) throws DbException {
|
||||
T txn = unbox(transaction);
|
||||
if (!db.containsContact(txn, c))
|
||||
throw new NoSuchContactException();
|
||||
db.setContactActive(txn, c, active);
|
||||
transaction.attach(new ContactStatusChangedEvent(c, active));
|
||||
}
|
||||
|
||||
public void setMessageShared(Transaction transaction, Message m,
|
||||
boolean shared) throws DbException {
|
||||
T txn = unbox(transaction);
|
||||
|
||||
@@ -89,6 +89,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
+ " name VARCHAR NOT NULL,"
|
||||
+ " publicKey BINARY NOT NULL,"
|
||||
+ " localAuthorId HASH NOT NULL,"
|
||||
+ " active BOOLEAN NOT NULL,"
|
||||
+ " PRIMARY KEY (contactId),"
|
||||
+ " FOREIGN KEY (localAuthorId)"
|
||||
+ " REFERENCES localAuthors (authorId)"
|
||||
@@ -441,20 +442,21 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
return new DeviceId(StringUtils.fromHexString(s.get(DEVICE_ID_KEY)));
|
||||
}
|
||||
|
||||
public ContactId addContact(Connection txn, Author remote, AuthorId local)
|
||||
throws DbException {
|
||||
public ContactId addContact(Connection txn, Author remote, AuthorId local,
|
||||
boolean active) throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
// Create a contact row
|
||||
String sql = "INSERT INTO contacts"
|
||||
+ " (authorId, name, publicKey, localAuthorId)"
|
||||
+ " VALUES (?, ?, ?, ?)";
|
||||
+ " (authorId, name, publicKey, localAuthorId, 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);
|
||||
int affected = ps.executeUpdate();
|
||||
if (affected != 1) throw new DbStateException();
|
||||
ps.close();
|
||||
@@ -926,7 +928,8 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql = "SELECT authorId, name, publicKey, localAuthorId"
|
||||
String sql = "SELECT authorId, name, publicKey,"
|
||||
+ " localAuthorId, active"
|
||||
+ " FROM contacts"
|
||||
+ " WHERE contactId = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
@@ -937,10 +940,11 @@ 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);
|
||||
rs.close();
|
||||
ps.close();
|
||||
Author author = new Author(authorId, name, publicKey);
|
||||
return new Contact(c, author, localAuthorId);
|
||||
return new Contact(c, author, localAuthorId, active);
|
||||
} catch (SQLException e) {
|
||||
tryToClose(rs);
|
||||
tryToClose(ps);
|
||||
@@ -954,7 +958,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
String sql = "SELECT contactId, authorId, name, publicKey,"
|
||||
+ " localAuthorId"
|
||||
+ " localAuthorId, active"
|
||||
+ " FROM contacts";
|
||||
ps = txn.prepareStatement(sql);
|
||||
rs = ps.executeQuery();
|
||||
@@ -966,7 +970,9 @@ 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));
|
||||
contacts.add(new Contact(contactId, author, localAuthorId));
|
||||
boolean active = rs.getBoolean(6);
|
||||
contacts.add(new Contact(contactId, author, localAuthorId,
|
||||
active));
|
||||
}
|
||||
rs.close();
|
||||
ps.close();
|
||||
@@ -2013,6 +2019,23 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
}
|
||||
}
|
||||
|
||||
public void setContactActive(Connection txn, ContactId c, boolean active)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
try {
|
||||
String sql = "UPDATE contacts SET active = ? WHERE contactId = ?";
|
||||
ps = txn.prepareStatement(sql);
|
||||
ps.setBoolean(1, active);
|
||||
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 setMessageShared(Connection txn, MessageId m, boolean shared)
|
||||
throws DbException {
|
||||
PreparedStatement ps = null;
|
||||
@@ -2022,7 +2045,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
ps.setBoolean(1, shared);
|
||||
ps.setBytes(2, m.getBytes());
|
||||
int affected = ps.executeUpdate();
|
||||
if (affected < 0) throw new DbStateException();
|
||||
if (affected < 0 || affected > 1) throw new DbStateException();
|
||||
ps.close();
|
||||
} catch (SQLException e) {
|
||||
tryToClose(ps);
|
||||
@@ -2039,7 +2062,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
||||
ps.setInt(1, valid ? VALID.getValue() : INVALID.getValue());
|
||||
ps.setBytes(2, m.getBytes());
|
||||
int affected = ps.executeUpdate();
|
||||
if (affected < 0) throw new DbStateException();
|
||||
if (affected < 0 || affected > 1) throw new DbStateException();
|
||||
ps.close();
|
||||
} catch (SQLException e) {
|
||||
tryToClose(ps);
|
||||
|
||||
@@ -219,7 +219,7 @@ abstract class Connector extends Thread {
|
||||
long timestamp, boolean alice) throws DbException {
|
||||
// Add the contact to the database
|
||||
contactId = contactManager.addContact(remoteAuthor,
|
||||
localAuthor.getId());
|
||||
localAuthor.getId(), true);
|
||||
// Derive transport keys
|
||||
keyManager.addContact(contactId, master, timestamp, alice);
|
||||
return contactId;
|
||||
|
||||
Reference in New Issue
Block a user