Use authenticated domain for API calls. Modify URL scheme

This commit is contained in:
jinsu kim 2023-01-01 15:55:54 -08:00
parent de962a0c09
commit 5c82c04232
4 changed files with 67 additions and 67 deletions

View File

@ -17,7 +17,6 @@ final class RemoteProfileViewModel: ProfileViewModel {
init(context: AppContext, authContext: AuthContext, userID: Mastodon.Entity.Account.ID) { init(context: AppContext, authContext: AuthContext, userID: Mastodon.Entity.Account.ID) {
super.init(context: context, authContext: authContext, optionalMastodonUser: nil) super.init(context: context, authContext: authContext, optionalMastodonUser: nil)
Task { @MainActor in
let domain = authContext.mastodonAuthenticationBox.domain let domain = authContext.mastodonAuthenticationBox.domain
let authorization = authContext.mastodonAuthenticationBox.userAuthorization let authorization = authContext.mastodonAuthenticationBox.userAuthorization
Just(userID) Just(userID)
@ -53,7 +52,6 @@ final class RemoteProfileViewModel: ProfileViewModel {
} }
.store(in: &disposeBag) .store(in: &disposeBag)
} }
}
init(context: AppContext, authContext: AuthContext, notificationID: Mastodon.Entity.Notification.ID) { init(context: AppContext, authContext: AuthContext, notificationID: Mastodon.Entity.Notification.ID) {
super.init(context: context, authContext: authContext, optionalMastodonUser: nil) super.init(context: context, authContext: authContext, optionalMastodonUser: nil)

View File

@ -77,8 +77,7 @@ extension ThreadViewModel.LoadThreadState {
do { do {
let response = try await viewModel.context.apiService.statusContext( let response = try await viewModel.context.apiService.statusContext(
statusID: threadContext.statusID, statusID: threadContext.statusID,
authenticationBox: viewModel.authContext.mastodonAuthenticationBox, authenticationBox: viewModel.authContext.mastodonAuthenticationBox
domain: threadContext.domain
) )
await enter(state: NoMore.self) await enter(state: NoMore.self)

View File

@ -245,29 +245,17 @@ extension SceneDelegate {
print("source application = \(sendingAppID ?? "Unknown")") print("source application = \(sendingAppID ?? "Unknown")")
print("url = \(url)") print("url = \(url)")
if let username = url.user { switch url.host {
guard let host = url.host else { return } case "post":
showComposeViewController()
case "profile":
let components = url.pathComponents let components = url.pathComponents
if components.count == 3 && components[1] == "status" { if components.count == 2 && components[0] == "/" {
let statusId = components[2] let addr = components[1]
// View post from user let tokens = addr.components(separatedBy: "@")
print("view status \(statusId)") if tokens.count != 2 { return }
if let authContext = coordinator?.authContext { let username = tokens[0]
Task { let host = tokens[1]
guard let thread = try await AppContext.shared.apiService.fetchThread(
statusID: statusId,
domain: host,
authenticationBox: authContext.mastodonAuthenticationBox
) else { return }
let threadViewModel = CachedThreadViewModel(context: AppContext.shared,
authContext: authContext,
status: thread)
coordinator?.present(scene: .thread(viewModel: threadViewModel), from: nil, transition: .show)
}
}
} else {
print("view profile \(username)@\(host)")
if let authContext = coordinator?.authContext { if let authContext = coordinator?.authContext {
Task { @MainActor in Task { @MainActor in
guard let user = try await AppContext.shared.apiService.fetchUser( guard let user = try await AppContext.shared.apiService.fetchUser(
@ -287,12 +275,28 @@ extension SceneDelegate {
} }
} }
} }
} else { case "status":
guard let action = url.host else { return } let components = url.pathComponents
if action == "post" { if components.count == 2 && components[0] == "/" {
print("make post") let statusId = components[1]
showComposeViewController() // View post from user
print("view status \(statusId)")
if let authContext = coordinator?.authContext {
Task {
guard let thread = try await AppContext.shared.apiService.fetchThread(
statusID: statusId,
authenticationBox: authContext.mastodonAuthenticationBox
) else { return }
let threadViewModel = CachedThreadViewModel(context: AppContext.shared,
authContext: authContext,
status: thread)
coordinator?.present(scene: .thread(viewModel: threadViewModel), from: nil, transition: .show)
} }
} }
} }
default:
return
}
}
} }

View File

@ -16,10 +16,9 @@ extension APIService {
public func statusContext( public func statusContext(
statusID: Mastodon.Entity.Status.ID, statusID: Mastodon.Entity.Status.ID,
authenticationBox: MastodonAuthenticationBox, authenticationBox: MastodonAuthenticationBox
domain: String? = nil
) async throws -> Mastodon.Response.Content<Mastodon.Entity.Context> { ) async throws -> Mastodon.Response.Content<Mastodon.Entity.Context> {
let domain = domain ?? authenticationBox.domain let domain = authenticationBox.domain
let authorization = authenticationBox.userAuthorization let authorization = authenticationBox.userAuthorization
let response = try await Mastodon.API.Statuses.statusContext( let response = try await Mastodon.API.Statuses.statusContext(
@ -54,9 +53,9 @@ extension APIService {
public func fetchThread( public func fetchThread(
statusID: Mastodon.Entity.Status.ID, statusID: Mastodon.Entity.Status.ID,
domain: String,
authenticationBox: MastodonAuthenticationBox authenticationBox: MastodonAuthenticationBox
) async throws -> Status? { ) async throws -> Status? {
let domain = authenticationBox.domain
let authorization = authenticationBox.userAuthorization let authorization = authenticationBox.userAuthorization
let managedObjectContext = self.backgroundManagedObjectContext let managedObjectContext = self.backgroundManagedObjectContext