mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-18 05:39:53 +01:00
Sort forum messages correctly even if clocks are wrong. Bug #57.
This commit is contained in:
@@ -387,13 +387,8 @@ implements EventListener, OnClickListener, OnItemClickListener {
|
|||||||
public void onClick(View view) {
|
public void onClick(View view) {
|
||||||
String message = content.getText().toString();
|
String message = content.getText().toString();
|
||||||
if(message.equals("")) return;
|
if(message.equals("")) return;
|
||||||
// Don't use an earlier timestamp than the newest message
|
|
||||||
long timestamp = System.currentTimeMillis();
|
long timestamp = System.currentTimeMillis();
|
||||||
int count = adapter.getCount();
|
timestamp = Math.max(timestamp, getMinTimestampForNewMessage());
|
||||||
for(int i = 0; i < count; i++) {
|
|
||||||
long time = adapter.getItem(i).getHeader().getTimestamp() + 1;
|
|
||||||
if(time > timestamp) timestamp = time;
|
|
||||||
}
|
|
||||||
createMessage(StringUtils.toUtf8(message), timestamp);
|
createMessage(StringUtils.toUtf8(message), timestamp);
|
||||||
Toast.makeText(this, R.string.message_sent_toast, LENGTH_SHORT).show();
|
Toast.makeText(this, R.string.message_sent_toast, LENGTH_SHORT).show();
|
||||||
content.setText("");
|
content.setText("");
|
||||||
@@ -402,6 +397,17 @@ implements EventListener, OnClickListener, OnItemClickListener {
|
|||||||
((InputMethodManager) o).toggleSoftInput(HIDE_IMPLICIT_ONLY, 0);
|
((InputMethodManager) o).toggleSoftInput(HIDE_IMPLICIT_ONLY, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private long getMinTimestampForNewMessage() {
|
||||||
|
// Don't use an earlier timestamp than the newest message
|
||||||
|
long timestamp = 0;
|
||||||
|
int count = adapter.getCount();
|
||||||
|
for(int i = 0; i < count; i++) {
|
||||||
|
long t = adapter.getItem(i).getHeader().getTimestamp();
|
||||||
|
if(t > timestamp) timestamp = t;
|
||||||
|
}
|
||||||
|
return timestamp + 1;
|
||||||
|
}
|
||||||
|
|
||||||
private void createMessage(final byte[] body, final long timestamp) {
|
private void createMessage(final byte[] body, final long timestamp) {
|
||||||
cryptoExecutor.execute(new Runnable() {
|
cryptoExecutor.execute(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
@@ -452,6 +458,7 @@ implements EventListener, OnClickListener, OnItemClickListener {
|
|||||||
i.putExtra("briar.MESSAGE_ID", header.getId().getBytes());
|
i.putExtra("briar.MESSAGE_ID", header.getId().getBytes());
|
||||||
i.putExtra("briar.CONTENT_TYPE", header.getContentType());
|
i.putExtra("briar.CONTENT_TYPE", header.getContentType());
|
||||||
i.putExtra("briar.TIMESTAMP", header.getTimestamp());
|
i.putExtra("briar.TIMESTAMP", header.getTimestamp());
|
||||||
|
i.putExtra("briar.MIN_TIMESTAMP", getMinTimestampForNewMessage());
|
||||||
i.putExtra("briar.POSITION", position);
|
i.putExtra("briar.POSITION", position);
|
||||||
startActivityForResult(i, REQUEST_READ);
|
startActivityForResult(i, REQUEST_READ);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ implements OnClickListener {
|
|||||||
|
|
||||||
private String contactName = null;
|
private String contactName = null;
|
||||||
private AuthorId localAuthorId = null;
|
private AuthorId localAuthorId = null;
|
||||||
|
private long timestamp = -1, minTimestamp = -1;
|
||||||
private ImageButton prevButton = null, nextButton = null;
|
private ImageButton prevButton = null, nextButton = null;
|
||||||
private ImageButton replyButton = null;
|
private ImageButton replyButton = null;
|
||||||
private TextView content = null;
|
private TextView content = null;
|
||||||
@@ -60,7 +61,6 @@ implements OnClickListener {
|
|||||||
@Inject private volatile DatabaseComponent db;
|
@Inject private volatile DatabaseComponent db;
|
||||||
private volatile MessageId messageId = null;
|
private volatile MessageId messageId = null;
|
||||||
private volatile GroupId groupId = null;
|
private volatile GroupId groupId = null;
|
||||||
private volatile long timestamp = -1;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle state) {
|
public void onCreate(Bundle state) {
|
||||||
@@ -85,6 +85,8 @@ implements OnClickListener {
|
|||||||
if(contentType == null) throw new IllegalStateException();
|
if(contentType == null) throw new IllegalStateException();
|
||||||
timestamp = i.getLongExtra("briar.TIMESTAMP", -1);
|
timestamp = i.getLongExtra("briar.TIMESTAMP", -1);
|
||||||
if(timestamp == -1) throw new IllegalStateException();
|
if(timestamp == -1) throw new IllegalStateException();
|
||||||
|
minTimestamp = i.getLongExtra("briar.MIN_TIMESTAMP", -1);
|
||||||
|
if(minTimestamp == -1) throw new IllegalStateException();
|
||||||
position = i.getIntExtra("briar.POSITION", -1);
|
position = i.getIntExtra("briar.POSITION", -1);
|
||||||
if(position == -1) throw new IllegalStateException();
|
if(position == -1) throw new IllegalStateException();
|
||||||
|
|
||||||
@@ -228,7 +230,7 @@ implements OnClickListener {
|
|||||||
i.putExtra("briar.LOCAL_AUTHOR_ID",
|
i.putExtra("briar.LOCAL_AUTHOR_ID",
|
||||||
localAuthorId.getBytes());
|
localAuthorId.getBytes());
|
||||||
i.putExtra("briar.PARENT_ID", messageId.getBytes());
|
i.putExtra("briar.PARENT_ID", messageId.getBytes());
|
||||||
i.putExtra("briar.TIMESTAMP", timestamp);
|
i.putExtra("briar.MIN_TIMESTAMP", minTimestamp);
|
||||||
startActivity(i);
|
startActivity(i);
|
||||||
setResult(RESULT_REPLY);
|
setResult(RESULT_REPLY);
|
||||||
finish();
|
finish();
|
||||||
|
|||||||
@@ -69,7 +69,7 @@ implements OnClickListener {
|
|||||||
private volatile GroupId groupId = null;
|
private volatile GroupId groupId = null;
|
||||||
private volatile AuthorId localAuthorId = null;
|
private volatile AuthorId localAuthorId = null;
|
||||||
private volatile MessageId parentId = null;
|
private volatile MessageId parentId = null;
|
||||||
private volatile long timestamp = -1;
|
private volatile long minTimestamp = -1;
|
||||||
private volatile LocalAuthor localAuthor = null;
|
private volatile LocalAuthor localAuthor = null;
|
||||||
private volatile Group group = null;
|
private volatile Group group = null;
|
||||||
|
|
||||||
@@ -86,10 +86,11 @@ implements OnClickListener {
|
|||||||
groupId = new GroupId(b);
|
groupId = new GroupId(b);
|
||||||
b = i.getByteArrayExtra("briar.LOCAL_AUTHOR_ID");
|
b = i.getByteArrayExtra("briar.LOCAL_AUTHOR_ID");
|
||||||
if(b == null) throw new IllegalStateException();
|
if(b == null) throw new IllegalStateException();
|
||||||
|
minTimestamp = i.getLongExtra("briar.MIN_TIMESTAMP", -1);
|
||||||
|
if(minTimestamp == -1) throw new IllegalStateException();
|
||||||
localAuthorId = new AuthorId(b);
|
localAuthorId = new AuthorId(b);
|
||||||
b = i.getByteArrayExtra("briar.PARENT_ID");
|
b = i.getByteArrayExtra("briar.PARENT_ID");
|
||||||
if(b != null) parentId = new MessageId(b);
|
if(b != null) parentId = new MessageId(b);
|
||||||
timestamp = i.getLongExtra("briar.TIMESTAMP", -1);
|
|
||||||
|
|
||||||
LinearLayout layout = new LinearLayout(this);
|
LinearLayout layout = new LinearLayout(this);
|
||||||
layout.setLayoutParams(MATCH_WRAP);
|
layout.setLayoutParams(MATCH_WRAP);
|
||||||
@@ -187,12 +188,12 @@ implements OnClickListener {
|
|||||||
private void createMessage(final byte[] body) {
|
private void createMessage(final byte[] body) {
|
||||||
cryptoExecutor.execute(new Runnable() {
|
cryptoExecutor.execute(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
// Don't use an earlier timestamp than the parent
|
// Don't use an earlier timestamp than the newest message
|
||||||
long time = System.currentTimeMillis();
|
long timestamp = System.currentTimeMillis();
|
||||||
time = Math.max(time, timestamp + 1);
|
timestamp = Math.max(timestamp, minTimestamp);
|
||||||
try {
|
try {
|
||||||
Message m = messageFactory.createAnonymousMessage(parentId,
|
Message m = messageFactory.createAnonymousMessage(parentId,
|
||||||
group, "text/plain", time, body);
|
group, "text/plain", timestamp, body);
|
||||||
storeMessage(m);
|
storeMessage(m);
|
||||||
} catch(GeneralSecurityException e) {
|
} catch(GeneralSecurityException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
|||||||
@@ -318,9 +318,21 @@ OnClickListener, OnItemClickListener {
|
|||||||
Intent i = new Intent(this, WriteGroupPostActivity.class);
|
Intent i = new Intent(this, WriteGroupPostActivity.class);
|
||||||
i.putExtra("briar.GROUP_ID", groupId.getBytes());
|
i.putExtra("briar.GROUP_ID", groupId.getBytes());
|
||||||
i.putExtra("briar.GROUP_NAME", groupName);
|
i.putExtra("briar.GROUP_NAME", groupName);
|
||||||
|
i.putExtra("briar.MIN_TIMESTAMP", getMinTimestampForNewMessage());
|
||||||
startActivity(i);
|
startActivity(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private long getMinTimestampForNewMessage() {
|
||||||
|
// Don't use an earlier timestamp than the newest message
|
||||||
|
long timestamp = 0;
|
||||||
|
int count = adapter.getCount();
|
||||||
|
for(int i = 0; i < count; i++) {
|
||||||
|
long t = adapter.getItem(i).getHeader().getTimestamp();
|
||||||
|
if(t > timestamp) timestamp = t;
|
||||||
|
}
|
||||||
|
return timestamp + 1;
|
||||||
|
}
|
||||||
|
|
||||||
public void onItemClick(AdapterView<?> parent, View view, int position,
|
public void onItemClick(AdapterView<?> parent, View view, int position,
|
||||||
long id) {
|
long id) {
|
||||||
displayMessage(position);
|
displayMessage(position);
|
||||||
@@ -337,6 +349,7 @@ OnClickListener, OnItemClickListener {
|
|||||||
i.putExtra("briar.AUTHOR_STATUS", item.getAuthorStatus().name());
|
i.putExtra("briar.AUTHOR_STATUS", item.getAuthorStatus().name());
|
||||||
i.putExtra("briar.CONTENT_TYPE", item.getContentType());
|
i.putExtra("briar.CONTENT_TYPE", item.getContentType());
|
||||||
i.putExtra("briar.TIMESTAMP", item.getTimestamp());
|
i.putExtra("briar.TIMESTAMP", item.getTimestamp());
|
||||||
|
i.putExtra("briar.MIN_TIMESTAMP", getMinTimestampForNewMessage());
|
||||||
i.putExtra("briar.POSITION", position);
|
i.putExtra("briar.POSITION", position);
|
||||||
startActivityForResult(i, REQUEST_READ);
|
startActivityForResult(i, REQUEST_READ);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ implements OnClickListener {
|
|||||||
|
|
||||||
private GroupId groupId = null;
|
private GroupId groupId = null;
|
||||||
private String groupName = null;
|
private String groupName = null;
|
||||||
|
private long timestamp = -1, minTimestamp = -1;
|
||||||
private ImageButton prevButton = null, nextButton = null;
|
private ImageButton prevButton = null, nextButton = null;
|
||||||
private ImageButton replyButton = null;
|
private ImageButton replyButton = null;
|
||||||
private TextView content = null;
|
private TextView content = null;
|
||||||
@@ -58,7 +59,6 @@ implements OnClickListener {
|
|||||||
// Fields that are accessed from background threads must be volatile
|
// Fields that are accessed from background threads must be volatile
|
||||||
@Inject private volatile DatabaseComponent db;
|
@Inject private volatile DatabaseComponent db;
|
||||||
private volatile MessageId messageId = null;
|
private volatile MessageId messageId = null;
|
||||||
private volatile long timestamp = -1;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle state) {
|
public void onCreate(Bundle state) {
|
||||||
@@ -78,6 +78,8 @@ implements OnClickListener {
|
|||||||
if(contentType == null) throw new IllegalStateException();
|
if(contentType == null) throw new IllegalStateException();
|
||||||
timestamp = i.getLongExtra("briar.TIMESTAMP", -1);
|
timestamp = i.getLongExtra("briar.TIMESTAMP", -1);
|
||||||
if(timestamp == -1) throw new IllegalStateException();
|
if(timestamp == -1) throw new IllegalStateException();
|
||||||
|
minTimestamp = i.getLongExtra("briar.MIN_TIMESTAMP", -1);
|
||||||
|
if(minTimestamp == -1) throw new IllegalStateException();
|
||||||
position = i.getIntExtra("briar.POSITION", -1);
|
position = i.getIntExtra("briar.POSITION", -1);
|
||||||
if(position == -1) throw new IllegalStateException();
|
if(position == -1) throw new IllegalStateException();
|
||||||
String authorName = i.getStringExtra("briar.AUTHOR_NAME");
|
String authorName = i.getStringExtra("briar.AUTHOR_NAME");
|
||||||
@@ -223,7 +225,7 @@ implements OnClickListener {
|
|||||||
i.putExtra("briar.GROUP_ID", groupId.getBytes());
|
i.putExtra("briar.GROUP_ID", groupId.getBytes());
|
||||||
i.putExtra("briar.GROUP_NAME", groupName);
|
i.putExtra("briar.GROUP_NAME", groupName);
|
||||||
i.putExtra("briar.PARENT_ID", messageId.getBytes());
|
i.putExtra("briar.PARENT_ID", messageId.getBytes());
|
||||||
i.putExtra("briar.TIMESTAMP", timestamp);
|
i.putExtra("briar.MIN_TIMESTAMP", minTimestamp);
|
||||||
startActivity(i);
|
startActivity(i);
|
||||||
setResult(RESULT_REPLY);
|
setResult(RESULT_REPLY);
|
||||||
finish();
|
finish();
|
||||||
|
|||||||
@@ -79,7 +79,7 @@ implements OnItemSelectedListener, OnClickListener {
|
|||||||
@Inject private volatile CryptoComponent crypto;
|
@Inject private volatile CryptoComponent crypto;
|
||||||
@Inject private volatile MessageFactory messageFactory;
|
@Inject private volatile MessageFactory messageFactory;
|
||||||
private volatile MessageId parentId = null;
|
private volatile MessageId parentId = null;
|
||||||
private volatile long timestamp = -1;
|
private volatile long minTimestamp = -1;
|
||||||
private volatile LocalAuthor localAuthor = null;
|
private volatile LocalAuthor localAuthor = null;
|
||||||
private volatile Group group = null;
|
private volatile Group group = null;
|
||||||
|
|
||||||
@@ -94,9 +94,10 @@ implements OnItemSelectedListener, OnClickListener {
|
|||||||
String groupName = i.getStringExtra("briar.GROUP_NAME");
|
String groupName = i.getStringExtra("briar.GROUP_NAME");
|
||||||
if(groupName == null) throw new IllegalStateException();
|
if(groupName == null) throw new IllegalStateException();
|
||||||
setTitle(groupName);
|
setTitle(groupName);
|
||||||
|
minTimestamp = i.getLongExtra("briar.MIN_TIMESTAMP", -1);
|
||||||
|
if(minTimestamp == -1) throw new IllegalStateException();
|
||||||
b = i.getByteArrayExtra("briar.PARENT_ID");
|
b = i.getByteArrayExtra("briar.PARENT_ID");
|
||||||
if(b != null) parentId = new MessageId(b);
|
if(b != null) parentId = new MessageId(b);
|
||||||
timestamp = i.getLongExtra("briar.TIMESTAMP", -1);
|
|
||||||
|
|
||||||
if(state != null) {
|
if(state != null) {
|
||||||
b = state.getByteArray("briar.LOCAL_AUTHOR_ID");
|
b = state.getByteArray("briar.LOCAL_AUTHOR_ID");
|
||||||
@@ -260,21 +261,21 @@ implements OnItemSelectedListener, OnClickListener {
|
|||||||
private void createMessage(final byte[] body) {
|
private void createMessage(final byte[] body) {
|
||||||
cryptoExecutor.execute(new Runnable() {
|
cryptoExecutor.execute(new Runnable() {
|
||||||
public void run() {
|
public void run() {
|
||||||
// Don't use an earlier timestamp than the parent
|
// Don't use an earlier timestamp than the newest post
|
||||||
long time = System.currentTimeMillis();
|
long timestamp = System.currentTimeMillis();
|
||||||
time = Math.max(time, timestamp + 1);
|
timestamp = Math.max(timestamp, minTimestamp);
|
||||||
Message m;
|
Message m;
|
||||||
try {
|
try {
|
||||||
if(localAuthor == null) {
|
if(localAuthor == null) {
|
||||||
m = messageFactory.createAnonymousMessage(parentId,
|
m = messageFactory.createAnonymousMessage(parentId,
|
||||||
group, "text/plain", time, body);
|
group, "text/plain", timestamp, body);
|
||||||
} else {
|
} else {
|
||||||
KeyParser keyParser = crypto.getSignatureKeyParser();
|
KeyParser keyParser = crypto.getSignatureKeyParser();
|
||||||
byte[] b = localAuthor.getPrivateKey();
|
byte[] b = localAuthor.getPrivateKey();
|
||||||
PrivateKey authorKey = keyParser.parsePrivateKey(b);
|
PrivateKey authorKey = keyParser.parsePrivateKey(b);
|
||||||
m = messageFactory.createPseudonymousMessage(parentId,
|
m = messageFactory.createPseudonymousMessage(parentId,
|
||||||
group, localAuthor, authorKey, "text/plain",
|
group, localAuthor, authorKey, "text/plain",
|
||||||
time, body);
|
timestamp, body);
|
||||||
}
|
}
|
||||||
} catch(GeneralSecurityException e) {
|
} catch(GeneralSecurityException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
|
|||||||
Reference in New Issue
Block a user