Skip to content

Commit ed1bddd

Browse files
Merge pull request #1691 from oxen-io/fix/ses-2804-message-reappearing-account-restored
Making sure restored accounts do not display deleted messages
2 parents e068b71 + bf61579 commit ed1bddd

File tree

4 files changed

+46
-6
lines changed

4 files changed

+46
-6
lines changed

app/src/main/java/org/thoughtcrime/securesms/dependencies/ConfigFactory.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,18 @@ class ConfigFactory(
196196
}
197197
}
198198

199+
override fun getConfigTimestamp(forConfigObject: ConfigBase, publicKey: String): Long {
200+
val variant = when (forConfigObject) {
201+
is UserProfile -> SharedConfigMessage.Kind.USER_PROFILE.name
202+
is Contacts -> SharedConfigMessage.Kind.CONTACTS.name
203+
is ConversationVolatileConfig -> SharedConfigMessage.Kind.CONVO_INFO_VOLATILE.name
204+
is UserGroupsConfig -> SharedConfigMessage.Kind.GROUPS.name
205+
else -> throw UnsupportedOperationException("Can't support type of ${forConfigObject::class.simpleName} yet")
206+
}
207+
208+
return configDatabase.retrieveConfigLastUpdateTimestamp(variant, publicKey)
209+
}
210+
199211
override fun conversationInConfig(
200212
publicKey: String?,
201213
groupPublicKey: String?,

libsession/src/main/java/org/session/libsession/messaging/jobs/BatchMessageReceiveJob.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import kotlinx.coroutines.Dispatchers
55
import kotlinx.coroutines.async
66
import kotlinx.coroutines.awaitAll
77
import kotlinx.coroutines.runBlocking
8+
import network.loki.messenger.libsession_util.ConfigBase
89
import nl.komponents.kovenant.Promise
910
import nl.komponents.kovenant.task
1011
import org.session.libsession.messaging.MessagingModuleConfiguration
1112
import org.session.libsession.messaging.messages.Message
13+
import org.session.libsession.messaging.messages.Message.Companion.senderOrSync
1214
import org.session.libsession.messaging.messages.control.CallMessage
1315
import org.session.libsession.messaging.messages.control.ClosedGroupControlMessage
1416
import org.session.libsession.messaging.messages.control.ConfigurationMessage
@@ -96,6 +98,26 @@ class BatchMessageReceiveJob(
9698
executeAsync(dispatcherName).get()
9799
}
98100

101+
private fun isHidden(message: Message): Boolean{
102+
// if the contact is marked as hidden for 1on1 messages
103+
// and the message's sentTimestamp is earlier than the sentTimestamp of the last config
104+
val config = MessagingModuleConfiguration.shared.configFactory
105+
val publicKey = MessagingModuleConfiguration.shared.storage.getUserPublicKey()
106+
if(config.contacts == null || message.sentTimestamp == null || publicKey == null) return false
107+
val contactConfigTimestamp = config.getConfigTimestamp(config.contacts!!, publicKey)
108+
if(message.groupPublicKey == null && // not a group
109+
message.openGroupServerMessageID == null && // not a community
110+
// not marked as hidden
111+
config.contacts?.get(message.senderOrSync)?.priority == ConfigBase.PRIORITY_HIDDEN &&
112+
// the message's sentTimestamp is earlier than the sentTimestamp of the last config
113+
message.sentTimestamp!! < contactConfigTimestamp
114+
) {
115+
return true
116+
}
117+
118+
return false
119+
}
120+
99121
fun executeAsync(dispatcherName: String): Promise<Unit, Exception> {
100122
return task {
101123
val threadMap = mutableMapOf<Long, MutableList<ParsedMessage>>()
@@ -112,6 +134,9 @@ class BatchMessageReceiveJob(
112134
val (message, proto) = MessageReceiver.parse(data, openGroupMessageServerID, openGroupPublicKey = serverPublicKey, currentClosedGroups = currentClosedGroups)
113135
message.serverHash = serverHash
114136
val parsedParams = ParsedMessage(messageParameters, message, proto)
137+
138+
if(isHidden(message)) return@forEach
139+
115140
val threadID = Message.getThreadId(message, openGroupID, storage, shouldCreateThread(parsedParams)) ?: NO_THREAD_MAPPING
116141
if (!threadMap.containsKey(threadID)) {
117142
threadMap[threadID] = mutableListOf(parsedParams)

libsession/src/main/java/org/session/libsession/messaging/messages/Message.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ abstract class Message {
3333

3434
companion object {
3535
fun getThreadId(message: Message, openGroupID: String?, storage: StorageProtocol, shouldCreateThread: Boolean): Long? {
36-
val senderOrSync = when (message) {
37-
is VisibleMessage -> message.syncTarget ?: message.sender!!
38-
is ExpirationTimerUpdate -> message.syncTarget ?: message.sender!!
39-
else -> message.sender!!
40-
}
41-
return storage.getThreadIdFor(senderOrSync, message.groupPublicKey, openGroupID, createThread = shouldCreateThread)
36+
return storage.getThreadIdFor(message.senderOrSync, message.groupPublicKey, openGroupID, createThread = shouldCreateThread)
37+
}
38+
39+
val Message.senderOrSync get() = when(this) {
40+
is VisibleMessage -> syncTarget ?: sender!!
41+
is ExpirationTimerUpdate -> syncTarget ?: sender!!
42+
else -> sender!!
4243
}
4344
}
4445

libsession/src/main/java/org/session/libsession/utilities/ConfigFactoryProtocol.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ interface ConfigFactoryProtocol {
1616

1717
fun conversationInConfig(publicKey: String?, groupPublicKey: String?, openGroupId: String?, visibleOnly: Boolean): Boolean
1818
fun canPerformChange(variant: String, publicKey: String, changeTimestampMs: Long): Boolean
19+
20+
fun getConfigTimestamp(forConfigObject: ConfigBase, publicKey: String): Long
1921
}
2022

2123
interface ConfigFactoryUpdateListener {

0 commit comments

Comments
 (0)