From e2d64e0a8c0bf2c5283f4c9e49ac95c4c6f34777 Mon Sep 17 00:00:00 2001
From: Torsten Grote <t@grobox.de>
Date: Mon, 29 Feb 2016 15:29:18 -0300
Subject: [PATCH] allow adding contacts within an existing transactions

---
 .../api/contact/ContactManager.java           | 13 +++++++++++--
 .../contact/ContactManagerImpl.java           | 19 ++++++++++++++-----
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/briar-api/src/org/briarproject/api/contact/ContactManager.java b/briar-api/src/org/briarproject/api/contact/ContactManager.java
index ba1d2c6630..183378060f 100644
--- a/briar-api/src/org/briarproject/api/contact/ContactManager.java
+++ b/briar-api/src/org/briarproject/api/contact/ContactManager.java
@@ -16,12 +16,21 @@ public interface ContactManager {
 	/** Registers a hook to be called whenever a contact is removed. */
 	void registerRemoveContactHook(RemoveContactHook hook);
 
+	/**
+	 * Stores a contact within the given transaction associated with the given
+	 * local and remote pseudonyms, and returns an ID for the contact.
+	 */
+	ContactId addContact(Transaction txn, Author remote, AuthorId local,
+			SecretKey master, long timestamp, boolean alice, boolean active)
+			throws DbException;
+
 	/**
 	 * Stores a contact associated with the given local and remote pseudonyms,
 	 * and returns an ID for the contact.
 	 */
-	ContactId addContact(Author remote, AuthorId local, SecretKey master,
-			long timestamp, boolean alice, boolean active) throws DbException;
+	ContactId addContact(Author remote, AuthorId local,
+			SecretKey master, long timestamp, boolean alice, boolean active)
+			throws DbException;
 
 	/** Returns the contact with the given ID. */
 	Contact getContact(ContactId c) throws DbException;
diff --git a/briar-core/src/org/briarproject/contact/ContactManagerImpl.java b/briar-core/src/org/briarproject/contact/ContactManagerImpl.java
index 7ff709b70c..e04878d43f 100644
--- a/briar-core/src/org/briarproject/contact/ContactManagerImpl.java
+++ b/briar-core/src/org/briarproject/contact/ContactManagerImpl.java
@@ -46,6 +46,18 @@ class ContactManagerImpl implements ContactManager, RemoveIdentityHook {
 		removeHooks.add(hook);
 	}
 
+	@Override
+	public ContactId addContact(Transaction txn, Author remote, AuthorId local,
+			SecretKey master,long timestamp, boolean alice, boolean active)
+			throws DbException {
+		ContactId c = db.addContact(txn, remote, local, active);
+		keyManager.addContact(txn, c, master, timestamp, alice);
+		Contact contact = db.getContact(txn, c);
+		for (AddContactHook hook : addHooks)
+			hook.addingContact(txn, contact);
+		return c;
+	}
+
 	@Override
 	public ContactId addContact(Author remote, AuthorId local, SecretKey master,
 			long timestamp, boolean alice, boolean active)
@@ -53,11 +65,8 @@ class ContactManagerImpl implements ContactManager, RemoveIdentityHook {
 		ContactId c;
 		Transaction txn = db.startTransaction(false);
 		try {
-			c = db.addContact(txn, remote, local, active);
-			keyManager.addContact(txn, c, master, timestamp, alice);
-			Contact contact = db.getContact(txn, c);
-			for (AddContactHook hook : addHooks)
-				hook.addingContact(txn, contact);
+			c = addContact(txn, remote, local, master, timestamp, alice,
+					active);
 			txn.setComplete();
 		} finally {
 			db.endTransaction(txn);
-- 
GitLab