Skip to content

Commit 09050b4

Browse files
Merge pull request #1668 from session-foundation/integrate-session-pro
Integrate session protocol APIs from libsession-util
1 parent e051110 commit 09050b4

File tree

54 files changed

+2340
-1085
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2340
-1085
lines changed

app/src/main/java/org/session/libsession/messaging/jobs/AttachmentDownloadJob.kt

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package org.session.libsession.messaging.jobs
22

3-
import android.content.Context
43
import dagger.assisted.Assisted
54
import dagger.assisted.AssistedFactory
65
import dagger.assisted.AssistedInject
7-
import dagger.hilt.android.qualifiers.ApplicationContext
86
import okhttp3.HttpUrl.Companion.toHttpUrl
97
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
108
import org.session.libsession.database.MessageDataProvider
@@ -16,7 +14,6 @@ import org.session.libsession.messaging.sending_receiving.attachments.Attachment
1614
import org.session.libsession.messaging.sending_receiving.attachments.DatabaseAttachment
1715
import org.session.libsession.messaging.utilities.Data
1816
import org.session.libsession.snode.OnionRequestAPI
19-
import org.session.libsession.snode.utilities.await
2017
import org.session.libsession.utilities.Address
2118
import org.session.libsession.utilities.DecodedAudio
2219
import org.session.libsession.utilities.InputStreamMediaDataSource
@@ -252,21 +249,18 @@ class AttachmentDownloadJob @AssistedInject constructor(
252249
return KEY
253250
}
254251

255-
class DeserializeFactory(private val factory: Factory) : Job.DeserializeFactory<AttachmentDownloadJob> {
252+
@AssistedFactory
253+
abstract class Factory : Job.DeserializeFactory<AttachmentDownloadJob> {
254+
abstract fun create(
255+
@Assisted("attachmentID") attachmentID: Long,
256+
mmsMessageId: Long
257+
): AttachmentDownloadJob
256258

257259
override fun create(data: Data): AttachmentDownloadJob {
258-
return factory.create(
260+
return create(
259261
attachmentID = data.getLong(ATTACHMENT_ID_KEY),
260262
mmsMessageId = data.getLong(TS_INCOMING_MESSAGE_ID_KEY)
261263
)
262264
}
263265
}
264-
265-
@AssistedFactory
266-
interface Factory {
267-
fun create(
268-
@Assisted("attachmentID") attachmentID: Long,
269-
mmsMessageId: Long
270-
): AttachmentDownloadJob
271-
}
272266
}

app/src/main/java/org/session/libsession/messaging/jobs/AttachmentUploadJob.kt

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import org.session.libsession.messaging.messages.Message
1515
import org.session.libsession.messaging.open_groups.OpenGroupApi
1616
import org.session.libsession.messaging.sending_receiving.MessageSender
1717
import org.session.libsession.messaging.utilities.Data
18-
import org.session.libsession.snode.utilities.await
1918
import org.session.libsession.utilities.Address
2019
import org.session.libsession.utilities.DecodedAudio
2120
import org.session.libsession.utilities.InputStreamMediaDataSource
@@ -38,6 +37,7 @@ class AttachmentUploadJob @AssistedInject constructor(
3837
private val attachmentProcessor: AttachmentProcessor,
3938
private val preferences: TextSecurePreferences,
4039
private val fileServerApi: FileServerApi,
40+
private val messageSender: MessageSender,
4141
) : Job {
4242
override var delegate: JobDelegate? = null
4343
override var id: String? = null
@@ -219,7 +219,7 @@ class AttachmentUploadJob @AssistedInject constructor(
219219

220220
private fun failAssociatedMessageSendJob(e: Exception) {
221221
val messageSendJob = storage.getMessageSendJob(messageSendJobID)
222-
MessageSender.handleFailedMessageSend(this.message, e)
222+
messageSender.handleFailedMessageSend(this.message, e)
223223
if (messageSendJob != null) {
224224
storage.markJobAsFailedPermanently(messageSendJobID)
225225
}
@@ -244,7 +244,14 @@ class AttachmentUploadJob @AssistedInject constructor(
244244
return KEY
245245
}
246246

247-
class DeserializeFactory(private val factory: Factory): Job.DeserializeFactory<AttachmentUploadJob> {
247+
@AssistedFactory
248+
abstract class Factory : Job.DeserializeFactory<AttachmentUploadJob> {
249+
abstract fun create(
250+
attachmentID: Long,
251+
@Assisted("threadID") threadID: String,
252+
message: Message,
253+
messageSendJobID: String
254+
): AttachmentUploadJob
248255

249256
override fun create(data: Data): AttachmentUploadJob? {
250257
val serializedMessage = data.getByteArray(MESSAGE_KEY)
@@ -259,22 +266,12 @@ class AttachmentUploadJob @AssistedInject constructor(
259266
return null
260267
}
261268
input.close()
262-
return factory.create(
269+
return create(
263270
attachmentID = data.getLong(ATTACHMENT_ID_KEY),
264271
threadID = data.getString(THREAD_ID_KEY)!!,
265272
message = message,
266273
messageSendJobID = data.getString(MESSAGE_SEND_JOB_ID_KEY)!!
267274
)
268275
}
269276
}
270-
271-
@AssistedFactory
272-
interface Factory {
273-
fun create(
274-
attachmentID: Long,
275-
@Assisted("threadID") threadID: String,
276-
message: Message,
277-
messageSendJobID: String
278-
): AttachmentUploadJob
279-
}
280277
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ data class MessageReceiveParameters(
5151
val closedGroup: Destination.ClosedGroup? = null
5252
)
5353

54+
@Deprecated("BatchMessageReceiveJob is now only here so that existing persisted jobs can be processed.")
5455
class BatchMessageReceiveJob @AssistedInject constructor(
5556
@Assisted private val messages: List<MessageReceiveParameters>,
5657
@Assisted val fromCommunity: Address.Community?, // The community the messages are received in, if any
@@ -62,6 +63,7 @@ class BatchMessageReceiveJob @AssistedInject constructor(
6263
private val messageNotifier: MessageNotifier,
6364
private val threadDatabase: ThreadDatabase,
6465
private val recipientRepository: RecipientRepository,
66+
private val messageReceiver: MessageReceiver,
6567
) : Job {
6668

6769
override var delegate: JobDelegate? = null
@@ -105,6 +107,7 @@ class BatchMessageReceiveJob @AssistedInject constructor(
105107
fromCommunity = fromCommunity,
106108
threadDatabase = threadDatabase,
107109
recipientRepository = recipientRepository,
110+
messageReceiver = messageReceiver,
108111
)
109112
}
110113

@@ -157,7 +160,7 @@ class BatchMessageReceiveJob @AssistedInject constructor(
157160
messages.forEach { messageParameters ->
158161
val (data, serverHash, openGroupMessageServerID) = messageParameters
159162
try {
160-
val (message, proto) = MessageReceiver.parse(
163+
val (message, proto) = messageReceiver.parse(
161164
data,
162165
openGroupMessageServerID,
163166
openGroupPublicKey = serverPublicKey,
@@ -358,7 +361,8 @@ class BatchMessageReceiveJob @AssistedInject constructor(
358361

359362
@AssistedFactory
360363
abstract class Factory : Job.DeserializeFactory<BatchMessageReceiveJob> {
361-
abstract fun create(
364+
@Deprecated("New code should try to handle message directly instead of creating this job")
365+
protected abstract fun create(
362366
messages: List<MessageReceiveParameters>,
363367
fromCommunity: Address.Community?,
364368
): BatchMessageReceiveJob

app/src/main/java/org/session/libsession/messaging/jobs/InviteContactsJob.kt

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package org.session.libsession.messaging.jobs
22

33
import android.widget.Toast
44
import com.google.protobuf.ByteString
5+
import dagger.assisted.Assisted
6+
import dagger.assisted.AssistedFactory
7+
import dagger.assisted.AssistedInject
58
import kotlinx.coroutines.Dispatchers
69
import kotlinx.coroutines.async
710
import kotlinx.coroutines.awaitAll
@@ -16,13 +19,19 @@ import org.session.libsession.messaging.sending_receiving.MessageSender
1619
import org.session.libsession.messaging.utilities.Data
1720
import org.session.libsession.messaging.utilities.MessageAuthentication.buildGroupInviteSignature
1821
import org.session.libsession.snode.SnodeAPI
22+
import org.session.libsession.utilities.ConfigFactoryProtocol
1923
import org.session.libsession.utilities.getGroup
2024
import org.session.libsignal.protos.SignalServiceProtos.DataMessage.GroupUpdateInviteMessage
2125
import org.session.libsignal.protos.SignalServiceProtos.DataMessage.GroupUpdateMessage
2226
import org.session.libsignal.utilities.AccountId
2327
import org.session.libsignal.utilities.Log
2428

25-
class InviteContactsJob(val groupSessionId: String, val memberSessionIds: Array<String>) : Job {
29+
class InviteContactsJob @AssistedInject constructor(
30+
@Assisted val groupSessionId: String,
31+
@Assisted val memberSessionIds: Array<String>,
32+
private val configFactory: ConfigFactoryProtocol,
33+
private val messageSender: MessageSender,
34+
) : Job {
2635

2736
companion object {
2837
const val KEY = "InviteContactJob"
@@ -37,8 +46,7 @@ class InviteContactsJob(val groupSessionId: String, val memberSessionIds: Array<
3746
override val maxFailureCount: Int = 1
3847

3948
override suspend fun execute(dispatcherName: String) {
40-
val configs = MessagingModuleConfiguration.shared.configFactory
41-
val group = requireNotNull(configs.getGroup(AccountId(groupSessionId))) {
49+
val group = requireNotNull(configFactory.getGroup(AccountId(groupSessionId))) {
4250
"Group must exist to invite"
4351
}
4452

@@ -54,7 +62,7 @@ class InviteContactsJob(val groupSessionId: String, val memberSessionIds: Array<
5462
runCatching {
5563
// Make the request for this member
5664
val memberId = AccountId(memberSessionId)
57-
val (groupName, subAccount) = configs.withMutableGroupConfigs(sessionId) { configs ->
65+
val (groupName, subAccount) = configFactory.withMutableGroupConfigs(sessionId) { configs ->
5866
configs.groupInfo.getName() to configs.groupKeys.makeSubAccount(memberSessionId)
5967
}
6068

@@ -76,14 +84,14 @@ class InviteContactsJob(val groupSessionId: String, val memberSessionIds: Array<
7684
sentTimestamp = timestamp
7785
}
7886

79-
MessageSender.sendNonDurably(update, Destination.Contact(memberSessionId), false)
87+
messageSender.sendNonDurably(update, Destination.Contact(memberSessionId), false)
8088
}
8189
}
8290
}
8391

8492
val results = memberSessionIds.zip(requests.awaitAll())
8593

86-
configs.withMutableGroupConfigs(sessionId) { configs ->
94+
configFactory.withMutableGroupConfigs(sessionId) { configs ->
8795
results.forEach { (memberSessionId, result) ->
8896
configs.groupMembers.get(memberSessionId)?.let { member ->
8997
if (result.isFailure) {
@@ -96,8 +104,8 @@ class InviteContactsJob(val groupSessionId: String, val memberSessionIds: Array<
96104
}
97105
}
98106

99-
val groupName = configs.withGroupConfigs(sessionId) { it.groupInfo.getName() }
100-
?: configs.getGroup(sessionId)?.name
107+
val groupName = configFactory.withGroupConfigs(sessionId) { it.groupInfo.getName() }
108+
?: configFactory.getGroup(sessionId)?.name
101109

102110
// Gather all the exceptions, while keeping track of the invitee account IDs
103111
val failures = results.mapNotNull { (id, result) ->
@@ -140,4 +148,20 @@ class InviteContactsJob(val groupSessionId: String, val memberSessionIds: Array<
140148

141149
override fun getFactoryKey(): String = KEY
142150

151+
@AssistedFactory
152+
abstract class Factory : Job.DeserializeFactory<InviteContactsJob> {
153+
abstract fun create(
154+
groupSessionId: String,
155+
memberSessionIds: Array<String>,
156+
): InviteContactsJob
157+
158+
override fun create(data: Data): InviteContactsJob? {
159+
val groupSessionId = data.getString(GROUP) ?: return null
160+
val memberSessionIds = data.getStringArray(MEMBER) ?: return null
161+
return create(
162+
groupSessionId = groupSessionId,
163+
memberSessionIds = memberSessionIds,
164+
)
165+
}
166+
}
143167
}

app/src/main/java/org/session/libsession/messaging/jobs/MessageSendJob.kt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class MessageSendJob @AssistedInject constructor(
3434
private val messageDataProvider: MessageDataProvider,
3535
private val storage: StorageProtocol,
3636
private val configFactory: ConfigFactoryProtocol,
37+
private val messageSender: MessageSender,
3738
) : Job {
3839

3940
object AwaitingAttachmentUploadException : Exception("Awaiting attachment upload.")
@@ -97,7 +98,7 @@ class MessageSendJob @AssistedInject constructor(
9798
}
9899
}
99100

100-
MessageSender.sendNonDurably(this@MessageSendJob.message, destination, isSync)
101+
messageSender.sendNonDurably(this@MessageSendJob.message, destination, isSync)
101102

102103
this.handleSuccess(dispatcherName)
103104
statusCallback?.trySend(Result.success(Unit))
@@ -173,7 +174,14 @@ class MessageSendJob @AssistedInject constructor(
173174
return KEY
174175
}
175176

176-
class DeserializeFactory(private val factory: Factory) : Job.DeserializeFactory<MessageSendJob> {
177+
178+
@AssistedFactory
179+
abstract class Factory : Job.DeserializeFactory<MessageSendJob> {
180+
abstract fun create(
181+
message: Message,
182+
destination: Destination,
183+
statusCallback: SendChannel<Result<Unit>>? = null
184+
): MessageSendJob
177185

178186
override fun create(data: Data): MessageSendJob? {
179187
val serializedMessage = data.getByteArray(MESSAGE_KEY)
@@ -201,20 +209,11 @@ class MessageSendJob @AssistedInject constructor(
201209
}
202210
destinationInput.close()
203211
// Return
204-
return factory.create(
212+
return create(
205213
message = message,
206214
destination = destination,
207215
statusCallback = null
208216
)
209217
}
210218
}
211-
212-
@AssistedFactory
213-
interface Factory {
214-
fun create(
215-
message: Message,
216-
destination: Destination,
217-
statusCallback: SendChannel<Result<Unit>>? = null
218-
): MessageSendJob
219-
}
220219
}

app/src/main/java/org/session/libsession/messaging/jobs/SessionJobManagerFactories.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,20 @@ class SessionJobManagerFactories @Inject constructor(
88
private val batchFactory: BatchMessageReceiveJob.Factory,
99
private val trimThreadFactory: TrimThreadJob.Factory,
1010
private val messageSendJobFactory: MessageSendJob.Factory,
11-
private val deleteJobFactory: OpenGroupDeleteJob.Factory
11+
private val deleteJobFactory: OpenGroupDeleteJob.Factory,
12+
private val inviteContactsJobFactory: InviteContactsJob.Factory,
1213
) {
1314

1415
fun getSessionJobFactories(): Map<String, Job.DeserializeFactory<out Job>> {
1516
return mapOf(
16-
AttachmentDownloadJob.KEY to AttachmentDownloadJob.DeserializeFactory(attachmentDownloadJobFactory),
17-
AttachmentUploadJob.KEY to AttachmentUploadJob.DeserializeFactory(attachmentUploadJobFactory),
18-
MessageSendJob.KEY to MessageSendJob.DeserializeFactory(messageSendJobFactory),
17+
AttachmentDownloadJob.KEY to attachmentDownloadJobFactory,
18+
AttachmentUploadJob.KEY to attachmentUploadJobFactory,
19+
MessageSendJob.KEY to messageSendJobFactory,
1920
NotifyPNServerJob.KEY to NotifyPNServerJob.DeserializeFactory(),
2021
TrimThreadJob.KEY to trimThreadFactory,
2122
BatchMessageReceiveJob.KEY to batchFactory,
2223
OpenGroupDeleteJob.KEY to deleteJobFactory,
24+
InviteContactsJob.KEY to inviteContactsJobFactory,
2325
)
2426
}
2527
}

app/src/main/java/org/session/libsession/messaging/messages/Destination.kt

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ sealed class Destination {
88
data class Contact(var publicKey: String) : Destination() {
99
internal constructor(): this("")
1010
}
11-
data class LegacyClosedGroup(var groupPublicKey: String) : Destination() {
12-
internal constructor(): this("")
13-
}
14-
data class LegacyOpenGroup(var roomToken: String, var server: String) : Destination() {
15-
internal constructor(): this("", "")
16-
}
1711
data class ClosedGroup(var publicKey: String): Destination() {
1812
internal constructor(): this("")
1913
}
@@ -39,9 +33,6 @@ sealed class Destination {
3933
is Address.Standard -> {
4034
Contact(address.address)
4135
}
42-
is Address.LegacyGroup -> {
43-
LegacyClosedGroup(address.groupPublicKeyHex)
44-
}
4536
is Address.Community -> {
4637
OpenGroup(roomToken = address.room, server = address.serverUrl, fileIds = fileIds)
4738
}
@@ -63,9 +54,10 @@ sealed class Destination {
6354
is Address.Group -> {
6455
ClosedGroup(address.accountId.hexString)
6556
}
66-
else -> {
67-
throw Exception("TODO: Handle legacy closed groups.")
68-
}
57+
58+
is Address.Blinded,
59+
is Address.LegacyGroup,
60+
is Address.Unknown -> error("Unsupported address as destination: $address")
6961
}
7062
}
7163
}

0 commit comments

Comments
 (0)