Commit fe14cdb0 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Save tokens with different keys for each connected server

parent 5a678af5
......@@ -382,7 +382,7 @@ class LoginPresenter @Inject constructor(
}
private suspend fun registerPushToken() {
localRepository.get(LocalRepository.KEY_PUSH_TOKEN)?.let {
localRepository.getPushToken(currentServer)?.let {
client.registerPushToken(it, getAccountsInteractor.get(), factory)
}
// TODO: When the push token is null, at some point we should receive it with
......
......@@ -65,7 +65,7 @@ class RegisterUsernamePresenter @Inject constructor(
}
private suspend fun registerPushToken() {
localRepository.get(LocalRepository.KEY_PUSH_TOKEN)?.let {
localRepository.getPushToken(currentServer)?.let {
client.registerPushToken(it, getAccountsInteractor.get(), factory)
}
// TODO: When the push token is null, at some point we should receive it with
......
......@@ -95,7 +95,7 @@ class SignupPresenter @Inject constructor(private val view: SignupView,
}
private suspend fun registerPushToken() {
localRepository.get(LocalRepository.KEY_PUSH_TOKEN)?.let {
localRepository.getPushToken(currentServer)?.let {
client.registerPushToken(it, getAccountsInteractor.get(), factory)
}
// TODO: When the push token is null, at some point we should receive it with
......
......@@ -79,7 +79,7 @@ class TwoFAPresenter @Inject constructor(private val view: TwoFAView,
fun signup() = navigator.toSignUp()
private suspend fun registerPushToken() {
localRepository.get(LocalRepository.KEY_PUSH_TOKEN)?.let {
localRepository.getPushToken(currentServer)?.let {
client.registerPushToken(it, getAccountsInteractor.get(), factory)
}
// TODO: When the push token is null, at some point we should receive it with
......
......@@ -18,9 +18,11 @@ interface LocalRepository {
fun clearAllFromServer(server: String)
fun getCurrentUser(url: String): User?
fun saveCurrentUser(url: String, user: User)
fun savePushToken(url: String, token: String)
fun getPushToken(url: String): String?
companion object {
const val KEY_PUSH_TOKEN = "KEY_PUSH_TOKEN"
const val PUSH_TOKEN_KEY = "push_token_"
const val MIGRATION_FINISHED_KEY = "MIGRATION_FINISHED_KEY"
const val TOKEN_KEY = "token_"
const val SETTINGS_KEY = "settings_"
......
......@@ -2,6 +2,7 @@ package chat.rocket.android.infrastructure
import android.content.SharedPreferences
import androidx.core.content.edit
import chat.rocket.android.infrastructure.LocalRepository.Companion.PUSH_TOKEN_KEY
import chat.rocket.common.model.User
import com.squareup.moshi.Moshi
......@@ -44,8 +45,15 @@ class SharedPreferencesLocalRepository(
override fun clear(key: String) = preferences.edit { remove(key) }
override fun savePushToken(url: String, token: String) {
save(PUSH_TOKEN_KEY + url, token)
}
override fun getPushToken(url: String): String? = preferences.getString(PUSH_TOKEN_KEY + url, null)
override fun clearAllFromServer(server: String) {
clear(LocalRepository.KEY_PUSH_TOKEN)
clear(LocalRepository.PUSH_TOKEN_KEY) //TODO: keep this for now to clear up for some version rolls.
clear(LocalRepository.PUSH_TOKEN_KEY + server)
clear(LocalRepository.TOKEN_KEY + server)
clear(LocalRepository.SETTINGS_KEY + server)
clear(LocalRepository.CURRENT_USERNAME_KEY)
......
......@@ -152,7 +152,7 @@ class MainPresenter @Inject constructor(
suspend fun refreshToken(token: String?) {
token?.let {
localRepository.save(LocalRepository.KEY_PUSH_TOKEN, token)
localRepository.savePushToken(currentServer, it)
client.registerPushToken(token, getAccountsInteractor.get(), factory)
}
}
......@@ -172,16 +172,15 @@ class MainPresenter @Inject constructor(
}
private suspend fun clearTokens() {
serverInteractor.clear()
val pushToken = localRepository.get(LocalRepository.KEY_PUSH_TOKEN)
if (pushToken != null) {
localRepository.get(LocalRepository.PUSH_TOKEN_KEY + currentServer)?.let {
try {
retryIO("unregisterPushToken") { client.unregisterPushToken(pushToken) }
view.invalidateToken(pushToken)
retryIO("unregisterPushToken") { client.unregisterPushToken(it) }
view.invalidateToken(it)
} catch (ex: Exception) {
Timber.d(ex, "Error unregistering push token")
}
}
serverInteractor.clear()
localRepository.clearAllFromServer(currentServer)
}
......
......@@ -39,8 +39,8 @@ class FirebaseTokenService : FirebaseInstanceIdService() {
val currentServer = getCurrentServerInteractor.get()
val client = currentServer?.let { factory.create(currentServer) }
gcmToken?.let {
localRepository.save(LocalRepository.KEY_PUSH_TOKEN, gcmToken)
if (gcmToken != null && currentServer != null) {
localRepository.savePushToken(currentServer, gcmToken)
client?.let {
launch {
try {
......@@ -53,7 +53,7 @@ class FirebaseTokenService : FirebaseInstanceIdService() {
}
}
} catch (ex: Exception) {
Timber.d(ex, "Error refreshing Firebase TOKEN")
Timber.e(ex, "Error refreshing Firebase TOKEN")
}
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment