mirror of
https://github.com/oxen-io/session-android.git
synced 2025-08-11 17:57:47 +00:00
Merge branch 'od' into on-2
This commit is contained in:
@@ -53,7 +53,7 @@ interface StorageProtocol {
|
||||
fun persistJob(job: Job)
|
||||
fun markJobAsSucceeded(jobId: String)
|
||||
fun markJobAsFailedPermanently(jobId: String)
|
||||
fun getAllPendingJobs(type: String): Map<String,Job?>
|
||||
fun getAllPendingJobs(vararg types: String): Map<String,Job?>
|
||||
fun getAttachmentUploadJob(attachmentID: Long): AttachmentUploadJob?
|
||||
fun getMessageSendJob(messageSendJobID: String): MessageSendJob?
|
||||
fun getMessageReceiveJob(messageReceiveJobID: String): Job?
|
||||
|
@@ -1,6 +1,8 @@
|
||||
package org.session.libsession.messaging.jobs
|
||||
|
||||
import okhttp3.HttpUrl
|
||||
import org.session.libsession.database.MessageDataProvider
|
||||
import org.session.libsession.database.StorageProtocol
|
||||
import org.session.libsession.messaging.MessagingModuleConfiguration
|
||||
import org.session.libsession.messaging.open_groups.OpenGroupApi
|
||||
import org.session.libsession.messaging.sending_receiving.attachments.AttachmentId
|
||||
@@ -40,6 +42,36 @@ class AttachmentDownloadJob(val attachmentID: Long, val databaseMessageID: Long)
|
||||
// Keys used for database storage
|
||||
private val ATTACHMENT_ID_KEY = "attachment_id"
|
||||
private val TS_INCOMING_MESSAGE_ID_KEY = "tsIncoming_message_id"
|
||||
|
||||
/**
|
||||
* Check if the attachment in the given message is eligible for download.
|
||||
*
|
||||
* Note that this function only checks for the eligibility of the attachment in the sense
|
||||
* of whether the download is allowed, it does not check if the download has already taken
|
||||
* place.
|
||||
*/
|
||||
fun eligibleForDownload(threadID: Long,
|
||||
storage: StorageProtocol,
|
||||
messageDataProvider: MessageDataProvider,
|
||||
databaseMessageID: Long): Boolean {
|
||||
val threadRecipient = storage.getRecipientForThread(threadID) ?: return false
|
||||
|
||||
// if we are the sender we are always eligible
|
||||
val selfSend = messageDataProvider.isMmsOutgoing(databaseMessageID)
|
||||
if (selfSend) {
|
||||
return true
|
||||
}
|
||||
|
||||
// you can't be eligible without a sender
|
||||
val sender = messageDataProvider.getIndividualRecipientForMms(databaseMessageID)?.address?.serialize()
|
||||
?: return false
|
||||
|
||||
// you can't be eligible without a contact entry
|
||||
val contact = storage.getContactWithAccountID(sender) ?: return false
|
||||
|
||||
// we are eligible if we are receiving a group message or the contact is trusted
|
||||
return threadRecipient.isGroupRecipient || contact.isTrusted
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun execute(dispatcherName: String) {
|
||||
@@ -88,21 +120,7 @@ class AttachmentDownloadJob(val attachmentID: Long, val databaseMessageID: Long)
|
||||
return
|
||||
}
|
||||
|
||||
val threadRecipient = storage.getRecipientForThread(threadID)
|
||||
val selfSend = messageDataProvider.isMmsOutgoing(databaseMessageID)
|
||||
val sender = if (selfSend) {
|
||||
storage.getUserPublicKey()
|
||||
} else {
|
||||
messageDataProvider.getIndividualRecipientForMms(databaseMessageID)?.address?.serialize()
|
||||
}
|
||||
val contact = sender?.let { storage.getContactWithAccountID(it) }
|
||||
if (threadRecipient == null || sender == null || (contact == null && !selfSend)) {
|
||||
handleFailure(Error.NoSender, null)
|
||||
return
|
||||
}
|
||||
if (!threadRecipient.isGroupRecipient && contact?.isTrusted != true && storage.getUserPublicKey() != sender) {
|
||||
// if we aren't receiving a group message, a message from ourselves (self-send) and the contact sending is not trusted:
|
||||
// do not continue, but do not fail
|
||||
if (!eligibleForDownload(threadID, storage, messageDataProvider, databaseMessageID)) {
|
||||
handleFailure(Error.NoSender, null)
|
||||
return
|
||||
}
|
||||
|
@@ -61,7 +61,7 @@ data class ConfigurationSyncJob(val destination: Destination): Job {
|
||||
SharedConfigurationMessage(config.protoKindFor(), data, seqNo) to config
|
||||
}.map { (message, config) ->
|
||||
// return a list of batch request objects
|
||||
val snodeMessage = MessageSender.buildWrappedMessageToSnode(destination, message, true)
|
||||
val snodeMessage = MessageSender.buildConfigMessageToSnode(destination.destinationPublicKey(), message)
|
||||
val authenticated = SnodeAPI.buildAuthenticatedStoreBatchInfo(
|
||||
destination.destinationPublicKey(),
|
||||
config.configNamespace(),
|
||||
|
@@ -102,7 +102,7 @@ class JobQueue : JobDelegate {
|
||||
execute(dispatcherName)
|
||||
}
|
||||
catch (e: Exception) {
|
||||
Log.d(dispatcherName, "unhandledJobException: ${javaClass.simpleName} (id: $id)")
|
||||
Log.d(dispatcherName, "unhandledJobException: ${javaClass.simpleName} (id: $id)", e)
|
||||
this@JobQueue.handleJobFailed(this, dispatcherName, e)
|
||||
}
|
||||
}
|
||||
|
@@ -6,6 +6,11 @@ data class GroupMember(
|
||||
val role: GroupMemberRole
|
||||
)
|
||||
|
||||
enum class GroupMemberRole {
|
||||
STANDARD, ZOOMBIE, MODERATOR, ADMIN, HIDDEN_MODERATOR, HIDDEN_ADMIN
|
||||
enum class GroupMemberRole(val isModerator: Boolean = false) {
|
||||
STANDARD,
|
||||
ZOOMBIE,
|
||||
MODERATOR(true),
|
||||
ADMIN(true),
|
||||
HIDDEN_MODERATOR(true),
|
||||
HIDDEN_ADMIN(true),
|
||||
}
|
||||
|
@@ -81,6 +81,15 @@ object MessageSender {
|
||||
}
|
||||
}
|
||||
|
||||
fun buildConfigMessageToSnode(destinationPubKey: String, message: SharedConfigurationMessage): SnodeMessage {
|
||||
return SnodeMessage(
|
||||
destinationPubKey,
|
||||
Base64.encodeBytes(message.data),
|
||||
ttl = message.ttl,
|
||||
SnodeAPI.nowWithOffset
|
||||
)
|
||||
}
|
||||
|
||||
// One-on-One Chats & Closed Groups
|
||||
@Throws(Exception::class)
|
||||
fun buildWrappedMessageToSnode(destination: Destination, message: Message, isSyncMessage: Boolean): SnodeMessage {
|
||||
|
@@ -25,6 +25,7 @@ import org.session.libsession.snode.RawResponse
|
||||
import org.session.libsession.snode.SnodeAPI
|
||||
import org.session.libsession.snode.SnodeModule
|
||||
import org.session.libsession.utilities.ConfigFactoryProtocol
|
||||
import org.session.libsignal.utilities.Base64
|
||||
import org.session.libsignal.utilities.Log
|
||||
import org.session.libsignal.utilities.Namespace
|
||||
import org.session.libsignal.utilities.Snode
|
||||
@@ -126,37 +127,26 @@ class Poller(private val configFactory: ConfigFactoryProtocol, debounceTimer: Ti
|
||||
private fun processConfig(snode: Snode, rawMessages: RawResponse, namespace: Int, forConfigObject: ConfigBase?) {
|
||||
if (forConfigObject == null) return
|
||||
|
||||
val messages = SnodeAPI.parseRawMessagesResponse(
|
||||
rawMessages,
|
||||
snode,
|
||||
userPublicKey,
|
||||
namespace,
|
||||
updateLatestHash = true,
|
||||
updateStoredHashes = true,
|
||||
)
|
||||
val messages = rawMessages["messages"] as? List<*>
|
||||
val processed = if (!messages.isNullOrEmpty()) {
|
||||
SnodeAPI.updateLastMessageHashValueIfPossible(snode, userPublicKey, messages, namespace)
|
||||
SnodeAPI.removeDuplicates(userPublicKey, messages, namespace, true).mapNotNull { messageBody ->
|
||||
val rawMessageAsJSON = messageBody as? Map<*, *> ?: return@mapNotNull null
|
||||
val hashValue = rawMessageAsJSON["hash"] as? String ?: return@mapNotNull null
|
||||
val b64EncodedBody = rawMessageAsJSON["data"] as? String ?: return@mapNotNull null
|
||||
val timestamp = rawMessageAsJSON["t"] as? Long ?: SnodeAPI.nowWithOffset
|
||||
val body = Base64.decode(b64EncodedBody)
|
||||
Triple(body, hashValue, timestamp)
|
||||
}
|
||||
} else emptyList()
|
||||
|
||||
if (messages.isEmpty()) {
|
||||
// no new messages to process
|
||||
return
|
||||
}
|
||||
if (processed.isEmpty()) return
|
||||
|
||||
var latestMessageTimestamp: Long? = null
|
||||
messages.forEach { (envelope, hash) ->
|
||||
processed.forEach { (body, hash, timestamp) ->
|
||||
try {
|
||||
val (message, _) = MessageReceiver.parse(data = envelope.toByteArray(),
|
||||
// assume no groups in personal poller messages
|
||||
openGroupServerID = null, currentClosedGroups = emptySet()
|
||||
)
|
||||
// sanity checks
|
||||
if (message !is SharedConfigurationMessage) {
|
||||
Log.w("Loki", "shared config message handled in configs wasn't SharedConfigurationMessage but was ${message.javaClass.simpleName}")
|
||||
return@forEach
|
||||
}
|
||||
val merged = forConfigObject.merge(hash!! to message.data).firstOrNull { it == hash }
|
||||
if (merged != null) {
|
||||
// We successfully merged the hash, we can now update the timestamp
|
||||
latestMessageTimestamp = if ((message.sentTimestamp ?: 0L) > (latestMessageTimestamp ?: 0L)) { message.sentTimestamp } else { latestMessageTimestamp }
|
||||
}
|
||||
forConfigObject.merge(hash to body)
|
||||
latestMessageTimestamp = if (timestamp > (latestMessageTimestamp ?: 0L)) { timestamp } else { latestMessageTimestamp }
|
||||
} catch (e: Exception) {
|
||||
Log.e("Loki", e)
|
||||
}
|
||||
|
@@ -25,7 +25,6 @@ import org.session.libsignal.utilities.Snode
|
||||
import org.session.libsignal.utilities.ThreadUtils
|
||||
import org.session.libsignal.utilities.recover
|
||||
import org.session.libsignal.utilities.toHexString
|
||||
import java.util.Date
|
||||
import java.util.concurrent.atomic.AtomicReference
|
||||
import kotlin.collections.set
|
||||
|
||||
|
@@ -829,7 +829,7 @@ object SnodeAPI {
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateLastMessageHashValueIfPossible(snode: Snode, publicKey: String, rawMessages: List<*>, namespace: Int) {
|
||||
fun updateLastMessageHashValueIfPossible(snode: Snode, publicKey: String, rawMessages: List<*>, namespace: Int) {
|
||||
val lastMessageAsJSON = rawMessages.lastOrNull() as? Map<*, *>
|
||||
val hashValue = lastMessageAsJSON?.get("hash") as? String
|
||||
if (hashValue != null) {
|
||||
@@ -839,7 +839,7 @@ object SnodeAPI {
|
||||
}
|
||||
}
|
||||
|
||||
private fun removeDuplicates(publicKey: String, rawMessages: List<*>, namespace: Int, updateStoredHashes: Boolean): List<*> {
|
||||
fun removeDuplicates(publicKey: String, rawMessages: List<*>, namespace: Int, updateStoredHashes: Boolean): List<*> {
|
||||
val originalMessageHashValues = database.getReceivedMessageHashValues(publicKey, namespace)?.toMutableSet() ?: mutableSetOf()
|
||||
val receivedMessageHashValues = originalMessageHashValues.toMutableSet()
|
||||
val result = rawMessages.filter { rawMessage ->
|
||||
|
@@ -73,4 +73,10 @@
|
||||
<string name="ConversationItem_group_action_left">%1$s has left the group.</string>
|
||||
<!-- RecipientProvider -->
|
||||
<string name="RecipientProvider_unnamed_group">Unnamed group</string>
|
||||
|
||||
<string name="clearDataErrorDescriptionGeneric">An unknown error occurred and your data was not deleted. Do you want to delete your data from just this device instead?</string>
|
||||
<string name="errorUnknown">An unknown error occurred.</string>
|
||||
<string name="clearDevice">Clear Device</string>
|
||||
<string name="clearDeviceOnly">Clear device only</string>
|
||||
<string name="clearDeviceAndNetwork">Clear device and network</string>
|
||||
</resources>
|
||||
|
Reference in New Issue
Block a user