From 767e815f61da23cd3a072bade7a9f7b302cfcbbd Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Thu, 25 Feb 2021 19:21:06 -0800 Subject: [PATCH] Use keychain instead of DB in notification service --- DB/Sources/DB/Identity/IdentityDatabase.swift | 10 -------- .../NotificationService.swift | 15 ++++-------- Secrets/Sources/Secrets/Secrets.swift | 18 ++++++++++++++ .../Services/AllIdentitiesService.swift | 5 ---- .../Services/IdentityService.swift | 4 ++++ .../PushNotificationParsingService.swift | 24 +++++++++++-------- 6 files changed, 40 insertions(+), 36 deletions(-) diff --git a/DB/Sources/DB/Identity/IdentityDatabase.swift b/DB/Sources/DB/Identity/IdentityDatabase.swift index 33c098f..77af7bc 100644 --- a/DB/Sources/DB/Identity/IdentityDatabase.swift +++ b/DB/Sources/DB/Identity/IdentityDatabase.swift @@ -228,16 +228,6 @@ public extension IdentityDatabase { return Identity(info: info) } - - // Only for use in notification extension - func identity(id: Identity.Id) throws -> Identity? { - guard let info = try databaseWriter.read( - IdentityInfo.request(IdentityRecord.filter(IdentityRecord.Columns.id == id)) - .fetchOne) - else { return nil } - - return Identity(info: info) - } } private extension IdentityDatabase { diff --git a/Notification Service Extension/NotificationService.swift b/Notification Service Extension/NotificationService.swift index 451c48d..ce6ac40 100644 --- a/Notification Service Extension/NotificationService.swift +++ b/Notification Service Extension/NotificationService.swift @@ -49,23 +49,16 @@ final class NotificationService: UNNotificationServiceExtension { bestAttemptContent.sound = .default } - var identity: Identity? - - if appPreferences.notificationAccountName { - identity = try? AllIdentitiesService(environment: Self.environment).identity(id: identityId) - - if let handle = identity?.handle { - bestAttemptContent.subtitle = handle - } + if appPreferences.notificationAccountName, + case let .success(handle) = parsingService.handle(identityId: identityId) { + bestAttemptContent.subtitle = handle } Self.attachment(imageURL: pushNotification.icon) .map { [$0] } .replaceError(with: []) .handleEvents(receiveOutput: { bestAttemptContent.attachments = $0 }) - .zip(parsingService.title(pushNotification: pushNotification, - identityId: identityId, - accountId: identity?.account?.id) + .zip(parsingService.title(pushNotification: pushNotification, identityId: identityId) .replaceError(with: pushNotification.title) .handleEvents(receiveOutput: { bestAttemptContent.title = $0 })) .sink { _ in contentHandler(bestAttemptContent) } diff --git a/Secrets/Sources/Secrets/Secrets.swift b/Secrets/Sources/Secrets/Secrets.swift index accc789..74eec7e 100644 --- a/Secrets/Sources/Secrets/Secrets.swift +++ b/Secrets/Sources/Secrets/Secrets.swift @@ -35,6 +35,8 @@ public extension Secrets { case databaseKey case imageCacheKey case identityDatabaseName + case accountId + case username } } @@ -165,6 +167,22 @@ public extension Secrets { try set(accessToken, forItem: .accessToken) } + func getAccountId() throws -> String { + try item(.accountId) + } + + func setAccountId(_ accountId: String) throws { + try set(accountId, forItem: .accountId) + } + + func getUsername() throws -> String { + try item(.username) + } + + func setUsername(_ username: String) throws { + try set(username, forItem: .username) + } + func generatePushKeyAndReturnPublicKey() throws -> Data { try keychain.generateKeyAndReturnPublicKey( applicationTag: scopedKey(item: .pushKey), diff --git a/ServiceLayer/Sources/ServiceLayer/Services/AllIdentitiesService.swift b/ServiceLayer/Sources/ServiceLayer/Services/AllIdentitiesService.swift index 2c5e366..fa7224a 100644 --- a/ServiceLayer/Sources/ServiceLayer/Services/AllIdentitiesService.swift +++ b/ServiceLayer/Sources/ServiceLayer/Services/AllIdentitiesService.swift @@ -136,11 +136,6 @@ public extension AllIdentitiesService { .ignoreOutput() .eraseToAnyPublisher() } - - // Only for use in notification extension - func identity(id: Identity.Id) throws -> Identity? { - try database.identity(id: id) - } } private extension AllIdentitiesService.IdentityCreation { diff --git a/ServiceLayer/Sources/ServiceLayer/Services/IdentityService.swift b/ServiceLayer/Sources/ServiceLayer/Services/IdentityService.swift index 23168bd..9ab2908 100644 --- a/ServiceLayer/Sources/ServiceLayer/Services/IdentityService.swift +++ b/ServiceLayer/Sources/ServiceLayer/Services/IdentityService.swift @@ -51,6 +51,10 @@ public extension IdentityService { func verifyCredentials() -> AnyPublisher { mastodonAPIClient.request(AccountEndpoint.verifyCredentials) + .handleEvents(receiveOutput: { + try? secrets.setAccountId($0.id) + try? secrets.setUsername($0.username) + }) .flatMap { identityDatabase.updateAccount($0, id: id) } .eraseToAnyPublisher() } diff --git a/ServiceLayer/Sources/ServiceLayer/Services/PushNotificationParsingService.swift b/ServiceLayer/Sources/ServiceLayer/Services/PushNotificationParsingService.swift index cf72af5..fd101cd 100644 --- a/ServiceLayer/Sources/ServiceLayer/Services/PushNotificationParsingService.swift +++ b/ServiceLayer/Sources/ServiceLayer/Services/PushNotificationParsingService.swift @@ -51,9 +51,13 @@ public extension PushNotificationParsingService { identityId) } - func title(pushNotification: PushNotification, - identityId: Identity.Id, - accountId: Account.Id?) -> AnyPublisher { + func handle(identityId: Identity.Id) -> Result { + let secrets = Secrets(identityId: identityId, keychain: environment.keychain) + + return Result { try secrets.getUsername().appending("@").appending(secrets.getInstanceURL().host ?? "") } + } + + func title(pushNotification: PushNotification, identityId: Identity.Id) -> AnyPublisher { switch pushNotification.notificationType { case .poll, .status: let secrets = Secrets(identityId: identityId, keychain: environment.keychain) @@ -79,15 +83,15 @@ public extension PushNotificationParsingService { NSLocalizedString("notification.status-%@", comment: ""), $0.account.displayName) case .poll: - switch accountId ?? (try? AllIdentitiesService(environment: environment) - .identity(id: identityId)?.account?.id) { - case .some($0.account.id): - return NSLocalizedString("notification.poll.own", comment: "") - case .some: - return NSLocalizedString("notification.poll", comment: "") - default: + guard let accountId = try? secrets.getAccountId() else { return NSLocalizedString("notification.poll.unknown", comment: "") } + + if $0.account.id == accountId { + return NSLocalizedString("notification.poll.own", comment: "") + } else { + return NSLocalizedString("notification.poll", comment: "") + } default: return pushNotification.title }