diff --git a/Mastodon/Scene/Search/SearchViewModel.swift b/Mastodon/Scene/Search/SearchViewModel.swift index 40eddb965..bdb7e0a22 100644 --- a/Mastodon/Scene/Search/SearchViewModel.swift +++ b/Mastodon/Scene/Search/SearchViewModel.swift @@ -7,13 +7,17 @@ import Foundation import Combine +import MastodonSDK +import UIKit final class SearchViewModel { var disposeBag = Set() - // input + let context: AppContext + // input + let username = CurrentValueSubject("") init(context: AppContext) { self.context = context diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Search.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Search.swift index bfb7b0566..114ca27b8 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Search.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Search.swift @@ -1,12 +1,12 @@ // // Mastodon+API+Search.swift -// +// // // Created by sxiaojian on 2021/3/31. // -import Foundation import Combine +import Foundation /// Search results /// @@ -21,29 +21,28 @@ import Combine /// - Parameters: /// - session: `URLSession` /// - domain: Mastodon instance domain. e.g. "example.com" -/// - statusID: id for status -/// - authorization: User token. Could be nil if status is public +/// - query: search query /// - Returns: `AnyPublisher` contains `Accounts,Hashtags,Status` nested in the response extension Mastodon.API.Search { - static func searchURL(domain: String) -> URL { - return Mastodon.API.endpointURL(domain: domain).appendingPathComponent("api/v2/search") + Mastodon.API.endpointURL(domain: domain).appendingPathComponent("api/v2/search") } - + public static func search( session: URLSession, domain: String, - query: Query - ) -> AnyPublisher, Error> { + query: Query, + authorization: Mastodon.API.OAuth.Authorization + ) -> AnyPublisher, Error> { let request = Mastodon.API.get( url: searchURL(domain: domain), query: query, - authorization: nil + authorization: authorization ) return session.dataTaskPublisher(for: request) .tryMap { data, response in - let value = try Mastodon.API.decode(type: [Mastodon.Entity.SearchResult].self, from: data, response: response) + let value = try Mastodon.API.decode(type: Mastodon.Entity.SearchResult.self, from: data, response: response) return Mastodon.Response.Content(value: value, response: response) } .eraseToAnyPublisher() @@ -64,7 +63,7 @@ extension Mastodon.API.Search { self.offset = offset self.following = following } - + public let accountID: Mastodon.Entity.Account.ID? public let maxID: Mastodon.Entity.Status.ID? public let minID: Mastodon.Entity.Status.ID? @@ -75,17 +74,17 @@ extension Mastodon.API.Search { public let limit: Int? // Maximum number of results to load, per type. Defaults to 20. Max 40. public let offset: Int? // Offset in search results. Used for pagination. Defaults to 0. public let following: Bool? // Only include accounts that the user is following. Defaults to false. - + var queryItems: [URLQueryItem]? { var items: [URLQueryItem] = [] - accountID.flatMap{ items.append(URLQueryItem(name: "account_id", value: $0)) } + accountID.flatMap { items.append(URLQueryItem(name: "account_id", value: $0)) } maxID.flatMap { items.append(URLQueryItem(name: "max_id", value: $0)) } minID.flatMap { items.append(URLQueryItem(name: "min_id", value: $0)) } type.flatMap { items.append(URLQueryItem(name: "type", value: $0)) } - excludeUnreviewed.flatMap{ items.append(URLQueryItem(name: "exclude_unreviewed", value: $0.queryItemValue)) } + excludeUnreviewed.flatMap { items.append(URLQueryItem(name: "exclude_unreviewed", value: $0.queryItemValue)) } items.append(URLQueryItem(name: "q", value: q)) resolve.flatMap { items.append(URLQueryItem(name: "resolve", value: $0.queryItemValue)) } - + limit.flatMap { items.append(URLQueryItem(name: "limit", value: String($0))) } offset.flatMap { items.append(URLQueryItem(name: "offset", value: String($0))) } following.flatMap { items.append(URLQueryItem(name: "following", value: $0.queryItemValue)) } diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Suggestions.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Suggestions.swift new file mode 100644 index 000000000..bc2adaee4 --- /dev/null +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Suggestions.swift @@ -0,0 +1,8 @@ +// +// File.swift +// +// +// Created by sxiaojian on 2021/3/31. +// + +import Foundation diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Trends.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Trends.swift new file mode 100644 index 000000000..b311cf2b4 --- /dev/null +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Trends.swift @@ -0,0 +1,65 @@ +// +// Mastodon+API+Trends.swift +// +// +// Created by sxiaojian on 2021/3/31. +// + +import Combine +import Foundation + +/// Trending tags +/// +/// Tags that are being used more frequently within the past week. +/// +/// Version history: +/// 3.0.0 - added +/// # Reference +/// [Document](https://docs.joinmastodon.org/methods/instance/trends/) +/// - Parameters: +/// - session: `URLSession` +/// - domain: Mastodon instance domain. e.g. "example.com" +/// - query: query +/// - authorization: User token. +/// - Returns: `AnyPublisher` contains `Hashtags` nested in the response + +extension Mastodon.API.Trends { + static func trendsURL(domain: String) -> URL { + Mastodon.API.endpointURL(domain: domain).appendingPathComponent("/api/v1/trends") + } + + public static func get( + session: URLSession, + domain: String, + query: Query + ) -> AnyPublisher, Error> { + let request = Mastodon.API.get( + url: trendsURL(domain: domain), + query: query, + authorization: nil + ) + return session.dataTaskPublisher(for: request) + .tryMap { data, response in + let value = try Mastodon.API.decode(type: [Mastodon.Entity.Tag].self, from: data, response: response) + return Mastodon.Response.Content(value: value, response: response) + } + .eraseToAnyPublisher() + } +} + +extension Mastodon.API.Trends { + public struct Query: Codable, GetQuery { + public init(limit: Int?) { + self.limit = limit + } + + public let limit: Int? // Maximum number of results to return. Defaults to 10. + + var queryItems: [URLQueryItem]? { + var items: [URLQueryItem] = [] + limit.flatMap { items.append(URLQueryItem(name: "limit", value: String($0))) } + guard !items.isEmpty else { return nil } + return items + } + } +} diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API.swift index 239031cd1..644c86b91 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API.swift @@ -102,6 +102,8 @@ extension Mastodon.API { public enum Statuses { } public enum Timeline { } public enum Search { } + public enum Trends { } + public enum Suggestions { } } extension Mastodon.API {