mirror of
https://github.com/oxen-io/session-android.git
synced 2024-12-25 09:17:44 +00:00
Add missing unsubscribe case
This commit is contained in:
parent
5c2f570277
commit
557eecffd1
@ -17,19 +17,27 @@ import java.util.concurrent.TimeUnit
|
|||||||
|
|
||||||
object LokiPushNotificationManager {
|
object LokiPushNotificationManager {
|
||||||
private val connection = OkHttpClient()
|
private val connection = OkHttpClient()
|
||||||
|
private val tokenExpirationInterval = 12 * 60 * 60 * 1000
|
||||||
|
|
||||||
private val server by lazy {
|
private val server by lazy {
|
||||||
PushNotificationAcknowledgement.shared.server
|
PushNotificationAcknowledgement.shared.server
|
||||||
}
|
}
|
||||||
|
|
||||||
public const val subscribe = "subscribe_closed_group"
|
enum class ClosedGroupOperation {
|
||||||
public const val unsubscribe = "unsubscribe_closed_group"
|
Subscribe, Unsubscribe;
|
||||||
|
|
||||||
private const val tokenExpirationInterval = 12 * 60 * 60 * 1000
|
val rawValue: String
|
||||||
|
get() {
|
||||||
|
return when (this) {
|
||||||
|
Subscribe -> "subscribe_closed_group"
|
||||||
|
Unsubscribe -> "unsubscribe_closed_group"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun unregister(token: String, context: Context?) {
|
fun unregister(token: String, context: Context) {
|
||||||
val parameters = mapOf("token" to token)
|
val parameters = mapOf( "token" to token )
|
||||||
val url = "$server/register"
|
val url = "$server/register"
|
||||||
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).build()
|
val request = Request.Builder().url(url).post(body).build()
|
||||||
@ -54,18 +62,20 @@ object LokiPushNotificationManager {
|
|||||||
Log.d("Loki", "Couldn't disable FCM.")
|
Log.d("Loki", "Couldn't disable FCM.")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
// Unsubscribe from all closed groups
|
||||||
for (closedGroup: String in DatabaseFactory.getSSKDatabase(context).getAllClosedGroupPublicKeys()) {
|
val allClosedGroupPublicKeys = DatabaseFactory.getSSKDatabase(context).getAllClosedGroupPublicKeys()
|
||||||
operateClosedGroup(closedGroup, TextSecurePreferences.getLocalNumber(context), context, unsubscribe)
|
val userPublicKey = TextSecurePreferences.getLocalNumber(context)
|
||||||
|
allClosedGroupPublicKeys.forEach { closedGroup ->
|
||||||
|
performOperation(context, ClosedGroupOperation.Unsubscribe, closedGroup, userPublicKey)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun register(token: String, publicKey: String, context: Context?, force: Boolean) {
|
fun register(token: String, publicKey: String, context: Context, force: Boolean) {
|
||||||
val oldToken = TextSecurePreferences.getFCMToken(context)
|
val oldToken = TextSecurePreferences.getFCMToken(context)
|
||||||
val lastUploadDate = TextSecurePreferences.getLastFCMUploadTime(context)
|
val lastUploadDate = TextSecurePreferences.getLastFCMUploadTime(context)
|
||||||
if (!force && token == oldToken && System.currentTimeMillis() - lastUploadDate < tokenExpirationInterval) { return }
|
if (!force && token == oldToken && System.currentTimeMillis() - lastUploadDate < tokenExpirationInterval) { return }
|
||||||
val parameters = mapOf("token" to token, "pubKey" to publicKey)
|
val parameters = mapOf( "token" to token, "pubKey" to publicKey )
|
||||||
val url = "$server/register"
|
val url = "$server/register"
|
||||||
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).build()
|
val request = Request.Builder().url(url).post(body).build()
|
||||||
@ -92,19 +102,19 @@ object LokiPushNotificationManager {
|
|||||||
Log.d("Loki", "Couldn't register for FCM.")
|
Log.d("Loki", "Couldn't register for FCM.")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
// Subscribe to all closed groups
|
||||||
for (closedGroup: String in DatabaseFactory.getSSKDatabase(context).getAllClosedGroupPublicKeys()) {
|
val allClosedGroupPublicKeys = DatabaseFactory.getSSKDatabase(context).getAllClosedGroupPublicKeys()
|
||||||
operateClosedGroup(closedGroup, publicKey, context, subscribe)
|
allClosedGroupPublicKeys.forEach { closedGroup ->
|
||||||
|
performOperation(context, ClosedGroupOperation.Subscribe, closedGroup, publicKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun operateClosedGroup(closedGroupPublicKey: String, publicKey: String, context: Context?, operation: String) {
|
fun performOperation(context: Context, operation: ClosedGroupOperation, closedGroupPublicKey: String, publicKey: String) {
|
||||||
Log.d("Loki", "Start to notify PN server of closed group.")
|
Log.d("Loki", "Start to notify PN server of closed group.")
|
||||||
if (!TextSecurePreferences.isUsingFCM(context)) { return }
|
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 = "$server/$operation"
|
val url = "$server/${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).build()
|
val request = Request.Builder().url(url).post(body).build()
|
||||||
connection.newCall(request).enqueue(object : Callback {
|
connection.newCall(request).enqueue(object : Callback {
|
||||||
@ -116,48 +126,15 @@ object LokiPushNotificationManager {
|
|||||||
val json = JsonUtil.fromJson(bodyAsString, Map::class.java)
|
val json = JsonUtil.fromJson(bodyAsString, Map::class.java)
|
||||||
val code = json?.get("code") as? Int
|
val code = json?.get("code") as? Int
|
||||||
if (code == null || code == 0) {
|
if (code == null || code == 0) {
|
||||||
Log.d("Loki", "Couldn't subscribe/unsubscribe $closedGroupPublicKey due to error: ${json?.get("message") as? String ?: "null"}.")
|
Log.d("Loki", "Couldn't subscribe/unsubscribe to/from PNs for closed group with ID: $closedGroupPublicKey due to error: ${json?.get("message") as? String ?: "null"}.")
|
||||||
} else {
|
|
||||||
Log.d("Loki", "Subscribe/unsubscribe success.")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onFailure(call: Call, exception: IOException) {
|
override fun onFailure(call: Call, exception: IOException) {
|
||||||
Log.d("Loki", "Couldn't subscribe/unsubscribe $closedGroupPublicKey.")
|
Log.d("Loki", "Couldn't subscribe/unsubscribe to/from PNs for closed group with ID: $closedGroupPublicKey due to error: $exception.")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ClosedGroupSubscribeJob private constructor(parameters: Parameters, private val closedGroupPublicKey: String) : BaseJob(parameters) {
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
const val KEY = "ClosedGroupSubscribeJob"
|
|
||||||
}
|
|
||||||
|
|
||||||
constructor(closedGroupPublicKey: String) : this(Parameters.Builder()
|
|
||||||
.addConstraint(NetworkConstraint.KEY)
|
|
||||||
.setQueue(KEY)
|
|
||||||
.setLifespan(TimeUnit.DAYS.toMillis(1))
|
|
||||||
.setMaxAttempts(1)
|
|
||||||
.build(),
|
|
||||||
closedGroupPublicKey)
|
|
||||||
|
|
||||||
override fun serialize(): Data {
|
|
||||||
val builder = Data.Builder()
|
|
||||||
builder.putString("closedGroupPublicKey", closedGroupPublicKey)
|
|
||||||
return builder.build()
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun getFactoryKey(): String { return KEY }
|
|
||||||
|
|
||||||
override fun onCanceled() { }
|
|
||||||
|
|
||||||
public override fun onRun() {
|
|
||||||
LokiPushNotificationManager.operateClosedGroup(closedGroupPublicKey, TextSecurePreferences.getLocalNumber(context), context, LokiPushNotificationManager.subscribe)
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onShouldRetry(e: Exception): Boolean { return false }
|
|
||||||
}
|
|
||||||
|
@ -8,8 +8,8 @@ import nl.komponents.kovenant.deferred
|
|||||||
import org.thoughtcrime.securesms.ApplicationContext
|
import org.thoughtcrime.securesms.ApplicationContext
|
||||||
import org.thoughtcrime.securesms.database.Address
|
import org.thoughtcrime.securesms.database.Address
|
||||||
import org.thoughtcrime.securesms.database.DatabaseFactory
|
import org.thoughtcrime.securesms.database.DatabaseFactory
|
||||||
import org.thoughtcrime.securesms.loki.api.ClosedGroupSubscribeJob
|
|
||||||
import org.thoughtcrime.securesms.loki.api.LokiPushNotificationManager
|
import org.thoughtcrime.securesms.loki.api.LokiPushNotificationManager
|
||||||
|
import org.thoughtcrime.securesms.loki.api.LokiPushNotificationManager.ClosedGroupOperation
|
||||||
import org.thoughtcrime.securesms.loki.utilities.recipient
|
import org.thoughtcrime.securesms.loki.utilities.recipient
|
||||||
import org.thoughtcrime.securesms.mms.OutgoingGroupMediaMessage
|
import org.thoughtcrime.securesms.mms.OutgoingGroupMediaMessage
|
||||||
import org.thoughtcrime.securesms.recipients.Recipient
|
import org.thoughtcrime.securesms.recipients.Recipient
|
||||||
@ -35,7 +35,7 @@ import java.io.IOException
|
|||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
object ClosedGroupsProtocol {
|
object ClosedGroupsProtocol {
|
||||||
val isSharedSenderKeysEnabled = true
|
val isSharedSenderKeysEnabled = false
|
||||||
val groupSizeLimit = 10
|
val groupSizeLimit = 10
|
||||||
|
|
||||||
public fun createClosedGroup(context: Context, name: String, members: Collection<String>): Promise<String, Exception> {
|
public fun createClosedGroup(context: Context, name: String, members: Collection<String>): Promise<String, Exception> {
|
||||||
@ -68,17 +68,15 @@ object ClosedGroupsProtocol {
|
|||||||
if (member == userPublicKey) { continue }
|
if (member == userPublicKey) { continue }
|
||||||
val job = ClosedGroupUpdateMessageSendJob(member, closedGroupUpdateKind)
|
val job = ClosedGroupUpdateMessageSendJob(member, closedGroupUpdateKind)
|
||||||
job.setContext(context)
|
job.setContext(context)
|
||||||
job.onRun() // Run the job immediately
|
job.onRun() // Run the job immediately to make all of this sync
|
||||||
}
|
}
|
||||||
// Add the group to the user's set of public keys to poll for
|
// Add the group to the user's set of public keys to poll for
|
||||||
DatabaseFactory.getSSKDatabase(context).setClosedGroupPrivateKey(groupPublicKey, groupKeyPair.hexEncodedPrivateKey)
|
DatabaseFactory.getSSKDatabase(context).setClosedGroupPrivateKey(groupPublicKey, groupKeyPair.hexEncodedPrivateKey)
|
||||||
// Notify PN server
|
|
||||||
val jobForPN = ClosedGroupSubscribeJob(groupPublicKey)
|
|
||||||
jobForPN.setContext(context)
|
|
||||||
jobForPN.onRun()
|
|
||||||
// Notify the user
|
// Notify the user
|
||||||
val threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(Recipient.from(context, Address.fromSerialized(groupID), false))
|
val threadID = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(Recipient.from(context, Address.fromSerialized(groupID), false))
|
||||||
insertOutgoingInfoMessage(context, groupID, GroupContext.Type.UPDATE, name, members, admins, threadID)
|
insertOutgoingInfoMessage(context, groupID, GroupContext.Type.UPDATE, name, members, admins, threadID)
|
||||||
|
// Notify the PN server
|
||||||
|
LokiPushNotificationManager.performOperation(context, ClosedGroupOperation.Subscribe, groupPublicKey, userPublicKey)
|
||||||
// Fulfill the promise
|
// Fulfill the promise
|
||||||
deferred.resolve(groupID)
|
deferred.resolve(groupID)
|
||||||
}.start()
|
}.start()
|
||||||
@ -143,8 +141,8 @@ object ClosedGroupsProtocol {
|
|||||||
sskDatabase.removeClosedGroupPrivateKey(groupPublicKey)
|
sskDatabase.removeClosedGroupPrivateKey(groupPublicKey)
|
||||||
groupDB.setActive(groupID, false)
|
groupDB.setActive(groupID, false)
|
||||||
groupDB.remove(groupID, Address.fromSerialized(userPublicKey))
|
groupDB.remove(groupID, Address.fromSerialized(userPublicKey))
|
||||||
// Notify PN server
|
// Notify the PN server
|
||||||
LokiPushNotificationManager.operateClosedGroup(groupPublicKey, userPublicKey, context, LokiPushNotificationManager.unsubscribe)
|
LokiPushNotificationManager.performOperation(context, ClosedGroupOperation.Unsubscribe, groupPublicKey, userPublicKey)
|
||||||
} else {
|
} else {
|
||||||
// Establish sessions if needed
|
// Establish sessions if needed
|
||||||
establishSessionsWithMembersIfNeeded(context, members)
|
establishSessionsWithMembersIfNeeded(context, members)
|
||||||
@ -267,8 +265,8 @@ object ClosedGroupsProtocol {
|
|||||||
insertIncomingInfoMessage(context, senderPublicKey, groupID, GroupContext.Type.UPDATE, SignalServiceGroup.Type.UPDATE, name, members, admins)
|
insertIncomingInfoMessage(context, senderPublicKey, groupID, GroupContext.Type.UPDATE, SignalServiceGroup.Type.UPDATE, name, members, admins)
|
||||||
// Establish sessions if needed
|
// Establish sessions if needed
|
||||||
establishSessionsWithMembersIfNeeded(context, members)
|
establishSessionsWithMembersIfNeeded(context, members)
|
||||||
// Notify PN server
|
// Notify the PN server
|
||||||
LokiPushNotificationManager.operateClosedGroup(groupPublicKey, userPublicKey, context, LokiPushNotificationManager.subscribe)
|
LokiPushNotificationManager.performOperation(context, ClosedGroupOperation.Subscribe, groupPublicKey, userPublicKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
public fun handleClosedGroupUpdate(context: Context, closedGroupUpdate: SignalServiceProtos.ClosedGroupUpdate, senderPublicKey: String) {
|
public fun handleClosedGroupUpdate(context: Context, closedGroupUpdate: SignalServiceProtos.ClosedGroupUpdate, senderPublicKey: String) {
|
||||||
@ -314,6 +312,8 @@ object ClosedGroupsProtocol {
|
|||||||
sskDatabase.removeClosedGroupPrivateKey(groupPublicKey)
|
sskDatabase.removeClosedGroupPrivateKey(groupPublicKey)
|
||||||
groupDB.setActive(groupID, false)
|
groupDB.setActive(groupID, false)
|
||||||
groupDB.remove(groupID, Address.fromSerialized(userPublicKey))
|
groupDB.remove(groupID, Address.fromSerialized(userPublicKey))
|
||||||
|
// Notify the PN server
|
||||||
|
LokiPushNotificationManager.performOperation(context, ClosedGroupOperation.Unsubscribe, groupPublicKey, userPublicKey)
|
||||||
} else {
|
} else {
|
||||||
establishSessionsWithMembersIfNeeded(context, members)
|
establishSessionsWithMembersIfNeeded(context, members)
|
||||||
val userRatchet = SharedSenderKeysImplementation.shared.generateRatchet(groupPublicKey, userPublicKey)
|
val userRatchet = SharedSenderKeysImplementation.shared.generateRatchet(groupPublicKey, userPublicKey)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user