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( ...@@ -382,7 +382,7 @@ class LoginPresenter @Inject constructor(
} }
private suspend fun registerPushToken() { private suspend fun registerPushToken() {
localRepository.get(LocalRepository.KEY_PUSH_TOKEN)?.let { localRepository.getPushToken(currentServer)?.let {
client.registerPushToken(it, getAccountsInteractor.get(), factory) client.registerPushToken(it, getAccountsInteractor.get(), factory)
} }
// TODO: When the push token is null, at some point we should receive it with // TODO: When the push token is null, at some point we should receive it with
......
...@@ -65,7 +65,7 @@ class RegisterUsernamePresenter @Inject constructor( ...@@ -65,7 +65,7 @@ class RegisterUsernamePresenter @Inject constructor(
} }
private suspend fun registerPushToken() { private suspend fun registerPushToken() {
localRepository.get(LocalRepository.KEY_PUSH_TOKEN)?.let { localRepository.getPushToken(currentServer)?.let {
client.registerPushToken(it, getAccountsInteractor.get(), factory) client.registerPushToken(it, getAccountsInteractor.get(), factory)
} }
// TODO: When the push token is null, at some point we should receive it with // 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, ...@@ -95,7 +95,7 @@ class SignupPresenter @Inject constructor(private val view: SignupView,
} }
private suspend fun registerPushToken() { private suspend fun registerPushToken() {
localRepository.get(LocalRepository.KEY_PUSH_TOKEN)?.let { localRepository.getPushToken(currentServer)?.let {
client.registerPushToken(it, getAccountsInteractor.get(), factory) client.registerPushToken(it, getAccountsInteractor.get(), factory)
} }
// TODO: When the push token is null, at some point we should receive it with // 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, ...@@ -79,7 +79,7 @@ class TwoFAPresenter @Inject constructor(private val view: TwoFAView,
fun signup() = navigator.toSignUp() fun signup() = navigator.toSignUp()
private suspend fun registerPushToken() { private suspend fun registerPushToken() {
localRepository.get(LocalRepository.KEY_PUSH_TOKEN)?.let { localRepository.getPushToken(currentServer)?.let {
client.registerPushToken(it, getAccountsInteractor.get(), factory) client.registerPushToken(it, getAccountsInteractor.get(), factory)
} }
// TODO: When the push token is null, at some point we should receive it with // TODO: When the push token is null, at some point we should receive it with
......
...@@ -18,9 +18,11 @@ interface LocalRepository { ...@@ -18,9 +18,11 @@ interface LocalRepository {
fun clearAllFromServer(server: String) fun clearAllFromServer(server: String)
fun getCurrentUser(url: String): User? fun getCurrentUser(url: String): User?
fun saveCurrentUser(url: String, user: User) fun saveCurrentUser(url: String, user: User)
fun savePushToken(url: String, token: String)
fun getPushToken(url: String): String?
companion object { 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 MIGRATION_FINISHED_KEY = "MIGRATION_FINISHED_KEY"
const val TOKEN_KEY = "token_" const val TOKEN_KEY = "token_"
const val SETTINGS_KEY = "settings_" const val SETTINGS_KEY = "settings_"
......
...@@ -2,6 +2,7 @@ package chat.rocket.android.infrastructure ...@@ -2,6 +2,7 @@ package chat.rocket.android.infrastructure
import android.content.SharedPreferences import android.content.SharedPreferences
import androidx.core.content.edit import androidx.core.content.edit
import chat.rocket.android.infrastructure.LocalRepository.Companion.PUSH_TOKEN_KEY
import chat.rocket.common.model.User import chat.rocket.common.model.User
import com.squareup.moshi.Moshi import com.squareup.moshi.Moshi
...@@ -44,8 +45,15 @@ class SharedPreferencesLocalRepository( ...@@ -44,8 +45,15 @@ class SharedPreferencesLocalRepository(
override fun clear(key: String) = preferences.edit { remove(key) } 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) { 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.TOKEN_KEY + server)
clear(LocalRepository.SETTINGS_KEY + server) clear(LocalRepository.SETTINGS_KEY + server)
clear(LocalRepository.CURRENT_USERNAME_KEY) clear(LocalRepository.CURRENT_USERNAME_KEY)
......
...@@ -152,7 +152,7 @@ class MainPresenter @Inject constructor( ...@@ -152,7 +152,7 @@ class MainPresenter @Inject constructor(
suspend fun refreshToken(token: String?) { suspend fun refreshToken(token: String?) {
token?.let { token?.let {
localRepository.save(LocalRepository.KEY_PUSH_TOKEN, token) localRepository.savePushToken(currentServer, it)
client.registerPushToken(token, getAccountsInteractor.get(), factory) client.registerPushToken(token, getAccountsInteractor.get(), factory)
} }
} }
...@@ -172,16 +172,15 @@ class MainPresenter @Inject constructor( ...@@ -172,16 +172,15 @@ class MainPresenter @Inject constructor(
} }
private suspend fun clearTokens() { private suspend fun clearTokens() {
serverInteractor.clear() localRepository.get(LocalRepository.PUSH_TOKEN_KEY + currentServer)?.let {
val pushToken = localRepository.get(LocalRepository.KEY_PUSH_TOKEN)
if (pushToken != null) {
try { try {
retryIO("unregisterPushToken") { client.unregisterPushToken(pushToken) } retryIO("unregisterPushToken") { client.unregisterPushToken(it) }
view.invalidateToken(pushToken) view.invalidateToken(it)
} catch (ex: Exception) { } catch (ex: Exception) {
Timber.d(ex, "Error unregistering push token") Timber.d(ex, "Error unregistering push token")
} }
} }
serverInteractor.clear()
localRepository.clearAllFromServer(currentServer) localRepository.clearAllFromServer(currentServer)
} }
......
...@@ -39,8 +39,8 @@ class FirebaseTokenService : FirebaseInstanceIdService() { ...@@ -39,8 +39,8 @@ class FirebaseTokenService : FirebaseInstanceIdService() {
val currentServer = getCurrentServerInteractor.get() val currentServer = getCurrentServerInteractor.get()
val client = currentServer?.let { factory.create(currentServer) } val client = currentServer?.let { factory.create(currentServer) }
gcmToken?.let { if (gcmToken != null && currentServer != null) {
localRepository.save(LocalRepository.KEY_PUSH_TOKEN, gcmToken) localRepository.savePushToken(currentServer, gcmToken)
client?.let { client?.let {
launch { launch {
try { try {
...@@ -53,7 +53,7 @@ class FirebaseTokenService : FirebaseInstanceIdService() { ...@@ -53,7 +53,7 @@ class FirebaseTokenService : FirebaseInstanceIdService() {
} }
} }
} catch (ex: Exception) { } 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