From 666499337cea11bb378c1ba935d37fc65f0ac06d Mon Sep 17 00:00:00 2001
From: akwizgran <michael@briarproject.org>
Date: Tue, 5 Mar 2013 01:52:09 +0000
Subject: [PATCH] Mark local messages read when they're added to the database.

---
 .../sf/briar/db/DatabaseComponentImpl.java    |  4 +-
 .../src/net/sf/briar/db/JdbcDatabase.java     | 98 +++++++++----------
 .../sf/briar/db/DatabaseComponentTest.java    |  5 +
 3 files changed, 57 insertions(+), 50 deletions(-)

diff --git a/briar-core/src/net/sf/briar/db/DatabaseComponentImpl.java b/briar-core/src/net/sf/briar/db/DatabaseComponentImpl.java
index 888acb4642..471f7306b2 100644
--- a/briar-core/src/net/sf/briar/db/DatabaseComponentImpl.java
+++ b/briar-core/src/net/sf/briar/db/DatabaseComponentImpl.java
@@ -294,12 +294,13 @@ DatabaseCleaner.Callback {
 	 * the sender and unseen by all other contacts, and returns true.
 	 * <p>
 	 * Locking: contact read, message write, rating read.
-	 * @param sender may be null for a locally generated message.
+	 * @param sender is null for a locally generated message.
 	 */
 	private boolean storeGroupMessage(T txn, Message m, ContactId sender)
 			throws DbException {
 		if(m.getGroup() == null) throw new IllegalArgumentException();
 		boolean stored = db.addGroupMessage(txn, m);
+		if(stored && sender == null) db.setReadFlag(txn, m.getId(), true);
 		// Mark the message as seen by the sender
 		MessageId id = m.getId();
 		if(sender != null) db.addStatus(txn, sender, id, true);
@@ -472,6 +473,7 @@ DatabaseCleaner.Callback {
 		if(m.getGroup() != null) throw new IllegalArgumentException();
 		if(m.getAuthor() != null) throw new IllegalArgumentException();
 		if(!db.addPrivateMessage(txn, m, c)) return false;
+		if(!incoming) db.setReadFlag(txn, m.getId(), true);
 		db.addStatus(txn, c, m.getId(), incoming);
 		// Count the bytes stored
 		synchronized(spaceLock) {
diff --git a/briar-core/src/net/sf/briar/db/JdbcDatabase.java b/briar-core/src/net/sf/briar/db/JdbcDatabase.java
index 4836072a9e..db2dc476a3 100644
--- a/briar-core/src/net/sf/briar/db/JdbcDatabase.java
+++ b/briar-core/src/net/sf/briar/db/JdbcDatabase.java
@@ -736,55 +736,6 @@ abstract class JdbcDatabase implements Database<Connection> {
 		}
 	}
 
-	public void addStatus(Connection txn, ContactId c, MessageId m,
-			boolean seen) throws DbException {
-		PreparedStatement ps = null;
-		try {
-			String sql = "INSERT INTO statuses"
-					+ " (messageId, contactId, seen, expiry, txCount)"
-					+ " VALUES (?, ?, ?, ZERO(), ZERO())";
-			ps = txn.prepareStatement(sql);
-			ps.setBytes(1, m.getBytes());
-			ps.setInt(2, c.getInt());
-			ps.setBoolean(3, seen);
-			int affected = ps.executeUpdate();
-			if(affected != 1) throw new DbStateException();
-			ps.close();
-		} catch(SQLException e) {
-			tryToClose(ps);
-			throw new DbException(e);
-		}
-	}
-
-	public boolean addSubscription(Connection txn, Group g) throws DbException {
-		PreparedStatement ps = null;
-		ResultSet rs = null;
-		try {
-			String sql = "SELECT COUNT (groupId) FROM groups";
-			ps = txn.prepareStatement(sql);
-			rs = ps.executeQuery();
-			if(!rs.next()) throw new DbStateException();
-			int count = rs.getInt(1);
-			if(rs.next()) throw new DbStateException();
-			rs.close();
-			ps.close();
-			if(count > MAX_SUBSCRIPTIONS) throw new DbStateException();
-			if(count == MAX_SUBSCRIPTIONS) return false;
-			sql = "INSERT INTO groups (groupId, name, key) VALUES (?, ?, ?)";
-			ps = txn.prepareStatement(sql);
-			ps.setBytes(1, g.getId().getBytes());
-			ps.setString(2, g.getName());
-			ps.setBytes(3, g.getPublicKey());
-			int affected = ps.executeUpdate();
-			if(affected != 1) throw new DbStateException();
-			ps.close();
-			return true;
-		} catch(SQLException e) {
-			tryToClose(ps);
-			throw new DbException(e);
-		}
-	}
-
 	public void addSecrets(Connection txn, Collection<TemporarySecret> secrets)
 			throws DbException {
 		PreparedStatement ps = null;
@@ -834,6 +785,55 @@ abstract class JdbcDatabase implements Database<Connection> {
 		}
 	}
 
+	public void addStatus(Connection txn, ContactId c, MessageId m,
+			boolean seen) throws DbException {
+		PreparedStatement ps = null;
+		try {
+			String sql = "INSERT INTO statuses"
+					+ " (messageId, contactId, seen, expiry, txCount)"
+					+ " VALUES (?, ?, ?, ZERO(), ZERO())";
+			ps = txn.prepareStatement(sql);
+			ps.setBytes(1, m.getBytes());
+			ps.setInt(2, c.getInt());
+			ps.setBoolean(3, seen);
+			int affected = ps.executeUpdate();
+			if(affected != 1) throw new DbStateException();
+			ps.close();
+		} catch(SQLException e) {
+			tryToClose(ps);
+			throw new DbException(e);
+		}
+	}
+
+	public boolean addSubscription(Connection txn, Group g) throws DbException {
+		PreparedStatement ps = null;
+		ResultSet rs = null;
+		try {
+			String sql = "SELECT COUNT (groupId) FROM groups";
+			ps = txn.prepareStatement(sql);
+			rs = ps.executeQuery();
+			if(!rs.next()) throw new DbStateException();
+			int count = rs.getInt(1);
+			if(rs.next()) throw new DbStateException();
+			rs.close();
+			ps.close();
+			if(count > MAX_SUBSCRIPTIONS) throw new DbStateException();
+			if(count == MAX_SUBSCRIPTIONS) return false;
+			sql = "INSERT INTO groups (groupId, name, key) VALUES (?, ?, ?)";
+			ps = txn.prepareStatement(sql);
+			ps.setBytes(1, g.getId().getBytes());
+			ps.setString(2, g.getName());
+			ps.setBytes(3, g.getPublicKey());
+			int affected = ps.executeUpdate();
+			if(affected != 1) throw new DbStateException();
+			ps.close();
+			return true;
+		} catch(SQLException e) {
+			tryToClose(ps);
+			throw new DbException(e);
+		}
+	}
+
 	public boolean addTransport(Connection txn, TransportId t)
 			throws DbException {
 		PreparedStatement ps = null;
diff --git a/briar-tests/src/net/sf/briar/db/DatabaseComponentTest.java b/briar-tests/src/net/sf/briar/db/DatabaseComponentTest.java
index 72958c9de3..5a585bac33 100644
--- a/briar-tests/src/net/sf/briar/db/DatabaseComponentTest.java
+++ b/briar-tests/src/net/sf/briar/db/DatabaseComponentTest.java
@@ -375,6 +375,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
 			will(returnValue(true));
 			oneOf(database).addGroupMessage(txn, message);
 			will(returnValue(true));
+			oneOf(database).setReadFlag(txn, messageId, true);
 			oneOf(database).getContactIds(txn);
 			will(returnValue(Arrays.asList(contactId)));
 			oneOf(database).addStatus(txn, contactId, messageId, false);
@@ -410,6 +411,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
 			will(returnValue(true));
 			oneOf(database).addGroupMessage(txn, message);
 			will(returnValue(true));
+			oneOf(database).setReadFlag(txn, messageId, true);
 			oneOf(database).getContactIds(txn);
 			will(returnValue(Arrays.asList(contactId)));
 			oneOf(database).addStatus(txn, contactId, messageId, false);
@@ -473,6 +475,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
 			// addLocalPrivateMessage(privateMessage, contactId)
 			oneOf(database).addPrivateMessage(txn, privateMessage, contactId);
 			will(returnValue(true));
+			oneOf(database).setReadFlag(txn, messageId, true);
 			oneOf(database).addStatus(txn, contactId, messageId, false);
 		}});
 		DatabaseComponent db = createDatabaseComponent(database, cleaner,
@@ -1433,6 +1436,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
 			will(returnValue(true));
 			oneOf(database).addGroupMessage(txn, message);
 			will(returnValue(true));
+			oneOf(database).setReadFlag(txn, messageId, true);
 			oneOf(database).getContactIds(txn);
 			will(returnValue(Arrays.asList(contactId)));
 			oneOf(database).addStatus(txn, contactId, messageId, false);
@@ -1471,6 +1475,7 @@ public abstract class DatabaseComponentTest extends BriarTestCase {
 			// addLocalPrivateMessage(privateMessage, contactId)
 			oneOf(database).addPrivateMessage(txn, privateMessage, contactId);
 			will(returnValue(true));
+			oneOf(database).setReadFlag(txn, messageId, true);
 			oneOf(database).addStatus(txn, contactId, messageId, false);
 			// The message was added, so the listener should be called
 			oneOf(listener).eventOccurred(with(any(MessageAddedEvent.class)));
-- 
GitLab