Skip to content

Commit

Permalink
Merge pull request #176 from Tunous/search-accounts
Browse files Browse the repository at this point in the history
Search accounts endpoint
  • Loading branch information
kkostov committed Jul 8, 2023
2 parents 01f114d + 096523d commit 420f163
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
41 changes: 41 additions & 0 deletions Sources/TootSDK/Models/SearchAccountsParams.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// SearchAccountsParams.swift
// Created by Łukasz Rutkowski on 03/07/2023.
//

import Foundation

public struct SearchAccountsParams: Sendable {
/// The search query.
public var query: String
/// Attempt WebFinger lookup? Defaults to `false`.
public var resolve: Bool?
/// Only include accounts that the user is following? Defaults to `false`.
public var following: Bool?

/// The search parameters.
///
/// - Parameters:
/// - query: The search query.
/// - resolve: Attempt WebFinger lookup? Defaults to `false`.
/// - following: Only include accounts that the user is following? Defaults to `false`.
public init(
query: String,
resolve: Bool? = nil,
following: Bool? = nil
) {
self.query = query
self.resolve = resolve
self.following = following
}
}

extension SearchAccountsParams {
var queryItems: [URLQueryItem] {
[
URLQueryItem(name: "q", value: query),
URLQueryItem(name: "resolve", value: resolve?.description),
URLQueryItem(name: "following", value: following?.description)
].filter { $0.value != nil }
}
}
17 changes: 16 additions & 1 deletion Sources/TootSDK/TootClient/TootClient+Account.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ extension TootClient {
}
}

/// Search for matching accounts by username or display name.
///
/// - Parameters:
/// - params: The search parameters.
/// - limit: Maximum number of results to return. Defaults to 40. Max 80 accounts.
/// - offset: Skip the first n results.
/// - Returns: Search results.
public func searchAccounts(params: SearchAccountsParams, limit: Int? = nil, offset: Int? = nil) async throws -> [Account] {
let req = HTTPRequestBuilder {
$0.url = getURL(["api", "v1", "accounts", "search"])
$0.method = .get
$0.query = getQueryParams(limit: limit, offset: offset) + params.queryItems
}
return try await fetch([Account].self, req)
}

// swiftlint:disable todo
// TODO: - Update account credentials

Expand All @@ -94,7 +110,6 @@ extension TootClient {
// TODO: - Set private note on profile

// TODO: - Find familiar followers
// TODO: - Search for matching accounts
// TODO: - Lookup account ID from Webfinger address
// swiftlint:enable todo
}

0 comments on commit 420f163

Please sign in to comment.