Sort forum messages correctly even if clocks are wrong. Bug #57.

This commit is contained in:
akwizgran
2014-04-01 19:35:56 +01:00
parent 56f1eb80db
commit 3008bcc209
6 changed files with 49 additions and 23 deletions

View File

@@ -387,13 +387,8 @@ implements EventListener, OnClickListener, OnItemClickListener {
public void onClick(View view) {
String message = content.getText().toString();
if(message.equals("")) return;
// Don't use an earlier timestamp than the newest message
long timestamp = System.currentTimeMillis();
int count = adapter.getCount();
for(int i = 0; i < count; i++) {
long time = adapter.getItem(i).getHeader().getTimestamp() + 1;
if(time > timestamp) timestamp = time;
}
timestamp = Math.max(timestamp, getMinTimestampForNewMessage());
createMessage(StringUtils.toUtf8(message), timestamp);
Toast.makeText(this, R.string.message_sent_toast, LENGTH_SHORT).show();
content.setText("");
@@ -402,6 +397,17 @@ implements EventListener, OnClickListener, OnItemClickListener {
((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) {
cryptoExecutor.execute(new Runnable() {
public void run() {
@@ -452,6 +458,7 @@ implements EventListener, OnClickListener, OnItemClickListener {
i.putExtra("briar.MESSAGE_ID", header.getId().getBytes());
i.putExtra("briar.CONTENT_TYPE", header.getContentType());
i.putExtra("briar.TIMESTAMP", header.getTimestamp());
i.putExtra("briar.MIN_TIMESTAMP", getMinTimestampForNewMessage());
i.putExtra("briar.POSITION", position);
startActivityForResult(i, REQUEST_READ);
}

View File

@@ -51,6 +51,7 @@ implements OnClickListener {
private String contactName = null;
private AuthorId localAuthorId = null;
private long timestamp = -1, minTimestamp = -1;
private ImageButton prevButton = null, nextButton = null;
private ImageButton replyButton = null;
private TextView content = null;
@@ -60,7 +61,6 @@ implements OnClickListener {
@Inject private volatile DatabaseComponent db;
private volatile MessageId messageId = null;
private volatile GroupId groupId = null;
private volatile long timestamp = -1;
@Override
public void onCreate(Bundle state) {
@@ -85,6 +85,8 @@ implements OnClickListener {
if(contentType == null) throw new IllegalStateException();
timestamp = i.getLongExtra("briar.TIMESTAMP", -1);
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);
if(position == -1) throw new IllegalStateException();
@@ -228,7 +230,7 @@ implements OnClickListener {
i.putExtra("briar.LOCAL_AUTHOR_ID",
localAuthorId.getBytes());
i.putExtra("briar.PARENT_ID", messageId.getBytes());
i.putExtra("briar.TIMESTAMP", timestamp);
i.putExtra("briar.MIN_TIMESTAMP", minTimestamp);
startActivity(i);
setResult(RESULT_REPLY);
finish();

View File

@@ -69,7 +69,7 @@ implements OnClickListener {
private volatile GroupId groupId = null;
private volatile AuthorId localAuthorId = null;
private volatile MessageId parentId = null;
private volatile long timestamp = -1;
private volatile long minTimestamp = -1;
private volatile LocalAuthor localAuthor = null;
private volatile Group group = null;
@@ -86,10 +86,11 @@ implements OnClickListener {
groupId = new GroupId(b);
b = i.getByteArrayExtra("briar.LOCAL_AUTHOR_ID");
if(b == null) throw new IllegalStateException();
minTimestamp = i.getLongExtra("briar.MIN_TIMESTAMP", -1);
if(minTimestamp == -1) throw new IllegalStateException();
localAuthorId = new AuthorId(b);
b = i.getByteArrayExtra("briar.PARENT_ID");
if(b != null) parentId = new MessageId(b);
timestamp = i.getLongExtra("briar.TIMESTAMP", -1);
LinearLayout layout = new LinearLayout(this);
layout.setLayoutParams(MATCH_WRAP);
@@ -187,12 +188,12 @@ implements OnClickListener {
private void createMessage(final byte[] body) {
cryptoExecutor.execute(new Runnable() {
public void run() {
// Don't use an earlier timestamp than the parent
long time = System.currentTimeMillis();
time = Math.max(time, timestamp + 1);
// Don't use an earlier timestamp than the newest message
long timestamp = System.currentTimeMillis();
timestamp = Math.max(timestamp, minTimestamp);
try {
Message m = messageFactory.createAnonymousMessage(parentId,
group, "text/plain", time, body);
group, "text/plain", timestamp, body);
storeMessage(m);
} catch(GeneralSecurityException e) {
throw new RuntimeException(e);