metatext-app-ios-iphone-ipad/ServiceLayer/Sources/ServiceLayer/Services/NavigationService.swift

166 lines
5.6 KiB
Swift
Raw Normal View History

2020-09-15 03:39:35 +02:00
// Copyright © 2020 Metabolist. All rights reserved.
import Combine
import DB
import Foundation
import Mastodon
import MastodonAPI
2020-09-25 07:39:06 +02:00
public enum Navigation {
2020-09-15 03:39:35 +02:00
case url(URL)
2020-10-05 08:36:22 +02:00
case collection(CollectionService)
2020-09-27 04:03:53 +02:00
case profile(ProfileService)
2020-09-26 08:37:30 +02:00
case webfingerStart
case webfingerEnd
2020-09-15 03:39:35 +02:00
}
2020-09-25 07:39:06 +02:00
public struct NavigationService {
2020-09-15 03:39:35 +02:00
private let mastodonAPIClient: MastodonAPIClient
private let contentDatabase: ContentDatabase
2020-10-05 21:58:03 +02:00
private let status: Status?
2020-09-15 03:39:35 +02:00
2020-10-05 21:58:03 +02:00
init(mastodonAPIClient: MastodonAPIClient, contentDatabase: ContentDatabase, status: Status? = nil) {
2020-09-15 03:39:35 +02:00
self.mastodonAPIClient = mastodonAPIClient
self.contentDatabase = contentDatabase
2020-10-05 21:58:03 +02:00
self.status = status
2020-09-15 03:39:35 +02:00
}
}
2020-09-25 07:39:06 +02:00
public extension NavigationService {
func item(url: URL) -> AnyPublisher<Navigation, Never> {
2020-09-15 03:39:35 +02:00
if let tag = tag(url: url) {
2020-09-25 07:39:06 +02:00
return Just(
2020-10-05 08:36:22 +02:00
.collection(
2020-10-05 09:04:15 +02:00
TimelineService(
2020-09-25 07:39:06 +02:00
timeline: .tag(tag),
mastodonAPIClient: mastodonAPIClient,
contentDatabase: contentDatabase)))
.eraseToAnyPublisher()
2020-09-15 03:39:35 +02:00
} else if let accountID = accountID(url: url) {
2020-09-27 04:03:53 +02:00
return Just(.profile(profileService(id: accountID))).eraseToAnyPublisher()
2020-09-15 03:39:35 +02:00
} else if mastodonAPIClient.instanceURL.host == url.host, let statusID = url.statusID {
2020-10-05 09:04:15 +02:00
return Just(.collection(contextService(id: statusID))).eraseToAnyPublisher()
2020-09-15 03:39:35 +02:00
}
2020-09-26 08:37:30 +02:00
if url.shouldWebfinger {
return webfinger(url: url)
} else {
return Just(.url(url)).eraseToAnyPublisher()
}
2020-09-15 03:39:35 +02:00
}
2020-09-25 07:39:06 +02:00
2020-10-05 09:04:15 +02:00
func contextService(id: String) -> ContextService {
2020-10-05 22:22:47 +02:00
ContextService(parentID: id, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
2020-09-25 07:39:06 +02:00
}
2020-09-27 04:03:53 +02:00
func profileService(id: String) -> ProfileService {
ProfileService(id: id, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
2020-09-25 07:39:06 +02:00
}
2020-09-27 04:03:53 +02:00
func profileService(account: Account) -> ProfileService {
ProfileService(account: account, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
}
2020-09-25 07:39:06 +02:00
func statusService(status: Status) -> StatusService {
StatusService(status: status, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
}
func accountService(account: Account) -> AccountService {
AccountService(account: account, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
}
2020-10-04 10:39:54 +02:00
func loadMoreService(loadMore: LoadMore) -> LoadMoreService {
LoadMoreService(loadMore: loadMore, mastodonAPIClient: mastodonAPIClient, contentDatabase: contentDatabase)
}
2020-09-15 03:39:35 +02:00
}
2020-09-25 07:39:06 +02:00
private extension NavigationService {
2020-09-15 03:39:35 +02:00
func tag(url: URL) -> String? {
if status?.tags.first(where: { $0.url.path.lowercased() == url.path.lowercased() }) != nil {
return url.lastPathComponent
} else if
mastodonAPIClient.instanceURL.host == url.host {
return url.tag
}
return nil
}
func accountID(url: URL) -> String? {
if let mentionID = status?.mentions.first(where: { $0.url.path.lowercased() == url.path.lowercased() })?.id {
return mentionID
} else if
mastodonAPIClient.instanceURL.host == url.host {
return url.accountID
}
return nil
}
2020-09-26 08:37:30 +02:00
func webfinger(url: URL) -> AnyPublisher<Navigation, Never> {
let navigationSubject = PassthroughSubject<Navigation, Never>()
let request = mastodonAPIClient.request(ResultsEndpoint.search(query: url.absoluteString, resolve: true))
.handleEvents(
receiveSubscription: { _ in navigationSubject.send(.webfingerStart) },
receiveCompletion: { _ in navigationSubject.send(.webfingerEnd) })
.map { results -> Navigation in
if let tag = results.hashtags.first {
2020-10-05 08:36:22 +02:00
return .collection(
2020-10-05 09:04:15 +02:00
TimelineService(
2020-09-26 08:37:30 +02:00
timeline: .tag(tag.name),
mastodonAPIClient: mastodonAPIClient,
contentDatabase: contentDatabase))
} else if let account = results.accounts.first {
2020-09-27 04:03:53 +02:00
return .profile(profileService(account: account))
2020-09-26 08:37:30 +02:00
} else if let status = results.statuses.first {
2020-10-05 09:04:15 +02:00
return .collection(contextService(id: status.id))
2020-09-26 08:37:30 +02:00
} else {
return .url(url)
}
}
.replaceError(with: .url(url))
return navigationSubject.merge(with: request).eraseToAnyPublisher()
}
2020-09-15 03:39:35 +02:00
}
private extension URL {
var isAccountURL: Bool {
(pathComponents.count == 2 && pathComponents[1].starts(with: "@"))
|| (pathComponents.count == 3 && pathComponents[0...1] == ["/", "users"])
}
var accountID: String? {
if let accountID = pathComponents.last, pathComponents == ["/", "web", "accounts", accountID] {
return accountID
}
return nil
}
var statusID: String? {
guard let statusID = pathComponents.last else { return nil }
if pathComponents.count == 3, pathComponents[1].starts(with: "@") {
return statusID
} else if pathComponents == ["/", "web", "statuses", statusID] {
return statusID
}
return nil
}
var tag: String? {
if let tag = pathComponents.last, pathComponents == ["/", "tags", tag] {
return tag
}
return nil
}
var shouldWebfinger: Bool {
isAccountURL || accountID != nil || statusID != nil || tag != nil
}
}