feature: add trends API
This commit is contained in:
parent
9ddd8365d0
commit
0033ea0680
|
@ -7,13 +7,17 @@
|
|||
|
||||
import Foundation
|
||||
import Combine
|
||||
import MastodonSDK
|
||||
import UIKit
|
||||
|
||||
final class SearchViewModel {
|
||||
|
||||
var disposeBag = Set<AnyCancellable>()
|
||||
|
||||
// input
|
||||
|
||||
let context: AppContext
|
||||
// input
|
||||
let username = CurrentValueSubject<String, Never>("")
|
||||
|
||||
init(context: AppContext) {
|
||||
self.context = context
|
||||
|
|
|
@ -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<Mastodon.Response.Content<[Mastodon.Entity.SearchResult]>, Error> {
|
||||
query: Query,
|
||||
authorization: Mastodon.API.OAuth.Authorization
|
||||
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.SearchResult>, 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)) }
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
//
|
||||
// File.swift
|
||||
//
|
||||
//
|
||||
// Created by sxiaojian on 2021/3/31.
|
||||
//
|
||||
|
||||
import Foundation
|
|
@ -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<Mastodon.Response.Content<[Mastodon.Entity.Tag]>, 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
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue