feat: purge home timeline when user sign out

This commit is contained in:
CMK 2021-07-26 16:31:57 +08:00
parent c86c29afe7
commit 4db73d4f74
4 changed files with 33 additions and 9 deletions

View File

@ -65,7 +65,7 @@ extension HomeTimelineIndex {
public let domain: String public let domain: String
public let userID: String public let userID: String
public init(domain: String,userID: String) { public init(domain: String, userID: String) {
self.identifier = UUID().uuidString + "@" + domain self.identifier = UUID().uuidString + "@" + domain
self.domain = domain self.domain = domain
self.userID = userID self.userID = userID
@ -80,10 +80,20 @@ extension HomeTimelineIndex: Managed {
} }
extension HomeTimelineIndex { extension HomeTimelineIndex {
public static func predicate(userID: String) -> NSPredicate { static func predicate(domain: String) -> NSPredicate {
return NSPredicate(format: "%K == %@", #keyPath(HomeTimelineIndex.domain), domain)
}
static func predicate(userID: MastodonUser.ID) -> NSPredicate {
return NSPredicate(format: "%K == %@", #keyPath(HomeTimelineIndex.userID), userID) return NSPredicate(format: "%K == %@", #keyPath(HomeTimelineIndex.userID), userID)
} }
public static func predicate(domain: String, userID: MastodonUser.ID) -> NSPredicate {
return NSCompoundPredicate(andPredicateWithSubpredicates: [
predicate(domain: domain),
predicate(userID: userID)
])
}
public static func notDeleted() -> NSPredicate { public static func notDeleted() -> NSPredicate {
return NSPredicate(format: "%K == nil", #keyPath(HomeTimelineIndex.deletedAt)) return NSPredicate(format: "%K == nil", #keyPath(HomeTimelineIndex.deletedAt))

View File

@ -7,12 +7,12 @@
<key>AppShared.xcscheme_^#shared#^_</key> <key>AppShared.xcscheme_^#shared#^_</key>
<dict> <dict>
<key>orderHint</key> <key>orderHint</key>
<integer>23</integer> <integer>24</integer>
</dict> </dict>
<key>CoreDataStack.xcscheme_^#shared#^_</key> <key>CoreDataStack.xcscheme_^#shared#^_</key>
<dict> <dict>
<key>orderHint</key> <key>orderHint</key>
<integer>21</integer> <integer>22</integer>
</dict> </dict>
<key>Mastodon - ASDK.xcscheme_^#shared#^_</key> <key>Mastodon - ASDK.xcscheme_^#shared#^_</key>
<dict> <dict>
@ -37,12 +37,12 @@
<key>NotificationService.xcscheme_^#shared#^_</key> <key>NotificationService.xcscheme_^#shared#^_</key>
<dict> <dict>
<key>orderHint</key> <key>orderHint</key>
<integer>20</integer> <integer>3</integer>
</dict> </dict>
<key>ShareActionExtension.xcscheme_^#shared#^_</key> <key>ShareActionExtension.xcscheme_^#shared#^_</key>
<dict> <dict>
<key>orderHint</key> <key>orderHint</key>
<integer>22</integer> <integer>21</integer>
</dict> </dict>
</dict> </dict>
<key>SuppressBuildableAutocreation</key> <key>SuppressBuildableAutocreation</key>

View File

@ -120,9 +120,10 @@ final class HomeTimelineViewModel: NSObject {
.sink { [weak self] activeMastodonAuthentication in .sink { [weak self] activeMastodonAuthentication in
guard let self = self else { return } guard let self = self else { return }
guard let mastodonAuthentication = activeMastodonAuthentication else { return } guard let mastodonAuthentication = activeMastodonAuthentication else { return }
let activeMastodonUserID = mastodonAuthentication.userID let domain = mastodonAuthentication.domain
let userID = mastodonAuthentication.userID
let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [ let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [
HomeTimelineIndex.predicate(userID: activeMastodonUserID), HomeTimelineIndex.predicate(domain: domain, userID: userID),
HomeTimelineIndex.notDeleted() HomeTimelineIndex.notDeleted()
]) ])
self.timelinePredicate.value = predicate self.timelinePredicate.value = predicate

View File

@ -130,6 +130,19 @@ extension AuthenticationService {
appAuthorization: Mastodon.API.OAuth.Authorization(accessToken: mastodonAuthentication.appAccessToken), appAuthorization: Mastodon.API.OAuth.Authorization(accessToken: mastodonAuthentication.appAccessToken),
userAuthorization: Mastodon.API.OAuth.Authorization(accessToken: mastodonAuthentication.userAccessToken) userAuthorization: Mastodon.API.OAuth.Authorization(accessToken: mastodonAuthentication.userAccessToken)
) )
// remove home timeline indexes
let homeTimelineIndexRequest = HomeTimelineIndex.sortedFetchRequest
homeTimelineIndexRequest.predicate = HomeTimelineIndex.predicate(
domain: mastodonAuthentication.domain,
userID: mastodonAuthentication.userID
)
let homeTimelineIndexes = managedObjectContext.safeFetch(homeTimelineIndexRequest)
for homeTimelineIndex in homeTimelineIndexes {
managedObjectContext.delete(homeTimelineIndex)
}
// remove user authentication
managedObjectContext.delete(mastodonAuthentication) managedObjectContext.delete(mastodonAuthentication)
isSignOut = true isSignOut = true
} }