2020-08-07 21:57:18 +00:00
|
|
|
// Copyright © 2020 Metabolist. All rights reserved.
|
|
|
|
|
2020-09-06 21:37:54 +00:00
|
|
|
import Base16
|
2020-09-05 02:31:43 +00:00
|
|
|
import Combine
|
2020-09-03 03:28:34 +00:00
|
|
|
import DB
|
2020-08-07 21:57:18 +00:00
|
|
|
import Foundation
|
2020-08-30 23:33:11 +00:00
|
|
|
import Mastodon
|
2020-09-04 01:55:46 +00:00
|
|
|
import MastodonAPI
|
2020-09-04 00:54:05 +00:00
|
|
|
import Secrets
|
2020-08-07 21:57:18 +00:00
|
|
|
|
2020-09-07 16:33:36 +00:00
|
|
|
public struct IdentityService {
|
2020-10-05 22:50:05 +00:00
|
|
|
private let id: Identity.Id
|
2020-08-09 11:27:38 +00:00
|
|
|
private let identityDatabase: IdentityDatabase
|
2020-08-18 05:13:37 +00:00
|
|
|
private let contentDatabase: ContentDatabase
|
2020-08-09 05:37:04 +00:00
|
|
|
private let environment: AppEnvironment
|
2020-09-04 01:55:46 +00:00
|
|
|
private let mastodonAPIClient: MastodonAPIClient
|
2020-09-04 00:54:05 +00:00
|
|
|
private let secrets: Secrets
|
2020-08-07 21:57:18 +00:00
|
|
|
|
2020-10-05 22:50:05 +00:00
|
|
|
init(id: Identity.Id, database: IdentityDatabase, environment: AppEnvironment) throws {
|
|
|
|
self.id = id
|
2020-09-07 16:33:36 +00:00
|
|
|
identityDatabase = database
|
2020-08-09 05:37:04 +00:00
|
|
|
self.environment = environment
|
2020-09-04 00:54:05 +00:00
|
|
|
secrets = Secrets(
|
2020-10-05 22:50:05 +00:00
|
|
|
identityId: id,
|
2020-09-04 00:54:05 +00:00
|
|
|
keychain: environment.keychain)
|
2020-09-09 01:02:55 +00:00
|
|
|
mastodonAPIClient = MastodonAPIClient(session: environment.session,
|
|
|
|
instanceURL: try secrets.getInstanceURL())
|
2020-09-04 06:44:04 +00:00
|
|
|
mastodonAPIClient.accessToken = try? secrets.getAccessToken()
|
2020-08-07 21:57:18 +00:00
|
|
|
|
2020-10-27 03:01:12 +00:00
|
|
|
let appPreferences = AppPreferences(environment: environment)
|
|
|
|
|
|
|
|
contentDatabase = try ContentDatabase(
|
|
|
|
id: id,
|
|
|
|
useHomeTimelineLastReadId: appPreferences.homeTimelineBehavior == .rememberPosition,
|
|
|
|
useNotificationsLastReadId: appPreferences.notificationsTabBehavior == .rememberPosition,
|
|
|
|
inMemory: environment.inMemoryContent,
|
2020-11-09 03:07:23 +00:00
|
|
|
appGroup: AppEnvironment.appGroup,
|
2020-10-27 03:01:12 +00:00
|
|
|
keychain: environment.keychain)
|
2020-08-07 21:57:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 10:21:01 +00:00
|
|
|
public extension IdentityService {
|
2020-08-26 09:19:38 +00:00
|
|
|
func updateLastUse() -> AnyPublisher<Never, Error> {
|
2020-10-05 22:50:05 +00:00
|
|
|
identityDatabase.updateLastUsedAt(id: id)
|
2020-08-09 05:37:04 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 09:19:38 +00:00
|
|
|
func verifyCredentials() -> AnyPublisher<Never, Error> {
|
2020-09-04 01:55:46 +00:00
|
|
|
mastodonAPIClient.request(AccountEndpoint.verifyCredentials)
|
2020-10-05 22:50:05 +00:00
|
|
|
.flatMap { identityDatabase.updateAccount($0, id: id) }
|
2020-08-07 21:57:18 +00:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:19:38 +00:00
|
|
|
func refreshServerPreferences() -> AnyPublisher<Never, Error> {
|
2020-09-04 01:55:46 +00:00
|
|
|
mastodonAPIClient.request(PreferencesEndpoint.preferences)
|
2020-10-05 22:50:05 +00:00
|
|
|
.flatMap { identityDatabase.updatePreferences($0, id: id) }
|
2020-08-07 21:57:18 +00:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:19:38 +00:00
|
|
|
func refreshInstance() -> AnyPublisher<Never, Error> {
|
2020-09-04 01:55:46 +00:00
|
|
|
mastodonAPIClient.request(InstanceEndpoint.instance)
|
2020-10-05 22:50:05 +00:00
|
|
|
.flatMap { identityDatabase.updateInstance($0, id: id) }
|
2020-08-07 21:57:18 +00:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2020-09-13 08:03:08 +00:00
|
|
|
func confirmIdentity() -> AnyPublisher<Never, Error> {
|
2020-10-05 22:50:05 +00:00
|
|
|
identityDatabase.confirmIdentity(id: id)
|
2020-09-13 08:03:08 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 20:44:22 +00:00
|
|
|
func identitiesPublisher() -> AnyPublisher<[Identity], Error> {
|
|
|
|
identityDatabase.identitiesPublisher()
|
2020-08-07 21:57:18 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 20:44:22 +00:00
|
|
|
func recentIdentitiesPublisher() -> AnyPublisher<[Identity], Error> {
|
|
|
|
identityDatabase.recentIdentitiesPublisher(excluding: id)
|
2020-08-07 21:57:18 +00:00
|
|
|
}
|
|
|
|
|
2020-08-29 03:50:58 +00:00
|
|
|
func refreshLists() -> AnyPublisher<Never, Error> {
|
2020-09-04 01:55:46 +00:00
|
|
|
mastodonAPIClient.request(ListsEndpoint.lists)
|
2020-08-29 10:26:26 +00:00
|
|
|
.flatMap(contentDatabase.setLists(_:))
|
2020-08-29 03:50:58 +00:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
func createList(title: String) -> AnyPublisher<Never, Error> {
|
2020-09-04 01:55:46 +00:00
|
|
|
mastodonAPIClient.request(ListEndpoint.create(title: title))
|
2020-08-29 03:50:58 +00:00
|
|
|
.flatMap(contentDatabase.createList(_:))
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2020-10-05 22:50:05 +00:00
|
|
|
func deleteList(id: List.Id) -> AnyPublisher<Never, Error> {
|
2020-12-03 05:18:47 +00:00
|
|
|
mastodonAPIClient.request(EmptyEndpoint.deleteList(id: id))
|
2020-08-29 03:50:58 +00:00
|
|
|
.map { _ in id }
|
|
|
|
.flatMap(contentDatabase.deleteList(id:))
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2020-10-27 03:01:12 +00:00
|
|
|
func getMarker(_ markerTimeline: Marker.Timeline) -> AnyPublisher<Marker, Error> {
|
|
|
|
mastodonAPIClient.request(MarkersEndpoint.get([markerTimeline]))
|
|
|
|
.compactMap { $0[markerTimeline.rawValue] }
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLocalLastReadId(_ markerTimeline: Marker.Timeline) -> String? {
|
|
|
|
contentDatabase.lastReadId(markerTimeline)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setLastReadId(_ id: String, forMarker markerTimeline: Marker.Timeline) -> AnyPublisher<Never, Error> {
|
|
|
|
switch AppPreferences(environment: environment).positionBehavior(markerTimeline: markerTimeline) {
|
|
|
|
case .rememberPosition:
|
|
|
|
return contentDatabase.setLastReadId(id, markerTimeline: markerTimeline)
|
|
|
|
case .syncPosition:
|
|
|
|
return mastodonAPIClient.request(MarkersEndpoint.post([markerTimeline: id]))
|
|
|
|
.ignoreOutput()
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
case .newest:
|
|
|
|
return Empty().eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-06 20:44:22 +00:00
|
|
|
func identityPublisher(immediate: Bool) -> AnyPublisher<Identity, Error> {
|
|
|
|
identityDatabase.identityPublisher(id: id, immediate: immediate)
|
2020-09-08 02:12:38 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 20:44:22 +00:00
|
|
|
func listsPublisher() -> AnyPublisher<[Timeline], Error> {
|
|
|
|
contentDatabase.listsPublisher()
|
2020-08-29 10:26:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func refreshFilters() -> AnyPublisher<Never, Error> {
|
2020-09-04 01:55:46 +00:00
|
|
|
mastodonAPIClient.request(FiltersEndpoint.filters)
|
2020-08-29 10:26:26 +00:00
|
|
|
.flatMap(contentDatabase.setFilters(_:))
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
func createFilter(_ filter: Filter) -> AnyPublisher<Never, Error> {
|
2020-09-04 01:55:46 +00:00
|
|
|
mastodonAPIClient.request(FilterEndpoint.create(phrase: filter.phrase,
|
2020-08-29 10:26:26 +00:00
|
|
|
context: filter.context,
|
|
|
|
irreversible: filter.irreversible,
|
|
|
|
wholeWord: filter.wholeWord,
|
|
|
|
expiresIn: filter.expiresAt))
|
|
|
|
.flatMap(contentDatabase.createFilter(_:))
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateFilter(_ filter: Filter) -> AnyPublisher<Never, Error> {
|
2020-09-04 01:55:46 +00:00
|
|
|
mastodonAPIClient.request(FilterEndpoint.update(id: filter.id,
|
2020-08-29 10:26:26 +00:00
|
|
|
phrase: filter.phrase,
|
|
|
|
context: filter.context,
|
|
|
|
irreversible: filter.irreversible,
|
|
|
|
wholeWord: filter.wholeWord,
|
|
|
|
expiresIn: filter.expiresAt))
|
|
|
|
.flatMap(contentDatabase.createFilter(_:))
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2020-10-05 22:50:05 +00:00
|
|
|
func deleteFilter(id: Filter.Id) -> AnyPublisher<Never, Error> {
|
2020-12-03 05:18:47 +00:00
|
|
|
mastodonAPIClient.request(EmptyEndpoint.deleteFilter(id: id))
|
2020-09-07 16:33:36 +00:00
|
|
|
.flatMap { _ in contentDatabase.deleteFilter(id: id) }
|
2020-08-29 10:26:26 +00:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2020-10-06 20:44:22 +00:00
|
|
|
func activeFiltersPublisher() -> AnyPublisher<[Filter], Error> {
|
2020-10-03 20:50:46 +00:00
|
|
|
contentDatabase.activeFiltersPublisher
|
2020-08-30 00:32:34 +00:00
|
|
|
}
|
|
|
|
|
2020-10-06 20:44:22 +00:00
|
|
|
func expiredFiltersPublisher() -> AnyPublisher<[Filter], Error> {
|
|
|
|
contentDatabase.expiredFiltersPublisher()
|
2020-08-29 10:26:26 +00:00
|
|
|
}
|
|
|
|
|
2020-08-26 09:19:38 +00:00
|
|
|
func updatePreferences(_ preferences: Identity.Preferences) -> AnyPublisher<Never, Error> {
|
2020-10-05 22:50:05 +00:00
|
|
|
identityDatabase.updatePreferences(preferences, id: id)
|
2020-08-30 07:16:37 +00:00
|
|
|
.collect()
|
2020-09-07 16:33:36 +00:00
|
|
|
.filter { _ in preferences.useServerPostingReadingPreferences }
|
|
|
|
.flatMap { _ in refreshServerPreferences() }
|
2020-08-15 00:14:21 +00:00
|
|
|
.eraseToAnyPublisher()
|
2020-08-07 21:57:18 +00:00
|
|
|
}
|
2020-08-14 01:24:53 +00:00
|
|
|
|
2020-09-06 21:37:54 +00:00
|
|
|
func createPushSubscription(deviceToken: Data, alerts: PushSubscription.Alerts) -> AnyPublisher<Never, Error> {
|
2020-08-14 01:24:53 +00:00
|
|
|
let publicKey: String
|
|
|
|
let auth: String
|
|
|
|
|
|
|
|
do {
|
2020-09-04 00:54:05 +00:00
|
|
|
publicKey = try secrets.generatePushKeyAndReturnPublicKey().base64EncodedString()
|
|
|
|
auth = try secrets.generatePushAuth().base64EncodedString()
|
2020-08-14 01:24:53 +00:00
|
|
|
} catch {
|
|
|
|
return Fail(error: error).eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
|
|
|
let endpoint = Self.pushSubscriptionEndpointURL
|
2020-09-06 21:37:54 +00:00
|
|
|
.appendingPathComponent(deviceToken.base16EncodedString())
|
2020-10-05 22:50:05 +00:00
|
|
|
.appendingPathComponent(id.uuidString)
|
2020-08-14 01:24:53 +00:00
|
|
|
|
2020-09-04 01:55:46 +00:00
|
|
|
return mastodonAPIClient.request(
|
2020-08-14 01:24:53 +00:00
|
|
|
PushSubscriptionEndpoint.create(
|
|
|
|
endpoint: endpoint,
|
|
|
|
publicKey: publicKey,
|
|
|
|
auth: auth,
|
|
|
|
alerts: alerts))
|
2020-10-05 22:50:05 +00:00
|
|
|
.map { ($0.alerts, deviceToken, id) }
|
|
|
|
.flatMap(identityDatabase.updatePushSubscription(alerts:deviceToken:id:))
|
2020-08-14 21:41:55 +00:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2020-08-26 09:19:38 +00:00
|
|
|
func updatePushSubscription(alerts: PushSubscription.Alerts) -> AnyPublisher<Never, Error> {
|
2020-09-07 16:33:36 +00:00
|
|
|
mastodonAPIClient.request(PushSubscriptionEndpoint.update(alerts: alerts))
|
2020-10-05 22:50:05 +00:00
|
|
|
.map { ($0.alerts, nil, id) }
|
|
|
|
.flatMap(identityDatabase.updatePushSubscription(alerts:deviceToken:id:))
|
2020-08-14 01:24:53 +00:00
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
2020-08-18 05:13:37 +00:00
|
|
|
|
2020-10-05 07:04:15 +00:00
|
|
|
func service(timeline: Timeline) -> TimelineService {
|
|
|
|
TimelineService(timeline: timeline, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
|
2020-08-18 05:13:37 +00:00
|
|
|
}
|
2020-10-30 07:11:24 +00:00
|
|
|
|
2020-12-01 23:23:47 +00:00
|
|
|
func service(accountList: AccountsEndpoint) -> AccountListService {
|
|
|
|
AccountListService(
|
|
|
|
endpoint: accountList,
|
|
|
|
mastodonAPIClient: mastodonAPIClient,
|
|
|
|
contentDatabase: contentDatabase)
|
|
|
|
}
|
|
|
|
|
2020-10-30 07:11:24 +00:00
|
|
|
func notificationsService() -> NotificationsService {
|
|
|
|
NotificationsService(mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
|
|
|
|
}
|
2020-10-29 06:03:45 +00:00
|
|
|
|
|
|
|
func conversationsService() -> ConversationsService {
|
|
|
|
ConversationsService(mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
|
|
|
|
}
|
2020-12-04 03:13:18 +00:00
|
|
|
|
|
|
|
func domainBlocksService() -> DomainBlocksService {
|
|
|
|
DomainBlocksService(mastodonAPIClient: mastodonAPIClient)
|
|
|
|
}
|
2020-08-14 01:24:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private extension IdentityService {
|
|
|
|
#if DEBUG
|
|
|
|
static let pushSubscriptionEndpointURL = URL(string: "https://metatext-apns.metabolist.com/push?sandbox=true")!
|
|
|
|
#else
|
|
|
|
static let pushSubscriptionEndpointURL = URL(string: "https://metatext-apns.metabolist.com/push")!
|
|
|
|
#endif
|
2020-08-07 21:57:18 +00:00
|
|
|
}
|