diff --git a/src/org/thoughtcrime/securesms/conversation/ConversationAdapter.java b/src/org/thoughtcrime/securesms/conversation/ConversationAdapter.java index 64e7365b1d..09c25bc303 100644 --- a/src/org/thoughtcrime/securesms/conversation/ConversationAdapter.java +++ b/src/org/thoughtcrime/securesms/conversation/ConversationAdapter.java @@ -396,8 +396,11 @@ public class ConversationAdapter if (position < 0) return -1; MessageRecord record = getRecordForPositionOrThrow(position); - - calendar.setTime(new Date(record.getDateSent())); + if (record.getRecipient().getAddress().isOpenGroup()) { + calendar.setTime(new Date(record.getDateReceived())); + } else { + calendar.setTime(new Date(record.getDateSent())); + } return Util.hashCode(calendar.get(Calendar.YEAR), calendar.get(Calendar.DAY_OF_YEAR)); } diff --git a/src/org/thoughtcrime/securesms/database/MmsDatabase.java b/src/org/thoughtcrime/securesms/database/MmsDatabase.java index 8948ff6123..97a59c5007 100644 --- a/src/org/thoughtcrime/securesms/database/MmsDatabase.java +++ b/src/org/thoughtcrime/securesms/database/MmsDatabase.java @@ -972,6 +972,14 @@ public class MmsDatabase extends MessagingDatabase { public long insertMessageOutbox(@NonNull OutgoingMediaMessage message, long threadId, boolean forceSms, @Nullable SmsDatabase.InsertListener insertListener) + throws MmsException { + return insertMessageOutbox(message, threadId, forceSms, insertListener, 0); + } + + public long insertMessageOutbox(@NonNull OutgoingMediaMessage message, + long threadId, boolean forceSms, + @Nullable SmsDatabase.InsertListener insertListener, + long serverTimestamp) throws MmsException { long type = Types.BASE_SENDING_TYPE; @@ -998,7 +1006,10 @@ public class MmsDatabase extends MessagingDatabase { contentValues.put(MESSAGE_BOX, type); contentValues.put(THREAD_ID, threadId); contentValues.put(READ, 1); - contentValues.put(DATE_RECEIVED, System.currentTimeMillis()); + // In open groups messages should be sorted by their server timestamp + long receivedTimestamp = serverTimestamp; + if (serverTimestamp == 0) { receivedTimestamp = System.currentTimeMillis(); } + contentValues.put(DATE_RECEIVED, receivedTimestamp); contentValues.put(SUBSCRIPTION_ID, message.getSubscriptionId()); contentValues.put(EXPIRES_IN, message.getExpiresIn()); contentValues.put(ADDRESS, message.getRecipient().getAddress().serialize()); diff --git a/src/org/thoughtcrime/securesms/database/model/MessageRecord.java b/src/org/thoughtcrime/securesms/database/model/MessageRecord.java index effea38c60..7e8fe34985 100644 --- a/src/org/thoughtcrime/securesms/database/model/MessageRecord.java +++ b/src/org/thoughtcrime/securesms/database/model/MessageRecord.java @@ -136,10 +136,10 @@ public abstract class MessageRecord extends DisplayRecord { } public long getTimestamp() { - if (isPush() && getDateSent() < getDateReceived()) { - return getDateSent(); - } if (getRecipient().getAddress().isOpenGroup()) { + return getDateReceived(); + } + if (isPush() && getDateSent() < getDateReceived()) { return getDateSent(); } return getDateReceived(); diff --git a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java index 28ec14dfdf..4b97762c9b 100644 --- a/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java +++ b/src/org/thoughtcrime/securesms/jobs/PushDecryptJob.java @@ -1021,10 +1021,10 @@ public class PushDecryptJob extends BaseJob implements InjectableType { long messageId; if (isGroup) { - OutgoingMediaMessage outgoingMediaMessage = new OutgoingMediaMessage(recipient, new SlideDeck(), body, message.getTimestamp(), -1, expiresInMillis, ThreadDatabase.DistributionTypes.DEFAULT, null, Collections.emptyList(), Collections.emptyList()); + OutgoingMediaMessage outgoingMediaMessage = new OutgoingMediaMessage(recipient, new SlideDeck(), body, message.getMessage().getTimestamp(), -1, expiresInMillis, ThreadDatabase.DistributionTypes.DEFAULT, null, Collections.emptyList(), Collections.emptyList()); outgoingMediaMessage = new OutgoingSecureMediaMessage(outgoingMediaMessage); - messageId = DatabaseFactory.getMmsDatabase(context).insertMessageOutbox(outgoingMediaMessage, threadId, false, null); + messageId = DatabaseFactory.getMmsDatabase(context).insertMessageOutbox(outgoingMediaMessage, threadId, false, null,message.getTimestamp()); if (message.messageServerID >= 0) { DatabaseFactory.getLokiMessageDatabase(context).setServerID(messageId, message.messageServerID); } database = DatabaseFactory.getMmsDatabase(context); diff --git a/src/org/thoughtcrime/securesms/loki/api/PublicChatPoller.kt b/src/org/thoughtcrime/securesms/loki/api/PublicChatPoller.kt index 28aa5f93f3..6a5ee5dad0 100644 --- a/src/org/thoughtcrime/securesms/loki/api/PublicChatPoller.kt +++ b/src/org/thoughtcrime/securesms/loki/api/PublicChatPoller.kt @@ -192,7 +192,7 @@ class PublicChatPoller(private val context: Context, private val group: PublicCh val userHexEncodedPublicKey = TextSecurePreferences.getLocalNumber(context) val dataMessage = getDataMessage(message) SessionMetaProtocol.dropFromTimestampCacheIfNeeded(dataMessage.timestamp) - val transcript = SentTranscriptMessage(userHexEncodedPublicKey, dataMessage.timestamp, dataMessage, dataMessage.expiresInSeconds.toLong(), Collections.singletonMap(userHexEncodedPublicKey, false)) + val transcript = SentTranscriptMessage(userHexEncodedPublicKey, message.serverTimestamp, dataMessage, dataMessage.expiresInSeconds.toLong(), Collections.singletonMap(userHexEncodedPublicKey, false)) transcript.messageServerID = messageServerID if (dataMessage.quote.isPresent || (dataMessage.attachments.isPresent && dataMessage.attachments.get().size > 0) || dataMessage.previews.isPresent) { PushDecryptJob(context).handleSynchronizeSentMediaMessage(transcript)