shannon 77f3c5a64d Combine AuthenticationService into AuthenticationServiceProvider
Also, AppContext, APIService, and AuthenticationServiceProvider are now more obviously singletons.

And AuthenticationServiceProvider can now be asked for the current active user, instead of every caller assuming the first element of a list of users is the active user.
2024-11-14 16:30:51 -05:00

43 lines
1.4 KiB
Swift

// Copyright © 2023 Mastodon gGmbH. All rights reserved.
import Foundation
import Intents
import MastodonSDK
import MastodonCore
class HashtagIntentHandler: INExtension, HashtagIntentHandling {
func provideHashtagOptionsCollection(for intent: HashtagIntent, searchTerm: String?) async throws -> INObjectCollection<NSString> {
guard let authenticationBox = AuthenticationServiceProvider.shared.activeAuthentication
else {
return INObjectCollection(items: [])
}
var results: [NSString] = []
if let searchTerm, searchTerm.isEmpty == false {
let searchResults = try await AppContext.shared.apiService
.search(query: .init(q: searchTerm, type: .hashtags), authenticationBox: authenticationBox)
.value
.hashtags
.compactMap { "#\($0.name)" as NSString }
results = searchResults
} else {
let followedTags = try await AppContext.shared.apiService.getFollowedTags(
domain: authenticationBox.domain,
query: Mastodon.API.Account.FollowedTagsQuery(limit: nil),
authenticationBox: authenticationBox)
.value
.compactMap { "#\($0.name)" as NSString }
results = followedTags
}
return INObjectCollection(items: results)
}
}