mirror of
https://github.com/oxen-io/session-android.git
synced 2025-02-18 23:18:26 +00:00
Remove PushNotificationManager
This commit is contained in:
parent
ba6eca2443
commit
3f6229f841
@ -36,6 +36,7 @@ import org.session.libsession.avatars.AvatarHelper;
|
|||||||
import org.session.libsession.database.MessageDataProvider;
|
import org.session.libsession.database.MessageDataProvider;
|
||||||
import org.session.libsession.messaging.MessagingModuleConfiguration;
|
import org.session.libsession.messaging.MessagingModuleConfiguration;
|
||||||
import org.session.libsession.messaging.sending_receiving.notifications.MessageNotifier;
|
import org.session.libsession.messaging.sending_receiving.notifications.MessageNotifier;
|
||||||
|
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationAPI;
|
||||||
import org.session.libsession.messaging.sending_receiving.pollers.ClosedGroupPollerV2;
|
import org.session.libsession.messaging.sending_receiving.pollers.ClosedGroupPollerV2;
|
||||||
import org.session.libsession.messaging.sending_receiving.pollers.Poller;
|
import org.session.libsession.messaging.sending_receiving.pollers.Poller;
|
||||||
import org.session.libsession.snode.SnodeModule;
|
import org.session.libsession.snode.SnodeModule;
|
||||||
@ -78,7 +79,6 @@ import org.thoughtcrime.securesms.notifications.DefaultMessageNotifier;
|
|||||||
import org.thoughtcrime.securesms.notifications.NotificationChannels;
|
import org.thoughtcrime.securesms.notifications.NotificationChannels;
|
||||||
import org.thoughtcrime.securesms.notifications.OptimizedMessageNotifier;
|
import org.thoughtcrime.securesms.notifications.OptimizedMessageNotifier;
|
||||||
import org.thoughtcrime.securesms.notifications.PushManager;
|
import org.thoughtcrime.securesms.notifications.PushManager;
|
||||||
import org.thoughtcrime.securesms.notifications.PushNotificationManager;
|
|
||||||
import org.thoughtcrime.securesms.providers.BlobProvider;
|
import org.thoughtcrime.securesms.providers.BlobProvider;
|
||||||
import org.thoughtcrime.securesms.service.ExpiringMessageManager;
|
import org.thoughtcrime.securesms.service.ExpiringMessageManager;
|
||||||
import org.thoughtcrime.securesms.service.KeyCachingService;
|
import org.thoughtcrime.securesms.service.KeyCachingService;
|
||||||
@ -440,7 +440,7 @@ public class ApplicationContext extends Application implements DefaultLifecycleO
|
|||||||
private static class ProviderInitializationException extends RuntimeException { }
|
private static class ProviderInitializationException extends RuntimeException { }
|
||||||
|
|
||||||
public void registerForPnIfNeeded(final Boolean force) {
|
public void registerForPnIfNeeded(final Boolean force) {
|
||||||
pushManager.register(force);
|
pushManager.refresh(force);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setUpPollingIfNeeded() {
|
private void setUpPollingIfNeeded() {
|
||||||
@ -514,7 +514,7 @@ public class ApplicationContext extends Application implements DefaultLifecycleO
|
|||||||
public void clearAllData(boolean isMigratingToV2KeyPair) {
|
public void clearAllData(boolean isMigratingToV2KeyPair) {
|
||||||
String token = TextSecurePreferences.getFCMToken(this);
|
String token = TextSecurePreferences.getFCMToken(this);
|
||||||
if (token != null && !token.isEmpty()) {
|
if (token != null && !token.isEmpty()) {
|
||||||
PushNotificationManager.unregister(token, this);
|
PushNotificationAPI.unregister(token);
|
||||||
}
|
}
|
||||||
if (firebaseInstanceIdJob != null && firebaseInstanceIdJob.isActive()) {
|
if (firebaseInstanceIdJob != null && firebaseInstanceIdJob.isActive()) {
|
||||||
firebaseInstanceIdJob.cancel(null);
|
firebaseInstanceIdJob.cancel(null);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
package org.thoughtcrime.securesms.notifications
|
package org.thoughtcrime.securesms.notifications
|
||||||
|
|
||||||
interface PushManager {
|
interface PushManager {
|
||||||
fun register(force: Boolean)
|
fun refresh(force: Boolean)
|
||||||
}
|
}
|
@ -1,121 +0,0 @@
|
|||||||
package org.thoughtcrime.securesms.notifications
|
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import nl.komponents.kovenant.Promise
|
|
||||||
import nl.komponents.kovenant.functional.map
|
|
||||||
import okhttp3.MediaType
|
|
||||||
import okhttp3.Request
|
|
||||||
import okhttp3.RequestBody
|
|
||||||
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationAPI
|
|
||||||
import org.session.libsession.snode.OnionRequestAPI
|
|
||||||
import org.session.libsession.snode.Version
|
|
||||||
import org.session.libsession.utilities.TextSecurePreferences
|
|
||||||
import org.session.libsignal.utilities.JsonUtil
|
|
||||||
import org.session.libsignal.utilities.Log
|
|
||||||
import org.session.libsignal.utilities.retryIfNeeded
|
|
||||||
import org.thoughtcrime.securesms.dependencies.DatabaseComponent
|
|
||||||
|
|
||||||
object PushNotificationManager {
|
|
||||||
private val maxRetryCount = 4
|
|
||||||
private val tokenExpirationInterval = 12 * 60 * 60 * 1000
|
|
||||||
|
|
||||||
private val server by lazy {
|
|
||||||
PushNotificationAPI.server
|
|
||||||
}
|
|
||||||
private val pnServerPublicKey by lazy {
|
|
||||||
PushNotificationAPI.serverPublicKey
|
|
||||||
}
|
|
||||||
|
|
||||||
enum class ClosedGroupOperation {
|
|
||||||
Subscribe, Unsubscribe;
|
|
||||||
|
|
||||||
val rawValue: String
|
|
||||||
get() {
|
|
||||||
return when (this) {
|
|
||||||
Subscribe -> "subscribe_closed_group"
|
|
||||||
Unsubscribe -> "unsubscribe_closed_group"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@JvmStatic
|
|
||||||
fun unregister(token: String, context: Context) {
|
|
||||||
val parameters = mapOf( "token" to token )
|
|
||||||
val url = "$server/unregister"
|
|
||||||
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
|
||||||
val request = Request.Builder().url(url).post(body)
|
|
||||||
retryIfNeeded(maxRetryCount) {
|
|
||||||
getResponseBody(request.build()).map { json ->
|
|
||||||
val code = json["code"] as? Int
|
|
||||||
if (code != null && code != 0) {
|
|
||||||
TextSecurePreferences.setIsUsingFCM(context, false)
|
|
||||||
} else {
|
|
||||||
Log.d("Loki", "Couldn't disable FCM due to error: ${json["message"] as? String ?: "null"}.")
|
|
||||||
}
|
|
||||||
}.fail { exception ->
|
|
||||||
Log.d("Loki", "Couldn't disable FCM due to error: ${exception}.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Unsubscribe from all closed groups
|
|
||||||
val allClosedGroupPublicKeys = DatabaseComponent.get(context).lokiAPIDatabase().getAllClosedGroupPublicKeys()
|
|
||||||
val userPublicKey = TextSecurePreferences.getLocalNumber(context)!!
|
|
||||||
allClosedGroupPublicKeys.iterator().forEach { closedGroup ->
|
|
||||||
performOperation(context, ClosedGroupOperation.Unsubscribe, closedGroup, userPublicKey)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@JvmStatic
|
|
||||||
fun register(token: String, publicKey: String, context: Context, force: Boolean) {
|
|
||||||
val oldToken = TextSecurePreferences.getFCMToken(context)
|
|
||||||
val lastUploadDate = TextSecurePreferences.getLastFCMUploadTime(context)
|
|
||||||
if (!force && token == oldToken && System.currentTimeMillis() - lastUploadDate < tokenExpirationInterval) { return }
|
|
||||||
val parameters = mapOf( "token" to token, "pubKey" to publicKey )
|
|
||||||
val url = "$server/register"
|
|
||||||
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
|
||||||
val request = Request.Builder().url(url).post(body)
|
|
||||||
retryIfNeeded(maxRetryCount) {
|
|
||||||
getResponseBody(request.build()).map { json ->
|
|
||||||
val code = json["code"] as? Int
|
|
||||||
if (code != null && code != 0) {
|
|
||||||
TextSecurePreferences.setIsUsingFCM(context, true)
|
|
||||||
TextSecurePreferences.setFCMToken(context, token)
|
|
||||||
TextSecurePreferences.setLastFCMUploadTime(context, System.currentTimeMillis())
|
|
||||||
} else {
|
|
||||||
Log.d("Loki", "Couldn't register for FCM due to error: ${json["message"] as? String ?: "null"}.")
|
|
||||||
}
|
|
||||||
}.fail { exception ->
|
|
||||||
Log.d("Loki", "Couldn't register for FCM due to error: ${exception}.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Subscribe to all closed groups
|
|
||||||
val allClosedGroupPublicKeys = DatabaseComponent.get(context).lokiAPIDatabase().getAllClosedGroupPublicKeys()
|
|
||||||
allClosedGroupPublicKeys.iterator().forEach { closedGroup ->
|
|
||||||
performOperation(context, ClosedGroupOperation.Subscribe, closedGroup, publicKey)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@JvmStatic
|
|
||||||
fun performOperation(context: Context, operation: ClosedGroupOperation, closedGroupPublicKey: String, publicKey: String) {
|
|
||||||
if (!TextSecurePreferences.isUsingFCM(context)) { return }
|
|
||||||
val parameters = mapOf( "closedGroupPublicKey" to closedGroupPublicKey, "pubKey" to publicKey )
|
|
||||||
val url = "$server/${operation.rawValue}"
|
|
||||||
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
|
||||||
val request = Request.Builder().url(url).post(body)
|
|
||||||
retryIfNeeded(maxRetryCount) {
|
|
||||||
getResponseBody(request.build()).map { json ->
|
|
||||||
val code = json["code"] as? Int
|
|
||||||
if (code == null || code == 0) {
|
|
||||||
Log.d("Loki", "Couldn't subscribe/unsubscribe closed group: $closedGroupPublicKey due to error: ${json["message"] as? String ?: "null"}.")
|
|
||||||
}
|
|
||||||
}.fail { exception ->
|
|
||||||
Log.d("Loki", "Couldn't subscribe/unsubscribe closed group: $closedGroupPublicKey due to error: ${exception}.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getResponseBody(request: Request): Promise<Map<*, *>, Exception> {
|
|
||||||
return OnionRequestAPI.sendOnionRequest(request, server, pnServerPublicKey, Version.V2).map { response ->
|
|
||||||
JsonUtil.fromJson(response.body, Map::class.java)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -93,15 +93,15 @@ class FirebasePushManager(private val context: Context, private val prefs: TextS
|
|||||||
return content
|
return content
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun register(force: Boolean) {
|
override fun refresh(force: Boolean) {
|
||||||
firebaseInstanceIdJob?.apply {
|
firebaseInstanceIdJob?.apply {
|
||||||
if (force) cancel() else if (isActive) return
|
if (force) cancel() else if (isActive) return
|
||||||
}
|
}
|
||||||
|
|
||||||
firebaseInstanceIdJob = getFcmInstanceId { register(it, force) }
|
firebaseInstanceIdJob = getFcmInstanceId { refresh(it, force) }
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun register(task: Task<InstanceIdResult>, force: Boolean) {
|
private fun refresh(task: Task<InstanceIdResult>, force: Boolean) {
|
||||||
// context in here is Dispatchers.IO
|
// context in here is Dispatchers.IO
|
||||||
if (!task.isSuccessful) {
|
if (!task.isSuccessful) {
|
||||||
Log.w(
|
Log.w(
|
||||||
|
@ -8,6 +8,7 @@ import dagger.hilt.android.AndroidEntryPoint
|
|||||||
import org.session.libsession.messaging.jobs.BatchMessageReceiveJob
|
import org.session.libsession.messaging.jobs.BatchMessageReceiveJob
|
||||||
import org.session.libsession.messaging.jobs.JobQueue
|
import org.session.libsession.messaging.jobs.JobQueue
|
||||||
import org.session.libsession.messaging.jobs.MessageReceiveParameters
|
import org.session.libsession.messaging.jobs.MessageReceiveParameters
|
||||||
|
import org.session.libsession.messaging.sending_receiving.notifications.PushNotificationAPI
|
||||||
import org.session.libsession.messaging.utilities.MessageWrapper
|
import org.session.libsession.messaging.utilities.MessageWrapper
|
||||||
import org.session.libsession.utilities.TextSecurePreferences
|
import org.session.libsession.utilities.TextSecurePreferences
|
||||||
import org.session.libsignal.utilities.Base64
|
import org.session.libsignal.utilities.Base64
|
||||||
@ -23,7 +24,7 @@ class PushNotificationService : FirebaseMessagingService() {
|
|||||||
super.onNewToken(token)
|
super.onNewToken(token)
|
||||||
Log.d("Loki", "New FCM token: $token.")
|
Log.d("Loki", "New FCM token: $token.")
|
||||||
TextSecurePreferences.getLocalNumber(this) ?: return
|
TextSecurePreferences.getLocalNumber(this) ?: return
|
||||||
pushManager.register(true)
|
pushManager.refresh(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onMessageReceived(message: RemoteMessage) {
|
override fun onMessageReceived(message: RemoteMessage) {
|
||||||
@ -53,22 +54,18 @@ class PushNotificationService : FirebaseMessagingService() {
|
|||||||
Log.d("Loki", "Failed to decode data for message.")
|
Log.d("Loki", "Failed to decode data for message.")
|
||||||
val builder = NotificationCompat.Builder(this, NotificationChannels.OTHER)
|
val builder = NotificationCompat.Builder(this, NotificationChannels.OTHER)
|
||||||
.setSmallIcon(network.loki.messenger.R.drawable.ic_notification)
|
.setSmallIcon(network.loki.messenger.R.drawable.ic_notification)
|
||||||
.setColor(this.getResources().getColor(network.loki.messenger.R.color.textsecure_primary))
|
.setColor(resources.getColor(network.loki.messenger.R.color.textsecure_primary))
|
||||||
.setContentTitle("Session")
|
.setContentTitle("Session")
|
||||||
.setContentText("You've got a new message.")
|
.setContentText("You've got a new message.")
|
||||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||||
.setAutoCancel(true)
|
.setAutoCancel(true)
|
||||||
with(NotificationManagerCompat.from(this)) {
|
NotificationManagerCompat.from(this).notify(11111, builder.build())
|
||||||
notify(11111, builder.build())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onDeletedMessages() {
|
override fun onDeletedMessages() {
|
||||||
Log.d("Loki", "Called onDeletedMessages.")
|
Log.d("Loki", "Called onDeletedMessages.")
|
||||||
super.onDeletedMessages()
|
super.onDeletedMessages()
|
||||||
val token = TextSecurePreferences.getFCMToken(this)!!
|
PushNotificationAPI.register()
|
||||||
val userPublicKey = TextSecurePreferences.getLocalNumber(this) ?: return
|
|
||||||
PushNotificationManager.register(token, userPublicKey, this, true)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -4,7 +4,7 @@ import org.session.libsignal.utilities.Log
|
|||||||
|
|
||||||
class NoOpPushManager: PushManager {
|
class NoOpPushManager: PushManager {
|
||||||
|
|
||||||
override fun register(force: Boolean) {
|
override fun refresh(force: Boolean) {
|
||||||
Log.d("NoOpPushManager", "Push notifications not supported, not registering for push notifications")
|
Log.d("NoOpPushManager", "Push notifications not supported, not registering for push notifications")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -74,7 +74,7 @@ fun MessageSender.create(name: String, members: Collection<String>): Promise<Str
|
|||||||
val threadID = storage.getOrCreateThreadIdFor(Address.fromSerialized(groupID))
|
val threadID = storage.getOrCreateThreadIdFor(Address.fromSerialized(groupID))
|
||||||
storage.insertOutgoingInfoMessage(context, groupID, SignalServiceGroup.Type.CREATION, name, members, admins, threadID, sentTime)
|
storage.insertOutgoingInfoMessage(context, groupID, SignalServiceGroup.Type.CREATION, name, members, admins, threadID, sentTime)
|
||||||
// Notify the PN server
|
// Notify the PN server
|
||||||
PushNotificationAPI.performOperation(PushNotificationAPI.ClosedGroupOperation.Subscribe, groupPublicKey, userPublicKey)
|
PushNotificationAPI.subscribeGroup(groupPublicKey, userPublicKey)
|
||||||
// Start polling
|
// Start polling
|
||||||
ClosedGroupPollerV2.shared.startPolling(groupPublicKey)
|
ClosedGroupPollerV2.shared.startPolling(groupPublicKey)
|
||||||
// Fulfill the promise
|
// Fulfill the promise
|
||||||
@ -95,10 +95,10 @@ fun MessageSender.update(groupPublicKey: String, members: List<String>, name: St
|
|||||||
// Update name if needed
|
// Update name if needed
|
||||||
if (name != group.title) { setName(groupPublicKey, name) }
|
if (name != group.title) { setName(groupPublicKey, name) }
|
||||||
// Add members if needed
|
// Add members if needed
|
||||||
val addedMembers = members - group.members.map { it.serialize() }
|
val addedMembers = members - group.members.map { it.serialize() }.toSet()
|
||||||
if (!addedMembers.isEmpty()) { addMembers(groupPublicKey, addedMembers) }
|
if (addedMembers.isNotEmpty()) { addMembers(groupPublicKey, addedMembers) }
|
||||||
// Remove members if needed
|
// Remove members if needed
|
||||||
val removedMembers = group.members.map { it.serialize() } - members
|
val removedMembers = group.members.map { it.serialize() } - members.toSet()
|
||||||
if (removedMembers.isEmpty()) { removeMembers(groupPublicKey, removedMembers) }
|
if (removedMembers.isEmpty()) { removeMembers(groupPublicKey, removedMembers) }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -195,7 +195,7 @@ fun MessageSender.removeMembers(groupPublicKey: String, membersToRemove: List<St
|
|||||||
Log.d("Loki", "Only an admin can remove members from a group.")
|
Log.d("Loki", "Only an admin can remove members from a group.")
|
||||||
throw Error.InvalidClosedGroupUpdate
|
throw Error.InvalidClosedGroupUpdate
|
||||||
}
|
}
|
||||||
val updatedMembers = group.members.map { it.serialize() }.toSet() - membersToRemove
|
val updatedMembers = group.members.map { it.serialize() }.toSet() - membersToRemove.toSet()
|
||||||
if (membersToRemove.any { it in admins } && updatedMembers.isNotEmpty()) {
|
if (membersToRemove.any { it in admins } && updatedMembers.isNotEmpty()) {
|
||||||
Log.d("Loki", "Can't remove admin from closed group unless the group is destroyed entirely.")
|
Log.d("Loki", "Can't remove admin from closed group unless the group is destroyed entirely.")
|
||||||
throw Error.InvalidClosedGroupUpdate
|
throw Error.InvalidClosedGroupUpdate
|
||||||
|
@ -478,7 +478,7 @@ private fun handleNewClosedGroup(sender: String, sentTimestamp: Long, groupPubli
|
|||||||
// Set expiration timer
|
// Set expiration timer
|
||||||
storage.setExpirationTimer(groupID, expireTimer)
|
storage.setExpirationTimer(groupID, expireTimer)
|
||||||
// Notify the PN server
|
// Notify the PN server
|
||||||
PushNotificationAPI.performOperation(PushNotificationAPI.ClosedGroupOperation.Subscribe, groupPublicKey, storage.getUserPublicKey()!!)
|
PushNotificationAPI.subscribeGroup(groupPublicKey, userPublicKey)
|
||||||
// Notify the user
|
// Notify the user
|
||||||
if (userPublicKey == sender && !groupExists) {
|
if (userPublicKey == sender && !groupExists) {
|
||||||
val threadID = storage.getOrCreateThreadIdFor(Address.fromSerialized(groupID))
|
val threadID = storage.getOrCreateThreadIdFor(Address.fromSerialized(groupID))
|
||||||
@ -771,7 +771,7 @@ fun MessageReceiver.disableLocalGroupAndUnsubscribe(groupPublicKey: String, grou
|
|||||||
storage.setActive(groupID, false)
|
storage.setActive(groupID, false)
|
||||||
storage.removeMember(groupID, Address.fromSerialized(userPublicKey))
|
storage.removeMember(groupID, Address.fromSerialized(userPublicKey))
|
||||||
// Notify the PN server
|
// Notify the PN server
|
||||||
PushNotificationAPI.performOperation(PushNotificationAPI.ClosedGroupOperation.Unsubscribe, groupPublicKey, userPublicKey)
|
PushNotificationAPI.unsubscribeGroup(groupPublicKey, userPublicKey)
|
||||||
// Stop polling
|
// Stop polling
|
||||||
ClosedGroupPollerV2.shared.stopPolling(groupPublicKey)
|
ClosedGroupPollerV2.shared.stopPolling(groupPublicKey)
|
||||||
}
|
}
|
||||||
|
@ -16,69 +16,98 @@ import org.session.libsignal.utilities.retryIfNeeded
|
|||||||
@SuppressLint("StaticFieldLeak")
|
@SuppressLint("StaticFieldLeak")
|
||||||
object PushNotificationAPI {
|
object PushNotificationAPI {
|
||||||
val context = MessagingModuleConfiguration.shared.context
|
val context = MessagingModuleConfiguration.shared.context
|
||||||
val server = "https://push.getsession.org"
|
const val server = "https://push.getsession.org"
|
||||||
val serverPublicKey: String = "d7557fe563e2610de876c0ac7341b62f3c82d5eea4b62c702392ea4368f51b3b"
|
const val serverPublicKey: String = "d7557fe563e2610de876c0ac7341b62f3c82d5eea4b62c702392ea4368f51b3b"
|
||||||
private val legacyServer = "https://live.apns.getsession.org"
|
private const val legacyServer = "https://live.apns.getsession.org"
|
||||||
private val legacyServerPublicKey = "642a6585919742e5a2d4dc51244964fbcd8bcab2b75612407de58b810740d049"
|
private const val legacyServerPublicKey = "642a6585919742e5a2d4dc51244964fbcd8bcab2b75612407de58b810740d049"
|
||||||
private val maxRetryCount = 4
|
private const val maxRetryCount = 4
|
||||||
private val tokenExpirationInterval = 12 * 60 * 60 * 1000
|
|
||||||
|
|
||||||
enum class ClosedGroupOperation {
|
private enum class ClosedGroupOperation(val rawValue: String) {
|
||||||
Subscribe, Unsubscribe;
|
Subscribe("subscribe_closed_group"),
|
||||||
|
Unsubscribe("unsubscribe_closed_group");
|
||||||
val rawValue: String
|
|
||||||
get() {
|
|
||||||
return when (this) {
|
|
||||||
Subscribe -> "subscribe_closed_group"
|
|
||||||
Unsubscribe -> "unsubscribe_closed_group"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun register(token: String? = TextSecurePreferences.getFCMToken(context)) {
|
||||||
|
token?.let(::unregisterV1)
|
||||||
|
subscribeGroups()
|
||||||
|
}
|
||||||
|
|
||||||
|
@JvmStatic
|
||||||
fun unregister(token: String) {
|
fun unregister(token: String) {
|
||||||
|
unregisterV1(token)
|
||||||
|
unsubscribeGroups()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun unregisterV1(token: String) {
|
||||||
val parameters = mapOf( "token" to token )
|
val parameters = mapOf( "token" to token )
|
||||||
val url = "$server/unregister"
|
val url = "$server/unregister"
|
||||||
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
||||||
val request = Request.Builder().url(url).post(body)
|
val request = Request.Builder().url(url).post(body)
|
||||||
retryIfNeeded(maxRetryCount) {
|
retryIfNeeded(maxRetryCount) {
|
||||||
OnionRequestAPI.sendOnionRequest(request.build(), server, serverPublicKey, Version.V2).map { response ->
|
OnionRequestAPI.sendOnionRequest(request.build(), server, serverPublicKey, Version.V2).map { response ->
|
||||||
val code = response.info["code"] as? Int
|
when (response.info["code"]) {
|
||||||
if (code != null && code != 0) {
|
null, 0 -> Log.d("Loki", "Couldn't disable FCM due to error: ${response.info["message"]}.")
|
||||||
TextSecurePreferences.setIsUsingFCM(context, false)
|
|
||||||
} else {
|
|
||||||
Log.d("Loki", "Couldn't disable FCM due to error: ${response.info["message"] as? String ?: "null"}.")
|
|
||||||
}
|
}
|
||||||
}.fail { exception ->
|
}.fail { exception ->
|
||||||
Log.d("Loki", "Couldn't disable FCM due to error: ${exception}.")
|
Log.d("Loki", "Couldn't disable FCM due to error: ${exception}.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Unsubscribe from all closed groups
|
|
||||||
val allClosedGroupPublicKeys = MessagingModuleConfiguration.shared.storage.getAllClosedGroupPublicKeys()
|
|
||||||
val userPublicKey = MessagingModuleConfiguration.shared.storage.getUserPublicKey()!!
|
|
||||||
allClosedGroupPublicKeys.iterator().forEach { closedGroup ->
|
|
||||||
performOperation(ClosedGroupOperation.Unsubscribe, closedGroup, userPublicKey)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun register(token: String, publicKey: String, force: Boolean) {
|
// Legacy Closed Groups
|
||||||
// Subscribe to all closed groups
|
|
||||||
val allClosedGroupPublicKeys = MessagingModuleConfiguration.shared.storage.getAllClosedGroupPublicKeys()
|
fun subscribeGroup(
|
||||||
allClosedGroupPublicKeys.iterator().forEach { closedGroup ->
|
closedGroupPublicKey: String,
|
||||||
performOperation(ClosedGroupOperation.Subscribe, closedGroup, publicKey)
|
publicKey: String = MessagingModuleConfiguration.shared.storage.getUserPublicKey()!!
|
||||||
}
|
) {
|
||||||
|
performGroupOperation(ClosedGroupOperation.Subscribe, closedGroupPublicKey, publicKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun performOperation(operation: ClosedGroupOperation, closedGroupPublicKey: String, publicKey: String) {
|
private fun subscribeGroups(
|
||||||
if (!TextSecurePreferences.isUsingFCM(context)) { return }
|
closedGroupPublicKeys: Collection<String> = MessagingModuleConfiguration.shared.storage.getAllClosedGroupPublicKeys(),
|
||||||
|
publicKey: String = MessagingModuleConfiguration.shared.storage.getUserPublicKey()!!
|
||||||
|
) {
|
||||||
|
performGroupOperations(ClosedGroupOperation.Subscribe, closedGroupPublicKeys, 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()!!
|
||||||
|
) {
|
||||||
|
performGroupOperations(ClosedGroupOperation.Unsubscribe, closedGroupPublicKeys, publicKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun performGroupOperations(
|
||||||
|
operation: ClosedGroupOperation,
|
||||||
|
closedGroupPublicKeys: Collection<String>,
|
||||||
|
publicKey: String
|
||||||
|
) {
|
||||||
|
closedGroupPublicKeys.forEach { performGroupOperation(operation, it, publicKey) }
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun performGroupOperation(
|
||||||
|
operation: ClosedGroupOperation,
|
||||||
|
closedGroupPublicKey: String,
|
||||||
|
publicKey: String
|
||||||
|
) {
|
||||||
|
if (!TextSecurePreferences.isUsingFCM(context)) return
|
||||||
|
|
||||||
val parameters = mapOf( "closedGroupPublicKey" to closedGroupPublicKey, "pubKey" to publicKey )
|
val parameters = mapOf( "closedGroupPublicKey" to closedGroupPublicKey, "pubKey" to publicKey )
|
||||||
val url = "$legacyServer/${operation.rawValue}"
|
val url = "$legacyServer/${operation.rawValue}"
|
||||||
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
|
||||||
val request = Request.Builder().url(url).post(body)
|
val request = Request.Builder().url(url).post(body)
|
||||||
|
|
||||||
retryIfNeeded(maxRetryCount) {
|
retryIfNeeded(maxRetryCount) {
|
||||||
OnionRequestAPI.sendOnionRequest(request.build(), legacyServer, legacyServerPublicKey, Version.V2).map { response ->
|
OnionRequestAPI.sendOnionRequest(request.build(), legacyServer, legacyServerPublicKey, Version.V2).map { response ->
|
||||||
val code = response.info["code"] as? Int
|
when (response.info["code"]) {
|
||||||
if (code == null || code == 0) {
|
null, 0 -> Log.d("Loki", "Couldn't subscribe/unsubscribe closed group: $closedGroupPublicKey due to error: ${response.info["message"]}.")
|
||||||
Log.d("Loki", "Couldn't subscribe/unsubscribe closed group: $closedGroupPublicKey due to error: ${response.info["message"] as? String ?: "null"}.")
|
|
||||||
}
|
}
|
||||||
}.fail { exception ->
|
}.fail { exception ->
|
||||||
Log.d("Loki", "Couldn't subscribe/unsubscribe closed group: $closedGroupPublicKey due to error: ${exception}.")
|
Log.d("Loki", "Couldn't subscribe/unsubscribe closed group: $closedGroupPublicKey due to error: ${exception}.")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user