Skip to content

Commit

Permalink
Clear out user related data on logout (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
markocic authored Feb 21, 2025
1 parent d74acd7 commit 6258276
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ interface ArticleFeedCrossRefDao {

@Query("DELETE FROM ArticleFeedCrossRef WHERE spec = :spec AND ownerId = :ownerId")
fun deleteConnectionsBySpec(ownerId: String, spec: String)

@Query("DELETE FROM ArticleFeedCrossRef WHERE ownerId = :ownerId")
fun deleteConnections(ownerId: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class AuthRepository @Inject constructor(

userRepository.removeUserAccountById(pubkey = pubkey)
credentialsStore.removeCredentialByNpub(npub = pubkey.hexToNpubHrp())
userRepository.clearAllUserRelatedData(userId = pubkey)
}

private suspend fun setNextActiveAccount() {
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/kotlin/net/primal/android/feeds/db/FeedDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@ interface FeedDao {

@Query("DELETE FROM Feed WHERE ownerId = :ownerId AND spec = :spec")
fun deleteAllByOwnerIdAndSpec(ownerId: String, spec: String)

@Query("DELETE FROM Feed WHERE ownerId = :ownerId")
fun deleteAllByOwnerId(ownerId: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ interface DirectMessageDao {
""",
)
fun newestMessagesPagedByOwnerId(ownerId: String, participantId: String): PagingSource<Int, DirectMessage>

@Query("DELETE FROM DirectMessageData WHERE ownerId = :ownerId")
fun deleteAllByOwnerId(ownerId: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@ interface MessageConversationDao {

@Query("UPDATE MessageConversationData SET unreadMessagesCount = 0 WHERE ownerId = :ownerId")
fun markAllConversationAsRead(ownerId: String)

@Query("DELETE FROM MessageConversationData WHERE ownerId = :ownerId")
fun deleteAllByOwnerId(ownerId: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ interface FeedPostDataCrossRefDao {

@Query("DELETE FROM FeedPostDataCrossRef WHERE feedSpec = :feedSpec AND ownerId = :ownerId")
fun deleteConnectionsByDirective(ownerId: String, feedSpec: String)

@Query("DELETE FROM FeedPostDataCrossRef WHERE ownerId = :ownerId")
fun deleteConnections(ownerId: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,7 @@ interface FeedPostRemoteKeyDao {

@Query("DELETE FROM FeedPostRemoteKey WHERE directive = :directive AND ownerId = :ownerId")
fun deleteByDirective(ownerId: String, directive: String)

@Query("DELETE FROM FeedPostRemoteKey WHERE ownerId = :ownerId")
fun deleteAllByOwnerId(ownerId: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ interface NotificationDao {
fun allUnseenNotifications(ownerId: String): Flow<List<Notification>>

@Transaction
@Query("""
@Query(
"""
SELECT * FROM NotificationData
WHERE seenGloballyAt IS NOT NULL AND ownerId = :ownerId
ORDER BY createdAt DESC
Expand All @@ -38,4 +39,7 @@ interface NotificationDao {

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun upsertAll(data: List<NotificationData>)

@Query("DELETE FROM NotificationData WHERE ownerId = :ownerId")
fun deleteAllByOwnerId(ownerId: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ interface ProfileInteractionDao {
""",
)
fun observeRecentProfilesByOwnerId(ownerId: String, limit: Int = 10): Flow<List<Profile>>

@Query("DELETE FROM ProfileInteraction WHERE ownerId = :ownerId")
fun deleteAllByOwnerId(ownerId: String)
}
3 changes: 3 additions & 0 deletions app/src/main/kotlin/net/primal/android/user/db/RelayDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ interface RelayDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun upsertAll(relays: List<Relay>)

@Query("DELETE FROM Relay WHERE userId = :userId")
fun deleteAll(userId: String)

@Query("DELETE FROM Relay WHERE userId = :userId AND kind = :kind")
fun deleteAll(userId: String, kind: RelayKind)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.primal.android.user.repository

import androidx.room.withTransaction
import java.time.Instant
import javax.inject.Inject
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -64,6 +65,24 @@ class UserRepository @Inject constructor(
}
}

suspend fun clearAllUserRelatedData(userId: String) =
withContext(dispatchers.io()) {
database.withTransaction {
database.messages().deleteAllByOwnerId(ownerId = userId)
database.messageConversations().deleteAllByOwnerId(ownerId = userId)
database.feeds().deleteAllByOwnerId(ownerId = userId)
database.mutedUsers().deleteAllByOwnerId(ownerId = userId)
database.profileInteractions().deleteAllByOwnerId(ownerId = userId)
database.walletTransactions().deleteAllTransactionsByUserId(userId = userId)
database.notifications().deleteAllByOwnerId(ownerId = userId)
database.articleFeedsConnections().deleteConnections(ownerId = userId)
database.feedsConnections().deleteConnections(ownerId = userId)
database.feedPostsRemoteKeys().deleteAllByOwnerId(ownerId = userId)
database.publicBookmarks().deleteAllBookmarks(userId = userId)
database.relays().deleteAll(userId = userId)
}
}

suspend fun updateFollowList(userId: String, contactsUserAccount: UserAccount) {
accountsStore.getAndUpdateAccount(userId = userId) {
copyFollowListIfNotNull(followList = contactsUserAccount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ interface WalletTransactionDao {
fun findTransactionById(txId: String): WalletTransaction?

@Query("DELETE FROM WalletTransactionData WHERE userId IS :userId")
fun deleteAllTransactionsUserId(userId: String)
fun deleteAllTransactionsByUserId(userId: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class WalletRepository @Inject constructor(
}

fun deleteAllTransactions(userId: String) =
database.walletTransactions().deleteAllTransactionsUserId(userId = userId)
database.walletTransactions().deleteAllTransactionsByUserId(userId = userId)

suspend fun fetchMiningFees(
userId: String,
Expand Down

0 comments on commit 6258276

Please sign in to comment.