2020-09-24 06:48:51 +02:00
|
|
|
package org.thoughtcrime.securesms.loki.api
|
|
|
|
|
|
|
|
import android.content.BroadcastReceiver
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Intent
|
|
|
|
import androidx.work.*
|
|
|
|
import nl.komponents.kovenant.Promise
|
|
|
|
import nl.komponents.kovenant.all
|
|
|
|
import nl.komponents.kovenant.functional.map
|
2021-03-23 01:12:37 +01:00
|
|
|
import org.session.libsession.messaging.jobs.MessageReceiveJob
|
2021-05-03 09:58:02 +02:00
|
|
|
import org.session.libsession.messaging.open_groups.OpenGroupV2
|
2021-04-26 07:55:13 +02:00
|
|
|
import org.session.libsession.messaging.sending_receiving.pollers.ClosedGroupPoller
|
2021-03-23 01:12:37 +01:00
|
|
|
import org.session.libsession.messaging.sending_receiving.pollers.OpenGroupPoller
|
2021-05-03 09:58:02 +02:00
|
|
|
import org.session.libsession.messaging.sending_receiving.pollers.OpenGroupV2Poller
|
2021-04-23 08:09:47 +02:00
|
|
|
import org.session.libsession.snode.SnodeAPI
|
2021-01-18 23:12:44 +01:00
|
|
|
import org.session.libsession.utilities.TextSecurePreferences
|
2021-05-18 01:12:33 +02:00
|
|
|
import org.session.libsignal.utilities.Log
|
2021-03-19 07:08:31 +01:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
2020-09-24 06:48:51 +02:00
|
|
|
import java.util.concurrent.TimeUnit
|
|
|
|
|
|
|
|
class BackgroundPollWorker(val context: Context, params: WorkerParameters) : Worker(context, params) {
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
const val TAG = "BackgroundPollWorker"
|
|
|
|
|
|
|
|
@JvmStatic
|
|
|
|
fun schedulePeriodic(context: Context) {
|
|
|
|
Log.v(TAG, "Scheduling periodic work.")
|
2021-05-13 01:24:13 +02:00
|
|
|
val builder = PeriodicWorkRequestBuilder<BackgroundPollWorker>(5, TimeUnit.MINUTES)
|
|
|
|
builder.setConstraints(Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build())
|
|
|
|
val workRequest = builder.build()
|
|
|
|
WorkManager.getInstance(context).enqueueUniquePeriodicWork(
|
|
|
|
TAG,
|
|
|
|
ExistingPeriodicWorkPolicy.REPLACE,
|
|
|
|
workRequest
|
|
|
|
)
|
2020-09-24 06:48:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun doWork(): Result {
|
|
|
|
if (TextSecurePreferences.getLocalNumber(context) == null) {
|
2021-05-13 01:24:13 +02:00
|
|
|
Log.v(TAG, "User not registered yet.")
|
2020-09-24 06:48:51 +02:00
|
|
|
return Result.failure()
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Log.v(TAG, "Performing background poll.")
|
|
|
|
val promises = mutableListOf<Promise<Unit, Exception>>()
|
|
|
|
|
2021-05-13 01:24:13 +02:00
|
|
|
// DMs
|
2021-01-18 23:12:44 +01:00
|
|
|
val userPublicKey = TextSecurePreferences.getLocalNumber(context)!!
|
2021-05-13 01:24:13 +02:00
|
|
|
val dmsPromise = SnodeAPI.getMessages(userPublicKey).map { envelopes ->
|
2021-03-23 01:12:37 +01:00
|
|
|
envelopes.map { envelope ->
|
2021-05-12 08:17:25 +02:00
|
|
|
// FIXME: Using a job here seems like a bad idea...
|
2021-05-19 01:03:49 +02:00
|
|
|
MessageReceiveJob(envelope.toByteArray()).executeAsync()
|
2020-09-24 06:48:51 +02:00
|
|
|
}
|
|
|
|
}
|
2021-05-13 01:24:13 +02:00
|
|
|
promises.addAll(dmsPromise.get())
|
2020-09-24 06:48:51 +02:00
|
|
|
|
|
|
|
// Closed groups
|
2021-04-26 07:55:13 +02:00
|
|
|
promises.addAll(ClosedGroupPoller().pollOnce())
|
2020-09-24 06:48:51 +02:00
|
|
|
|
|
|
|
// Open Groups
|
2021-05-13 01:24:13 +02:00
|
|
|
val v2OpenGroups = DatabaseFactory.getLokiThreadDatabase(context).getAllV2OpenGroups().values.groupBy(OpenGroupV2::server)
|
2021-05-03 09:58:02 +02:00
|
|
|
|
2021-05-13 01:24:13 +02:00
|
|
|
v2OpenGroups.values.map { groups ->
|
2021-05-03 09:58:02 +02:00
|
|
|
OpenGroupV2Poller(groups)
|
|
|
|
}.forEach { poller ->
|
2021-05-13 01:24:13 +02:00
|
|
|
promises.add(poller.compactPoll(true).map { })
|
2021-05-03 09:58:02 +02:00
|
|
|
}
|
|
|
|
|
2021-05-13 01:24:13 +02:00
|
|
|
// Wait until all the promises are resolved
|
2020-09-24 06:48:51 +02:00
|
|
|
all(promises).get()
|
|
|
|
|
|
|
|
return Result.success()
|
|
|
|
} catch (exception: Exception) {
|
2021-05-13 01:24:13 +02:00
|
|
|
Log.e(TAG, "Background poll failed due to error: ${exception.message}.", exception)
|
|
|
|
return Result.retry()
|
2020-09-24 06:48:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class BootBroadcastReceiver: BroadcastReceiver() {
|
|
|
|
|
|
|
|
override fun onReceive(context: Context, intent: Intent) {
|
|
|
|
if (intent.action == Intent.ACTION_BOOT_COMPLETED) {
|
|
|
|
Log.v(TAG, "Boot broadcast caught.")
|
2021-05-13 01:24:13 +02:00
|
|
|
schedulePeriodic(context)
|
2020-09-24 06:48:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|