Skip to content

Commit 68b2ce9

Browse files
committed
chore(fc): delete push token's on logout
Signed-off-by: Brandon McAnsh <[email protected]>
1 parent 09d2105 commit 68b2ce9

File tree

6 files changed

+46
-12
lines changed

6 files changed

+46
-12
lines changed

flipchatApp/src/main/kotlin/xyz/flipchat/app/auth/AuthManager.kt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import com.ionspin.kotlin.crypto.LibsodiumInitializer
2424
import dagger.hilt.android.qualifiers.ApplicationContext
2525
import kotlinx.coroutines.CoroutineScope
2626
import kotlinx.coroutines.Dispatchers
27+
import kotlinx.coroutines.coroutineScope
2728
import kotlinx.coroutines.launch
2829
import timber.log.Timber
2930
import xyz.flipchat.FlipchatServices
@@ -178,28 +179,32 @@ class AuthManager @Inject constructor(
178179
}
179180
}
180181

181-
fun deleteAndLogout(context: Context, onComplete: () -> Unit = {}) {
182+
suspend fun deleteAndLogout(context: Context, onComplete: () -> Unit = {}) {
182183
//todo: add account deletion
183184
logout(context, onComplete)
184185
}
185186

186-
fun logout(context: Context, onComplete: () -> Unit = {}) {
187-
launch {
187+
suspend fun logout(context: Context, onComplete: () -> Unit = {}) {
188+
coroutineScope {
188189
val token = AccountUtils.getToken(context)
189190
when (token) {
190191
is TokenResult.Account -> {
191192
AccountUtils.removeAccounts(context)
192193
.doOnSuccess { res: Boolean ->
193194
if (res) {
194-
clearToken()
195-
onComplete()
195+
launch {
196+
clearToken()
197+
onComplete()
198+
}
196199
}
197200
}
198201
.subscribe()
199202
}
203+
200204
is TokenResult.Code -> {
201205
onComplete()
202206
}
207+
203208
null -> Unit
204209
}
205210
}
@@ -221,8 +226,12 @@ class AuthManager @Inject constructor(
221226
// )
222227
}
223228

224-
private fun clearToken() {
229+
private suspend fun clearToken() {
230+
val token = FirebaseMessaging.getInstance().token()
225231
FirebaseMessaging.getInstance().deleteToken()
232+
if (token != null) {
233+
pushController.deleteToken(token)
234+
}
226235
Database.close()
227236
userManager.clear()
228237
Database.delete(context)

flipchatApp/src/main/kotlin/xyz/flipchat/app/features/home/tabs/ChatTab.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import androidx.compose.runtime.getValue
1919
import androidx.compose.runtime.mutableIntStateOf
2020
import androidx.compose.runtime.mutableStateOf
2121
import androidx.compose.runtime.remember
22+
import androidx.compose.runtime.rememberCoroutineScope
2223
import androidx.compose.runtime.saveable.rememberSaveable
2324
import androidx.compose.runtime.setValue
2425
import androidx.compose.ui.Alignment
@@ -50,6 +51,7 @@ import com.getcode.ui.utils.unboundedClickable
5051
import kotlinx.coroutines.flow.filterIsInstance
5152
import kotlinx.coroutines.flow.launchIn
5253
import kotlinx.coroutines.flow.onEach
54+
import kotlinx.coroutines.launch
5355
import xyz.flipchat.app.R
5456
import xyz.flipchat.app.features.chat.list.ChatListViewModel
5557
import xyz.flipchat.app.features.chat.openChatDirectiveBottomModal
@@ -82,15 +84,18 @@ internal object ChatTab : ChildNavTab {
8284
}.launchIn(this)
8385
}
8486

87+
val composeScope = rememberCoroutineScope()
8588
Box {
8689
Column {
8790
AppBarWithTitle(
8891
title = options.title,
8992
startContent = {
9093
HiddenLogoutButton(modifier = Modifier.padding(CodeTheme.dimens.grid.x1)) {
9194
context.getActivity()?.let {
92-
settingsVm.logout(it) {
93-
navigator.replaceAll(ScreenRegistry.get(NavScreenProvider.Login.Home()))
95+
composeScope.launch {
96+
settingsVm.logout(it) {
97+
navigator.replaceAll(ScreenRegistry.get(NavScreenProvider.Login.Home()))
98+
}
9499
}
95100
}
96101
}

flipchatApp/src/main/kotlin/xyz/flipchat/app/features/login/accesskey/SeedInputViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class SeedInputViewModel @Inject constructor(
9696
}
9797
}
9898

99-
fun logout(activity: Activity, onComplete: () -> Unit = {}) =
99+
suspend fun logout(activity: Activity, onComplete: () -> Unit = {}) =
100100
authManager.logout(activity, onComplete)
101101

102102
@SuppressLint("CheckResult")

flipchatApp/src/main/kotlin/xyz/flipchat/app/features/settings/SettingsViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class SettingsViewModel @Inject constructor(
1111
private val authManager: AuthManager,
1212
) : ViewModel() {
1313

14-
fun logout(activity: Activity, onComplete: () -> Unit) {
14+
suspend fun logout(activity: Activity, onComplete: () -> Unit) {
1515
authManager.logout(activity, onComplete = onComplete)
1616
}
1717
}

flipchatApp/src/main/kotlin/xyz/flipchat/app/ui/navigation/MainRoot.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ internal object MainRoot : Screen {
113113
if (entropy == null && userId == null) {
114114
delay(500)
115115
showLoading = true
116-
delay(1500)
116+
delay(5_000)
117117
navigator.replace(ScreenRegistry.get(NavScreenProvider.Login.Home()))
118118
return@onEach
119119
}

services/flipchat/chat/src/main/kotlin/xyz/flipchat/services/internal/network/service/PushService.kt

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,27 @@ internal class PushService @Inject constructor(
5151
owner: KeyPair,
5252
token: String,
5353
): Result<Unit> {
54-
return Result.failure(NotImplementedError())
54+
return try {
55+
networkOracle.managedRequest(api.deleteToken(owner, token))
56+
.map {
57+
when (it.result) {
58+
PushService.DeleteTokenResponse.Result.OK -> Result.success(Unit)
59+
PushService.DeleteTokenResponse.Result.UNRECOGNIZED -> {
60+
val error = DeleteTokenError.Unrecognized()
61+
Timber.e(t = error)
62+
Result.failure(error)
63+
}
64+
else -> {
65+
val error = DeleteTokenError.Other()
66+
Timber.e(t = error)
67+
Result.failure(error)
68+
}
69+
}
70+
}.first()
71+
} catch (e: Exception) {
72+
val error = DeleteTokenError.Other(cause = e)
73+
Result.failure(error)
74+
}
5575
}
5676

5777
internal sealed class AddTokenError : Throwable() {

0 commit comments

Comments
 (0)