mirror of
https://github.com/oxen-io/session-android.git
synced 2025-08-11 17:27:42 +00:00
cleanup
This commit is contained in:
@@ -3,14 +3,13 @@ package org.session.libsession.messaging.jobs
|
||||
import com.esotericsoftware.kryo.Kryo
|
||||
import com.esotericsoftware.kryo.io.Input
|
||||
import com.esotericsoftware.kryo.io.Output
|
||||
import nl.komponents.kovenant.functional.map
|
||||
import okhttp3.MediaType
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody
|
||||
import org.session.libsession.messaging.jobs.Job.Companion.MAX_BUFFER_SIZE
|
||||
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationAPI
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationAPI.server
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.LegacyGroupsPushManager
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.LegacyGroupsPushManager.server
|
||||
import org.session.libsession.messaging.utilities.Data
|
||||
import org.session.libsession.snode.SnodeMessage
|
||||
import org.session.libsession.snode.OnionRequestAPI
|
||||
@@ -42,7 +41,7 @@ class NotifyPNServerJob(val message: SnodeMessage) : Job {
|
||||
OnionRequestAPI.sendOnionRequest(
|
||||
request,
|
||||
server,
|
||||
PushNotificationAPI.serverPublicKey,
|
||||
LegacyGroupsPushManager.serverPublicKey,
|
||||
Version.V2
|
||||
) success { response ->
|
||||
when (response.info["code"]) {
|
||||
|
@@ -8,7 +8,7 @@ import nl.komponents.kovenant.deferred
|
||||
import org.session.libsession.messaging.MessagingModuleConfiguration
|
||||
import org.session.libsession.messaging.messages.control.ClosedGroupControlMessage
|
||||
import org.session.libsession.messaging.sending_receiving.MessageSender.Error
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationAPI
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.LegacyGroupsPushManager
|
||||
import org.session.libsession.messaging.sending_receiving.pollers.ClosedGroupPollerV2
|
||||
import org.session.libsession.snode.SnodeAPI
|
||||
import org.session.libsession.utilities.Address
|
||||
@@ -83,7 +83,7 @@ fun MessageSender.create(name: String, members: Collection<String>): Promise<Str
|
||||
}
|
||||
|
||||
// Notify the PN server
|
||||
PushNotificationAPI.subscribeGroup(groupPublicKey, userPublicKey)
|
||||
LegacyGroupsPushManager.register(publicKey = userPublicKey)
|
||||
// Start polling
|
||||
ClosedGroupPollerV2.shared.startPolling(groupPublicKey)
|
||||
// Fulfill the promise
|
||||
|
@@ -23,7 +23,7 @@ import org.session.libsession.messaging.open_groups.OpenGroupApi
|
||||
import org.session.libsession.messaging.sending_receiving.attachments.PointerAttachment
|
||||
import org.session.libsession.messaging.sending_receiving.data_extraction.DataExtractionNotificationInfoMessage
|
||||
import org.session.libsession.messaging.sending_receiving.link_preview.LinkPreview
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationAPI
|
||||
import org.session.libsession.messaging.sending_receiving.notifications.LegacyGroupsPushManager
|
||||
import org.session.libsession.messaging.sending_receiving.pollers.ClosedGroupPollerV2
|
||||
import org.session.libsession.messaging.sending_receiving.quotes.QuoteModel
|
||||
import org.session.libsession.messaging.utilities.SessionId
|
||||
@@ -479,7 +479,7 @@ private fun handleNewClosedGroup(sender: String, sentTimestamp: Long, groupPubli
|
||||
// Set expiration timer
|
||||
storage.setExpirationTimer(groupID, expireTimer)
|
||||
// Notify the PN server
|
||||
PushNotificationAPI.subscribeGroup(groupPublicKey)
|
||||
LegacyGroupsPushManager.register(publicKey = userPublicKey)
|
||||
// Notify the user
|
||||
if (userPublicKey == sender && !groupExists) {
|
||||
val threadID = storage.getOrCreateThreadIdFor(Address.fromSerialized(groupID))
|
||||
@@ -772,7 +772,7 @@ fun MessageReceiver.disableLocalGroupAndUnsubscribe(groupPublicKey: String, grou
|
||||
storage.setActive(groupID, false)
|
||||
storage.removeMember(groupID, Address.fromSerialized(userPublicKey))
|
||||
// Notify the PN server
|
||||
PushNotificationAPI.unsubscribeGroup(groupPublicKey, userPublicKey)
|
||||
LegacyGroupsPushManager.register(publicKey = userPublicKey)
|
||||
// Stop polling
|
||||
ClosedGroupPollerV2.shared.stopPolling(groupPublicKey)
|
||||
}
|
||||
|
@@ -4,7 +4,6 @@ import android.annotation.SuppressLint
|
||||
import nl.komponents.kovenant.Promise
|
||||
import nl.komponents.kovenant.all
|
||||
import nl.komponents.kovenant.functional.map
|
||||
import nl.komponents.kovenant.task
|
||||
import okhttp3.MediaType
|
||||
import okhttp3.Request
|
||||
import okhttp3.RequestBody
|
||||
@@ -18,50 +17,50 @@ import org.session.libsignal.utilities.emptyPromise
|
||||
import org.session.libsignal.utilities.retryIfNeeded
|
||||
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
object PushNotificationAPI {
|
||||
private const val TAG = "PushNotificationAPI"
|
||||
object LegacyGroupsPushManager {
|
||||
private const val TAG = "LegacyGroupsPushManager"
|
||||
|
||||
val context = MessagingModuleConfiguration.shared.context
|
||||
const val server = "https://push.getsession.org"
|
||||
const val serverPublicKey: String = "d7557fe563e2610de876c0ac7341b62f3c82d5eea4b62c702392ea4368f51b3b"
|
||||
private const val legacyServer = "https://live.apns.getsession.org"
|
||||
private const val legacyServer = "https://dev.apns.getsession.org"
|
||||
private const val legacyServerPublicKey = "642a6585919742e5a2d4dc51244964fbcd8bcab2b75612407de58b810740d049"
|
||||
private const val maxRetryCount = 4
|
||||
|
||||
private enum class ClosedGroupOperation(val rawValue: String) {
|
||||
Subscribe("subscribe_closed_group"),
|
||||
Unsubscribe("unsubscribe_closed_group");
|
||||
}
|
||||
|
||||
fun register(
|
||||
token: String? = TextSecurePreferences.getLegacyFCMToken(context)
|
||||
): Promise<*, Exception> = all(
|
||||
register(token, TextSecurePreferences.getLocalNumber(context)),
|
||||
subscribeGroups()
|
||||
)
|
||||
|
||||
fun register(token: String?, publicKey: String?): Promise<Unit, Exception> =
|
||||
token: String? = TextSecurePreferences.getFCMToken(context),
|
||||
publicKey: String? = TextSecurePreferences.getLocalNumber(context),
|
||||
legacyGroupPublicKeys: Collection<String> = MessagingModuleConfiguration.shared.storage.getAllClosedGroupPublicKeys()
|
||||
): Promise<Unit, Exception> =
|
||||
retryIfNeeded(maxRetryCount) {
|
||||
val parameters = mapOf("token" to token!!, "pubKey" to publicKey!!)
|
||||
val url = "$legacyServer/register"
|
||||
Log.d(TAG, "register() called")
|
||||
|
||||
val parameters = mapOf(
|
||||
"token" to token!!,
|
||||
"pubKey" to publicKey!!,
|
||||
"legacyGroupPublicKeys" to legacyGroupPublicKeys.takeIf { it.isNotEmpty() }!!
|
||||
)
|
||||
val url = "$legacyServer/register_legacy_groups_only"
|
||||
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
||||
val request = Request.Builder().url(url).post(body).build()
|
||||
|
||||
OnionRequestAPI.sendOnionRequest(request, legacyServer, legacyServerPublicKey, Version.V2).map { response ->
|
||||
when (response.info["code"]) {
|
||||
null, 0 -> throw Exception("error: ${response.info["message"]}.")
|
||||
else -> TextSecurePreferences.setLegacyFCMToken(context, token)
|
||||
else -> Log.d(TAG, "register() success!!")
|
||||
}
|
||||
} fail { exception ->
|
||||
Log.d(TAG, "Couldn't register for FCM due to error: ${exception}.")
|
||||
}
|
||||
} fail { exception ->
|
||||
Log.d(TAG, "Couldn't register for FCM due to error: ${exception}.")
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister push notifications for 1-1 conversations as this is now done in FirebasePushManager.
|
||||
*/
|
||||
fun unregister(): Promise<*, Exception> {
|
||||
val token = TextSecurePreferences.getLegacyFCMToken(context) ?: emptyPromise()
|
||||
Log.d(TAG, "unregister() called")
|
||||
|
||||
val token = TextSecurePreferences.getFCMToken(context) ?: emptyPromise()
|
||||
|
||||
return retryIfNeeded(maxRetryCount) {
|
||||
val parameters = mapOf( "token" to token )
|
||||
@@ -75,13 +74,14 @@ object PushNotificationAPI {
|
||||
legacyServerPublicKey,
|
||||
Version.V2
|
||||
) success {
|
||||
android.util.Log.d(TAG, "unregister() success!!")
|
||||
|
||||
when (it.info["code"]) {
|
||||
null, 0 -> throw Exception("error: ${it.info["message"]}.")
|
||||
else -> Log.d(TAG, "unregisterV1 success token: $token")
|
||||
}
|
||||
TextSecurePreferences.clearLegacyFCMToken(context)
|
||||
} fail {
|
||||
exception -> Log.d(TAG, "Couldn't disable FCM with token: $token due to error: ${exception}.")
|
||||
Log.d(TAG, "Couldn't disable FCM with token: $token due to error: $it.")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -91,30 +91,20 @@ object PushNotificationAPI {
|
||||
fun subscribeGroup(
|
||||
closedGroupPublicKey: String,
|
||||
publicKey: String = MessagingModuleConfiguration.shared.storage.getUserPublicKey()!!
|
||||
) = performGroupOperation(ClosedGroupOperation.Subscribe, closedGroupPublicKey, publicKey)
|
||||
|
||||
private fun subscribeGroups(
|
||||
closedGroupPublicKeys: Collection<String> = MessagingModuleConfiguration.shared.storage.getAllClosedGroupPublicKeys(),
|
||||
publicKey: String = MessagingModuleConfiguration.shared.storage.getUserPublicKey()!!
|
||||
) = closedGroupPublicKeys.map { performGroupOperation(ClosedGroupOperation.Subscribe, it, publicKey) }.let(::all)
|
||||
) = performGroupOperation("subscribe_closed_group", closedGroupPublicKey, publicKey)
|
||||
|
||||
fun unsubscribeGroup(
|
||||
closedGroupPublicKey: String,
|
||||
publicKey: String = MessagingModuleConfiguration.shared.storage.getUserPublicKey()!!
|
||||
) = performGroupOperation(ClosedGroupOperation.Unsubscribe, closedGroupPublicKey, publicKey)
|
||||
|
||||
private fun unsubscribeGroups(
|
||||
closedGroupPublicKeys: Collection<String> = MessagingModuleConfiguration.shared.storage.getAllClosedGroupPublicKeys(),
|
||||
publicKey: String = MessagingModuleConfiguration.shared.storage.getUserPublicKey()!!
|
||||
) = closedGroupPublicKeys.map { performGroupOperation(ClosedGroupOperation.Unsubscribe, it, publicKey) }.let(::all)
|
||||
) = performGroupOperation("unsubscribe_closed_group", closedGroupPublicKey, publicKey)
|
||||
|
||||
private fun performGroupOperation(
|
||||
operation: ClosedGroupOperation,
|
||||
operation: String,
|
||||
closedGroupPublicKey: String,
|
||||
publicKey: String
|
||||
): Promise<*, Exception> {
|
||||
val parameters = mapOf( "closedGroupPublicKey" to closedGroupPublicKey, "pubKey" to publicKey )
|
||||
val url = "$legacyServer/${operation.rawValue}"
|
||||
val url = "$legacyServer/$operation"
|
||||
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
||||
val request = Request.Builder().url(url).post(body).build()
|
||||
|
||||
@@ -127,10 +117,10 @@ object PushNotificationAPI {
|
||||
) success {
|
||||
when (it.info["code"]) {
|
||||
null, 0 -> throw Exception("${it.info["message"]}")
|
||||
else -> Log.d(TAG, "performGroupOperation success: ${operation.rawValue}")
|
||||
else -> Log.d(TAG, "performGroupOperation success: $operation")
|
||||
}
|
||||
} fail { exception ->
|
||||
Log.d(TAG, "performGroupOperation fail: ${operation.rawValue}: $closedGroupPublicKey due to error: ${exception}.")
|
||||
Log.d(TAG, "performGroupOperation fail: $operation: $closedGroupPublicKey due to error: ${exception}.")
|
||||
}
|
||||
}
|
||||
}
|
@@ -250,7 +250,6 @@ interface TextSecurePreferences {
|
||||
const val GIF_METADATA_WARNING = "has_seen_gif_metadata_warning"
|
||||
const val GIF_GRID_LAYOUT = "pref_gif_grid_layout"
|
||||
const val IS_USING_FCM = "pref_is_using_fcm"
|
||||
const val FCM_TOKEN_LEGACY = "pref_fcm_token"
|
||||
const val FCM_TOKEN = "pref_fcm_token_2"
|
||||
const val LAST_FCM_TOKEN_UPLOAD_TIME = "pref_last_fcm_token_upload_time_2"
|
||||
const val LAST_CONFIGURATION_SYNC_TIME = "pref_last_configuration_sync_time"
|
||||
@@ -313,21 +312,6 @@ interface TextSecurePreferences {
|
||||
setBooleanPreference(context, IS_USING_FCM, value)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun getLegacyFCMToken(context: Context): String? {
|
||||
return getStringPreference(context, FCM_TOKEN_LEGACY, "")
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun setLegacyFCMToken(context: Context, token: String?) {
|
||||
setStringPreference(context, FCM_TOKEN_LEGACY, token)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun clearLegacyFCMToken(context: Context) {
|
||||
removePreference(context, FCM_TOKEN_LEGACY)
|
||||
}
|
||||
|
||||
@JvmStatic
|
||||
fun getFCMToken(context: Context): String? {
|
||||
return getStringPreference(context, FCM_TOKEN, "")
|
||||
|
Reference in New Issue
Block a user