mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-16 04:39:54 +01:00
Check for sendable private messages as well as group messages.
This commit is contained in:
@@ -264,9 +264,8 @@ interface Database<T> {
|
|||||||
Rating getRating(T txn, AuthorId a) throws DbException;
|
Rating getRating(T txn, AuthorId a) throws DbException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the sendability score of the given message. Messages with
|
* Returns the sendability score of the given message. Group messages with
|
||||||
* sendability scores greater than zero are eligible to be sent to
|
* sendability scores greater than zero are eligible to be sent to contacts.
|
||||||
* contacts.
|
|
||||||
* <p>
|
* <p>
|
||||||
* Locking: messages read.
|
* Locking: messages read.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -936,26 +936,47 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
try {
|
try {
|
||||||
|
// Do we have a sendable private message with the given ID?
|
||||||
String sql = "SELECT size, raw FROM messages"
|
String sql = "SELECT size, raw FROM messages"
|
||||||
|
+ " JOIN statuses ON messages.messageId = statuses.messageId"
|
||||||
|
+ " WHERE messages.messageId = ? AND messages.contactId = ?"
|
||||||
|
+ " AND status = ?";
|
||||||
|
ps = txn.prepareStatement(sql);
|
||||||
|
ps.setBytes(1, m.getBytes());
|
||||||
|
ps.setInt(2, c.getInt());
|
||||||
|
ps.setShort(3, (short) Status.NEW.ordinal());
|
||||||
|
rs = ps.executeQuery();
|
||||||
|
byte[] raw = null;
|
||||||
|
if(rs.next()) {
|
||||||
|
int size = rs.getInt(1);
|
||||||
|
Blob b = rs.getBlob(2);
|
||||||
|
raw = b.getBytes(1, size);
|
||||||
|
if(raw.length != size) throw new DbStateException();
|
||||||
|
}
|
||||||
|
if(rs.next()) throw new DbStateException();
|
||||||
|
rs.close();
|
||||||
|
ps.close();
|
||||||
|
if(raw != null) return raw;
|
||||||
|
// Do we have a sendable group message with the given ID?
|
||||||
|
sql = "SELECT size, raw FROM messages"
|
||||||
+ " JOIN contactSubscriptions"
|
+ " JOIN contactSubscriptions"
|
||||||
+ " ON messages.groupId = contactSubscriptions.groupId"
|
+ " ON messages.groupId = contactSubscriptions.groupId"
|
||||||
+ " JOIN visibilities"
|
+ " JOIN visibilities"
|
||||||
+ " ON messages.groupId = visibilities.groupId"
|
+ " ON messages.groupId = visibilities.groupId"
|
||||||
+ " JOIN statuses ON messages.messageId = statuses.messageId"
|
+ " AND contactSubscriptions.contactId = visibilities.contactId"
|
||||||
|
+ " JOIN statuses"
|
||||||
|
+ " ON messages.messageId = statuses.messageId"
|
||||||
|
+ " AND contactSubscriptions.contactId = statuses.contactId"
|
||||||
+ " WHERE messages.messageId = ?"
|
+ " WHERE messages.messageId = ?"
|
||||||
+ " AND contactSubscriptions.contactId = ?"
|
+ " AND contactSubscriptions.contactId = ?"
|
||||||
+ " AND visibilities.contactId = ?"
|
|
||||||
+ " AND statuses.contactId = ?"
|
|
||||||
+ " AND timestamp >= start"
|
+ " AND timestamp >= start"
|
||||||
+ " AND status = ? AND sendability > ZERO()";
|
+ " AND status = ?"
|
||||||
|
+ " AND sendability > ZERO()";
|
||||||
ps = txn.prepareStatement(sql);
|
ps = txn.prepareStatement(sql);
|
||||||
ps.setBytes(1, m.getBytes());
|
ps.setBytes(1, m.getBytes());
|
||||||
ps.setInt(2, c.getInt());
|
ps.setInt(2, c.getInt());
|
||||||
ps.setInt(3, c.getInt());
|
ps.setShort(3, (short) Status.NEW.ordinal());
|
||||||
ps.setInt(4, c.getInt());
|
|
||||||
ps.setShort(5, (short) Status.NEW.ordinal());
|
|
||||||
rs = ps.executeQuery();
|
rs = ps.executeQuery();
|
||||||
byte[] raw = null;
|
|
||||||
if(rs.next()) {
|
if(rs.next()) {
|
||||||
int size = rs.getInt(1);
|
int size = rs.getInt(1);
|
||||||
Blob b = rs.getBlob(2);
|
Blob b = rs.getBlob(2);
|
||||||
@@ -1091,7 +1112,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
if(rs.next()) throw new DbStateException();
|
if(rs.next()) throw new DbStateException();
|
||||||
rs.close();
|
rs.close();
|
||||||
ps.close();
|
ps.close();
|
||||||
return new MessageId(parent);
|
return parent == null ? null : new MessageId(parent);
|
||||||
} catch(SQLException e) {
|
} catch(SQLException e) {
|
||||||
tryToClose(rs);
|
tryToClose(rs);
|
||||||
tryToClose(ps);
|
tryToClose(ps);
|
||||||
@@ -1169,23 +1190,14 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
try {
|
try {
|
||||||
|
// Do we have any sendable private messages?
|
||||||
String sql = "SELECT size, messages.messageId FROM messages"
|
String sql = "SELECT size, messages.messageId FROM messages"
|
||||||
+ " JOIN contactSubscriptions"
|
|
||||||
+ " ON messages.groupId = contactSubscriptions.groupId"
|
|
||||||
+ " JOIN visibilities"
|
|
||||||
+ " ON messages.groupId = visibilities.groupId"
|
|
||||||
+ " JOIN statuses ON messages.messageId = statuses.messageId"
|
+ " JOIN statuses ON messages.messageId = statuses.messageId"
|
||||||
+ " WHERE contactSubscriptions.contactId = ?"
|
+ " WHERE messages.contactId = ? AND status = ?"
|
||||||
+ " AND visibilities.contactId = ?"
|
|
||||||
+ " AND statuses.contactId = ?"
|
|
||||||
+ " AND timestamp >= start"
|
|
||||||
+ " AND status = ? AND sendability > ZERO()"
|
|
||||||
+ " ORDER BY timestamp";
|
+ " ORDER BY timestamp";
|
||||||
ps = txn.prepareStatement(sql);
|
ps = txn.prepareStatement(sql);
|
||||||
ps.setInt(1, c.getInt());
|
ps.setInt(1, c.getInt());
|
||||||
ps.setInt(2, c.getInt());
|
ps.setShort(2, (short) Status.NEW.ordinal());
|
||||||
ps.setInt(3, c.getInt());
|
|
||||||
ps.setShort(4, (short) Status.NEW.ordinal());
|
|
||||||
rs = ps.executeQuery();
|
rs = ps.executeQuery();
|
||||||
Collection<MessageId> ids = new ArrayList<MessageId>();
|
Collection<MessageId> ids = new ArrayList<MessageId>();
|
||||||
int total = 0;
|
int total = 0;
|
||||||
@@ -1198,8 +1210,39 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
rs.close();
|
rs.close();
|
||||||
ps.close();
|
ps.close();
|
||||||
if(LOG.isLoggable(Level.FINE))
|
if(LOG.isLoggable(Level.FINE))
|
||||||
LOG.fine(ids.size() + " sendable messages, " + total + "/" +
|
LOG.fine(ids.size() + " sendable private messages, " +
|
||||||
capacity + " bytes");
|
total + "/" + capacity + " bytes");
|
||||||
|
if(total == capacity) return ids;
|
||||||
|
// Do we have any sendable group messages?
|
||||||
|
sql = "SELECT size, messages.messageId FROM messages"
|
||||||
|
+ " JOIN contactSubscriptions"
|
||||||
|
+ " ON messages.groupId = contactSubscriptions.groupId"
|
||||||
|
+ " JOIN visibilities"
|
||||||
|
+ " ON messages.groupId = visibilities.groupId"
|
||||||
|
+ " AND contactSubscriptions.contactId = visibilities.contactId"
|
||||||
|
+ " JOIN statuses"
|
||||||
|
+ " ON messages.messageId = statuses.messageId"
|
||||||
|
+ " AND contactSubscriptions.contactId = statuses.contactId"
|
||||||
|
+ " WHERE contactSubscriptions.contactId = ?"
|
||||||
|
+ " AND timestamp >= start"
|
||||||
|
+ " AND status = ?"
|
||||||
|
+ " AND sendability > ZERO()"
|
||||||
|
+ " ORDER BY timestamp";
|
||||||
|
ps = txn.prepareStatement(sql);
|
||||||
|
ps.setInt(1, c.getInt());
|
||||||
|
ps.setShort(2, (short) Status.NEW.ordinal());
|
||||||
|
rs = ps.executeQuery();
|
||||||
|
while(rs.next()) {
|
||||||
|
int size = rs.getInt(1);
|
||||||
|
if(total + size > capacity) break;
|
||||||
|
ids.add(new MessageId(rs.getBytes(2)));
|
||||||
|
total += size;
|
||||||
|
}
|
||||||
|
rs.close();
|
||||||
|
ps.close();
|
||||||
|
if(LOG.isLoggable(Level.FINE))
|
||||||
|
LOG.fine(ids.size() + " sendable private and group messages, " +
|
||||||
|
total + "/" + capacity + " bytes");
|
||||||
return ids;
|
return ids;
|
||||||
} catch(SQLException e) {
|
} catch(SQLException e) {
|
||||||
tryToClose(rs);
|
tryToClose(rs);
|
||||||
|
|||||||
@@ -220,7 +220,6 @@ public class H2DatabaseTest extends TestCase {
|
|||||||
db.setStatus(txn, contactId, messageId, Status.NEW);
|
db.setStatus(txn, contactId, messageId, Status.NEW);
|
||||||
|
|
||||||
// The message should not be sendable
|
// The message should not be sendable
|
||||||
assertEquals(0, db.getSendability(txn, messageId));
|
|
||||||
assertFalse(db.hasSendableMessages(txn, contactId));
|
assertFalse(db.hasSendableMessages(txn, contactId));
|
||||||
Iterator<MessageId> it =
|
Iterator<MessageId> it =
|
||||||
db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator();
|
db.getSendableMessages(txn, contactId, ONE_MEGABYTE).iterator();
|
||||||
|
|||||||
Reference in New Issue
Block a user