2023-03-27 22:41:36 +02:00
|
|
|
// Copyright © 2023 Mastodon gGmbH. All rights reserved.
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Intents
|
2023-03-27 22:57:11 +02:00
|
|
|
import MastodonSDK
|
2023-03-27 22:41:36 +02:00
|
|
|
|
|
|
|
class HashtagIntentHandler: INExtension, HashtagIntentHandling {
|
|
|
|
func provideHashtagOptionsCollection(for intent: HashtagIntent, searchTerm: String?) async throws -> INObjectCollection<NSString> {
|
|
|
|
|
|
|
|
guard let authenticationBox = WidgetExtension.appContext
|
|
|
|
.authenticationService
|
|
|
|
.mastodonAuthenticationBoxes
|
|
|
|
.first
|
|
|
|
else {
|
|
|
|
return INObjectCollection(items: [])
|
|
|
|
}
|
|
|
|
|
|
|
|
var results: [NSString] = []
|
|
|
|
|
|
|
|
if let searchTerm, searchTerm.isEmpty == false {
|
|
|
|
let searchResults = try await WidgetExtension.appContext
|
|
|
|
.apiService
|
|
|
|
.search(query: .init(q: searchTerm, type: .hashtags), authenticationBox: authenticationBox)
|
|
|
|
.value
|
2023-03-27 22:57:11 +02:00
|
|
|
.hashtags
|
|
|
|
.compactMap { $0.name as NSString }
|
|
|
|
|
2023-03-27 22:41:36 +02:00
|
|
|
results = searchResults
|
|
|
|
|
|
|
|
} else {
|
2023-03-27 22:57:11 +02:00
|
|
|
let followedTags = try await WidgetExtension.appContext.apiService.getFollowedTags(
|
|
|
|
domain: authenticationBox.domain,
|
|
|
|
query: Mastodon.API.Account.FollowedTagsQuery(limit: nil),
|
|
|
|
authenticationBox: authenticationBox)
|
|
|
|
.value
|
|
|
|
.compactMap { $0.name as NSString }
|
|
|
|
|
|
|
|
results = followedTags
|
|
|
|
|
2023-03-27 22:41:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return INObjectCollection(items: results)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|