Add authentication to trends (#1104)

And clean up here and there.
This commit is contained in:
Nathan Mattes 2023-08-21 11:29:48 +02:00
parent 3909675cd4
commit a20f9a57c2
13 changed files with 49 additions and 94 deletions

View File

@ -34,11 +34,6 @@ final class DiscoveryCommunityViewController: UIViewController, NeedsDependency,
}()
let refreshControl = RefreshControl()
deinit {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
}
}
extension DiscoveryCommunityViewController {

View File

@ -5,7 +5,6 @@
// Created by MainasuK on 2022-4-29.
//
import os.log
import Foundation
import GameplayKit
import MastodonSDK
@ -13,8 +12,6 @@ import MastodonSDK
extension DiscoveryCommunityViewModel {
class State: GKState {
let logger = Logger(subsystem: "DiscoveryCommunityViewModel.State", category: "StateMachine")
let id = UUID()
weak var viewModel: DiscoveryCommunityViewModel?
@ -28,17 +25,12 @@ extension DiscoveryCommunityViewModel {
let from = previousState.flatMap { String(describing: $0) } ?? "nil"
let to = String(describing: self)
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): \(from) -> \(to)")
}
@MainActor
func enter(state: State.Type) {
stateMachine?.enter(state)
}
deinit {
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): [\(self.id.uuidString)] \(String(describing: self))")
}
}
}
@ -87,9 +79,7 @@ extension DiscoveryCommunityViewModel.State {
super.didEnter(from: previousState)
guard let _ = viewModel, let stateMachine = stateMachine else { return }
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: retry loading 3s later…", ((#file as NSString).lastPathComponent), #line, #function)
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: retry loading", ((#file as NSString).lastPathComponent), #line, #function)
stateMachine.enter(Loading.self)
}
}
@ -174,7 +164,6 @@ extension DiscoveryCommunityViewModel.State {
viewModel.didLoadLatest.send()
} catch {
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): fetch user timeline fail: \(error.localizedDescription)")
await enter(state: Fail.self)
}
} // end Task

View File

@ -16,8 +16,6 @@ import MastodonCore
final class DiscoveryCommunityViewModel {
let logger = Logger(subsystem: "DiscoveryCommunityViewModel", category: "ViewModel")
var disposeBag = Set<AnyCancellable>()
// input
@ -54,9 +52,4 @@ final class DiscoveryCommunityViewModel {
)
// end init
}
deinit {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
}
}

View File

@ -5,7 +5,6 @@
// Created by MainasuK on 2022-4-14.
//
import os.log
import UIKit
import Combine
import MastodonUI
@ -13,8 +12,6 @@ import MastodonCore
final class DiscoveryForYouViewController: UIViewController, NeedsDependency, MediaPreviewableViewController {
let logger = Logger(subsystem: "DiscoveryForYouViewController", category: "ViewController")
weak var context: AppContext! { willSet { precondition(!isViewLoaded) } }
weak var coordinator: SceneCoordinator! { willSet { precondition(!isViewLoaded) } }
@ -33,11 +30,6 @@ final class DiscoveryForYouViewController: UIViewController, NeedsDependency, Me
}()
let refreshControl = RefreshControl()
deinit {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
}
}
extension DiscoveryForYouViewController {
@ -111,7 +103,6 @@ extension DiscoveryForYouViewController: AuthContextProvider {
extension DiscoveryForYouViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): \(indexPath)")
guard case let .user(record) = viewModel.diffableDataSource?.itemIdentifier(for: indexPath) else { return }
guard let user = record.object(in: context.managedObjectContext) else { return }
let profileViewModel = CachedProfileViewModel(

View File

@ -5,7 +5,6 @@
// Created by MainasuK on 2022-4-14.
//
import os.log
import UIKit
import Combine
import GameplayKit
@ -41,11 +40,6 @@ final class DiscoveryForYouViewModel {
)
// end init
}
deinit {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
}
}
extension DiscoveryForYouViewModel {

View File

@ -16,8 +16,6 @@ import MastodonSDK
final class DiscoveryHashtagsViewModel {
let logger = Logger(subsystem: "DiscoveryHashtagsViewModel", category: "ViewModel")
var disposeBag = Set<AnyCancellable>()
// input
@ -36,8 +34,12 @@ final class DiscoveryHashtagsViewModel {
viewDidAppeared
.throttle(for: 3, scheduler: DispatchQueue.main, latest: true)
.asyncMap { authenticationBox in
try await context.apiService.trendHashtags(domain: authContext.mastodonAuthenticationBox.domain, query: nil)
.asyncMap { _ in
let authenticationBox = authContext.mastodonAuthenticationBox
return try await context.apiService.trendHashtags(domain: authenticationBox.domain,
query: nil,
authenticationBox: authenticationBox
)
}
.retry(3)
.map { response in Result<Mastodon.Response.Content<[Mastodon.Entity.Tag]>, Error> { response } }
@ -54,20 +56,19 @@ final class DiscoveryHashtagsViewModel {
}
.store(in: &disposeBag)
}
deinit {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
}
}
extension DiscoveryHashtagsViewModel {
@MainActor
func fetch() async throws {
let response = try await context.apiService.trendHashtags(domain: authContext.mastodonAuthenticationBox.domain, query: nil)
let authenticationBox = authContext.mastodonAuthenticationBox
let response = try await context.apiService.trendHashtags(domain: authenticationBox.domain,
query: nil,
authenticationBox: authenticationBox
)
hashtags = response.value.filter { !$0.name.isEmpty }
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): fetch tags: \(response.value.count)")
}
}

View File

@ -35,10 +35,6 @@ extension DiscoveryNewsViewModel {
func enter(state: State.Type) {
stateMachine?.enter(state)
}
deinit {
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): [\(self.id.uuidString)] \(String(describing: self))")
}
}
}
@ -87,9 +83,8 @@ extension DiscoveryNewsViewModel.State {
super.didEnter(from: previousState)
guard let _ = viewModel, let stateMachine = stateMachine else { return }
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: retry loading 3s later…", ((#file as NSString).lastPathComponent), #line, #function)
// try reloading three seconds later
DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: retry loading", ((#file as NSString).lastPathComponent), #line, #function)
stateMachine.enter(Loading.self)
}
}
@ -144,7 +139,8 @@ extension DiscoveryNewsViewModel.State {
query: Mastodon.API.Trends.StatusQuery(
offset: offset,
limit: nil
)
),
authenticationBox: viewModel.authContext.mastodonAuthenticationBox
)
let newOffset: Int? = {
guard let offset = response.link?.offset else { return nil }
@ -174,7 +170,6 @@ extension DiscoveryNewsViewModel.State {
viewModel.links = links
viewModel.didLoadLatest.send()
} catch {
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): fetch news fail: \(error.localizedDescription)")
if let error = error as? Mastodon.API.Error, error.httpResponseStatus.code == 404 {
viewModel.isServerSupportEndpoint = false
await enter(state: NoMore.self)

View File

@ -51,11 +51,6 @@ final class DiscoveryNewsViewModel {
await checkServerEndpoint()
} // end Task
}
deinit {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
}
}
@ -64,7 +59,8 @@ extension DiscoveryNewsViewModel {
do {
_ = try await context.apiService.trendLinks(
domain: authContext.mastodonAuthenticationBox.domain,
query: .init(offset: nil, limit: nil)
query: .init(offset: nil, limit: nil),
authenticationBox: authContext.mastodonAuthenticationBox
)
} catch let error as Mastodon.API.Error where error.httpResponseStatus.code == 404 {
isServerSupportEndpoint = false

View File

@ -35,11 +35,6 @@ final class DiscoveryPostsViewController: UIViewController, NeedsDependency, Med
let refreshControl = RefreshControl()
let discoveryIntroBannerView = DiscoveryIntroBannerView()
deinit {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
}
}
extension DiscoveryPostsViewController {

View File

@ -145,7 +145,8 @@ extension DiscoveryPostsViewModel.State {
query: Mastodon.API.Trends.StatusQuery(
offset: offset,
limit: nil
)
),
authenticationBox: viewModel.authContext.mastodonAuthenticationBox
)
let newOffset: Int? = {
guard let offset = response.link?.offset else { return nil }

View File

@ -5,7 +5,6 @@
// Created by MainasuK on 2022-4-12.
//
import os.log
import UIKit
import Combine
import GameplayKit
@ -56,11 +55,6 @@ final class DiscoveryPostsViewModel {
await checkServerEndpoint()
} // end Task
}
deinit {
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
}
}
extension DiscoveryPostsViewModel {
@ -68,7 +62,8 @@ extension DiscoveryPostsViewModel {
do {
_ = try await context.apiService.trendStatuses(
domain: authContext.mastodonAuthenticationBox.domain,
query: .init(offset: nil, limit: nil)
query: .init(offset: nil, limit: nil),
authenticationBox: authContext.mastodonAuthenticationBox
)
} catch let error as Mastodon.API.Error where error.httpResponseStatus.code == 404 {
isServerSupportEndpoint = false

View File

@ -7,17 +7,20 @@
import Foundation
import MastodonSDK
import CoreDataStack
extension APIService {
public func trendHashtags(
domain: String,
query: Mastodon.API.Trends.HashtagQuery?
query: Mastodon.API.Trends.HashtagQuery?,
authenticationBox: MastodonAuthenticationBox
) async throws -> Mastodon.Response.Content<[Mastodon.Entity.Tag]> {
let response = try await Mastodon.API.Trends.hashtags(
session: session,
domain: domain,
query: query
query: query,
authorization: authenticationBox.userAuthorization
).singleOutput()
return response
@ -25,12 +28,14 @@ extension APIService {
public func trendStatuses(
domain: String,
query: Mastodon.API.Trends.StatusQuery
query: Mastodon.API.Trends.StatusQuery,
authenticationBox: MastodonAuthenticationBox
) async throws -> Mastodon.Response.Content<[Mastodon.Entity.Status]> {
let response = try await Mastodon.API.Trends.statuses(
session: session,
domain: domain,
query: query
query: query,
authorization: authenticationBox.userAuthorization
).singleOutput()
let managedObjectContext = backgroundManagedObjectContext
@ -55,12 +60,14 @@ extension APIService {
public func trendLinks(
domain: String,
query: Mastodon.API.Trends.LinkQuery
query: Mastodon.API.Trends.LinkQuery,
authenticationBox: MastodonAuthenticationBox
) async throws -> Mastodon.Response.Content<[Mastodon.Entity.Link]> {
let response = try await Mastodon.API.Trends.links(
session: session,
domain: domain,
query: query
query: query,
authorization: authenticationBox.userAuthorization
).singleOutput()
return response

View File

@ -31,12 +31,13 @@ extension Mastodon.API.Trends {
public static func hashtags(
session: URLSession,
domain: String,
query: Mastodon.API.Trends.HashtagQuery?
query: Mastodon.API.Trends.HashtagQuery?,
authorization: Mastodon.API.OAuth.Authorization
) -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Tag]>, Error> {
let request = Mastodon.API.get(
url: trendsURL(domain: domain),
query: query,
authorization: nil
authorization: authorization
)
return session.dataTaskPublisher(for: request)
.tryMap { data, response in
@ -88,12 +89,13 @@ extension Mastodon.API.Trends {
public static func statuses(
session: URLSession,
domain: String,
query: Mastodon.API.Trends.StatusQuery?
query: Mastodon.API.Trends.StatusQuery?,
authorization: Mastodon.API.OAuth.Authorization
) -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Status]>, Error> {
let request = Mastodon.API.get(
url: trendStatusesURL(domain: domain),
query: query,
authorization: nil
authorization: authorization
)
return session.dataTaskPublisher(for: request)
.tryMap { data, response in
@ -152,12 +154,13 @@ extension Mastodon.API.Trends {
public static func links(
session: URLSession,
domain: String,
query: Mastodon.API.Trends.LinkQuery?
query: Mastodon.API.Trends.LinkQuery?,
authorization: Mastodon.API.OAuth.Authorization
) -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Link]>, Error> {
let request = Mastodon.API.get(
url: trendLinksURL(domain: domain),
query: query,
authorization: nil
authorization: authorization
)
return session.dataTaskPublisher(for: request)
.tryMap { data, response in