11package org.session.libsession.messaging.sending_receiving
22
33import network.loki.messenger.libsession_util.SessionEncrypt
4+ import network.loki.messenger.libsession_util.pro.ProProof
45import network.loki.messenger.libsession_util.protocol.DecodedEnvelope
56import network.loki.messenger.libsession_util.protocol.DecodedPro
67import network.loki.messenger.libsession_util.protocol.SessionProtocol
7- import network.loki.messenger.libsession_util.util.BitSet
88import network.loki.messenger.libsession_util.util.asSequence
99import org.session.libsession.database.StorageProtocol
1010import org.session.libsession.messaging.messages.Message
@@ -28,8 +28,10 @@ import org.session.libsignal.utilities.Base64
2828import org.session.libsignal.utilities.Hex
2929import org.session.libsignal.utilities.IdPrefix
3030import org.session.protos.SessionProtos
31+ import org.thoughtcrime.securesms.pro.ProBackendConfig
3132import java.util.concurrent.TimeUnit
3233import javax.inject.Inject
34+ import javax.inject.Provider
3335import javax.inject.Singleton
3436import kotlin.math.abs
3537
@@ -39,17 +41,21 @@ class MessageParser @Inject constructor(
3941 private val storage : StorageProtocol ,
4042 private val snodeClock : SnodeClock ,
4143 private val prefs : TextSecurePreferences ,
44+ private val proBackendConfig : Provider <ProBackendConfig >,
4245) {
4346
44- // TODO: Obtain proBackendKey from somewhere
45- private val proBackendKey = ByteArray (32 )
46-
4747 // A faster way to check if the user is blocked than to go through RecipientRepository
4848 private fun isUserBlocked (accountId : AccountId ): Boolean {
4949 return configFactory.withUserConfigs { it.contacts.get(accountId.hexString) }
5050 ?.blocked == true
5151 }
5252
53+ class ParseResult (
54+ val message : Message ,
55+ val proto : SessionProtos .Content ,
56+ val pro : DecodedPro ?
57+ )
58+
5359
5460 private fun createMessageFromProto (proto : SessionProtos .Content , isGroupMessage : Boolean ): Message {
5561 val message = ReadReceipt .fromProto(proto) ? :
@@ -77,7 +83,7 @@ class MessageParser @Inject constructor(
7783 currentUserId : AccountId ,
7884 currentUserBlindedIDs : List <AccountId >,
7985 senderIdPrefix : IdPrefix
80- ): Pair < Message , SessionProtos . Content > {
86+ ): ParseResult {
8187 return parseMessage(
8288 sender = AccountId (senderIdPrefix, decodedEnvelope.senderX25519PubKey.data),
8389 contentPlaintext = decodedEnvelope.contentPlainText.data,
@@ -101,7 +107,7 @@ class MessageParser @Inject constructor(
101107 isForGroup : Boolean ,
102108 currentUserId : AccountId ,
103109 currentUserBlindedIDs : List <AccountId >,
104- ): Pair < Message , SessionProtos . Content > {
110+ ): ParseResult {
105111 val proto = SessionProtos .Content .parseFrom(contentPlaintext)
106112
107113 // Check signature
@@ -136,9 +142,11 @@ class MessageParser @Inject constructor(
136142
137143 // Only process pro features post pro launch
138144 if (prefs.forcePostPro()) {
139- (message as ? VisibleMessage )?.proFeatures = buildSet {
140- pro?.proMessageFeatures?.asSequence()?.let (::addAll)
141- pro?.proProfileFeatures?.asSequence()?.let (::addAll)
145+ if (pro?.status == ProProof .STATUS_VALID ) {
146+ (message as ? VisibleMessage )?.proFeatures = buildSet {
147+ addAll(pro.proMessageFeatures.asSequence())
148+ addAll(pro.proProfileFeatures.asSequence())
149+ }
142150 }
143151 }
144152
@@ -162,7 +170,11 @@ class MessageParser @Inject constructor(
162170 }
163171 storage.addReceivedMessageTimestamp(messageTimestampMs)
164172
165- return message to proto
173+ return ParseResult (
174+ message = message,
175+ proto = proto,
176+ pro = pro
177+ )
166178 }
167179
168180
@@ -171,12 +183,11 @@ class MessageParser @Inject constructor(
171183 serverHash : String? ,
172184 currentUserEd25519PrivKey : ByteArray ,
173185 currentUserId : AccountId ,
174- ): Pair < Message , SessionProtos . Content > {
186+ ): ParseResult {
175187 val envelop = SessionProtocol .decodeFor1o1(
176188 myEd25519PrivKey = currentUserEd25519PrivKey,
177189 payload = data,
178- nowEpochMs = snodeClock.currentTimeMills(),
179- proBackendPubKey = proBackendKey,
190+ proBackendPubKey = proBackendConfig.get().ed25519PubKey,
180191 )
181192
182193 return parseMessage(
@@ -187,8 +198,8 @@ class MessageParser @Inject constructor(
187198 senderIdPrefix = IdPrefix .STANDARD ,
188199 currentUserId = currentUserId,
189200 currentUserBlindedIDs = emptyList(),
190- ).also { (message, _) ->
191- message.serverHash = serverHash
201+ ).also { result ->
202+ result. message.serverHash = serverHash
192203 }
193204 }
194205
@@ -198,18 +209,17 @@ class MessageParser @Inject constructor(
198209 groupId : AccountId ,
199210 currentUserEd25519PrivKey : ByteArray ,
200211 currentUserId : AccountId ,
201- ): Pair < Message , SessionProtos . Content > {
212+ ): ParseResult {
202213 val keys = configFactory.withGroupConfigs(groupId) {
203214 it.groupKeys.groupKeys()
204215 }
205216
206217 val decoded = SessionProtocol .decodeForGroup(
207218 payload = data,
208219 myEd25519PrivKey = currentUserEd25519PrivKey,
209- nowEpochMs = snodeClock.currentTimeMills(),
210220 groupEd25519PublicKey = groupId.pubKeyBytes,
211221 groupEd25519PrivateKeys = keys.toTypedArray(),
212- proBackendPubKey = proBackendKey
222+ proBackendPubKey = proBackendConfig.get().ed25519PubKey,
213223 )
214224
215225 return parseMessage(
@@ -220,24 +230,24 @@ class MessageParser @Inject constructor(
220230 senderIdPrefix = IdPrefix .STANDARD ,
221231 currentUserId = currentUserId,
222232 currentUserBlindedIDs = emptyList(),
223- ).also { (message, _) ->
224- message.serverHash = serverHash
233+ ).also { result ->
234+ result. message.serverHash = serverHash
225235 }
226236 }
227237
228238 fun parseCommunityMessage (
229239 msg : OpenGroupApi .Message ,
230240 currentUserId : AccountId ,
231241 currentUserBlindedIDs : List <AccountId >,
232- ): Pair < Message , SessionProtos . Content > ? {
242+ ): ParseResult ? {
233243 if (msg.data.isNullOrBlank()) {
234244 return null
235245 }
236246
237247 val decoded = SessionProtocol .decodeForCommunity(
238248 payload = Base64 .decode(msg.data),
239- nowEpochMs = snodeClock.currentTimeMills (),
240- proBackendPubKey = proBackendKey ,
249+ timestampMs = (msg.posted * 1000 ).toLong (),
250+ proBackendPubKey = proBackendConfig.get().ed25519PubKey ,
241251 )
242252
243253 val sender = AccountId (msg.sessionId)
@@ -252,8 +262,8 @@ class MessageParser @Inject constructor(
252262 sender = sender,
253263 messageTimestampMs = (msg.posted * 1000 ).toLong(),
254264 currentUserBlindedIDs = currentUserBlindedIDs,
255- ).also { (message, _) ->
256- message.openGroupServerMessageID = msg.id
265+ ).also { result ->
266+ result. message.openGroupServerMessageID = msg.id
257267 }
258268 }
259269
@@ -263,7 +273,7 @@ class MessageParser @Inject constructor(
263273 currentUserEd25519PrivKey : ByteArray ,
264274 currentUserId : AccountId ,
265275 currentUserBlindedIDs : List <AccountId >,
266- ): Pair < Message , SessionProtos . Content > {
276+ ): ParseResult {
267277 val (senderId, plaintext) = SessionEncrypt .decryptForBlindedRecipient(
268278 ciphertext = Base64 .decode(msg.message),
269279 myEd25519Privkey = currentUserEd25519PrivKey,
@@ -274,8 +284,8 @@ class MessageParser @Inject constructor(
274284
275285 val decoded = SessionProtocol .decodeForCommunity(
276286 payload = plaintext.data,
277- nowEpochMs = snodeClock.currentTimeMills() ,
278- proBackendPubKey = proBackendKey ,
287+ timestampMs = msg.postedAt * 1000L ,
288+ proBackendPubKey = proBackendConfig.get().ed25519PubKey ,
279289 )
280290
281291 val sender = Address .Standard (AccountId (senderId))
0 commit comments