Mark local messages read when they're added to the database.

This commit is contained in:
akwizgran
2013-03-05 01:52:09 +00:00
parent 7cfdacb0ae
commit 666499337c
3 changed files with 57 additions and 50 deletions

View File

@@ -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) {

View File

@@ -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;

View File

@@ -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)));