Skip to content

Commit

Permalink
Merge pull request #137 from Tunous/feat/account-lookup
Browse files Browse the repository at this point in the history
[BREAKING] Extended account lookup API, support for Pleroma and Akkoma
  • Loading branch information
kkostov committed Mar 27, 2023
2 parents 14ed146 + 38ce736 commit 91f0149
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 49 deletions.
27 changes: 0 additions & 27 deletions Sources/TootSDK/Models/AccountLookup.swift

This file was deleted.

4 changes: 2 additions & 2 deletions Sources/TootSDK/Models/Relationship.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public struct Relationship: Codable, Hashable, Identifiable, Sendable {
showingReposts: Bool? = nil,
notifying: Bool? = nil,
blocking: Bool,
domainBlocking: Bool,
domainBlocking: Bool?,
blockedBy: Bool? = nil,
note: String? = nil) {
self.id = id
Expand Down Expand Up @@ -56,7 +56,7 @@ public struct Relationship: Codable, Hashable, Identifiable, Sendable {
/// Are you blocking this user?
public let blocking: Bool
/// Are you blocking this user's domain?
public let domainBlocking: Bool
public let domainBlocking: Bool?
/// Is this user blocking you?
public var blockedBy: Bool?
/// This user's profile bio
Expand Down
2 changes: 1 addition & 1 deletion Sources/TootSDK/Models/TootSDKError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public enum TootSDKError: Error, LocalizedError, Equatable {
case missingParameter(parameterName: String)
case invalidParameter(parameterName: String)
/// The requested operation is not supported by the current server flavour.
case unsupportedFlavour(current: TootSDKFlavour, required: [TootSDKFlavour])
case unsupportedFlavour(current: TootSDKFlavour, required: Set<TootSDKFlavour>)
case unexpectedError(_ description: String)
/// The remote instance did not respond with the expected payload during authorization
case clientAuthorizationFailed
Expand Down
10 changes: 3 additions & 7 deletions Sources/TootSDK/TootClient/TootClient+OauthApps.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ public extension TootClient {
/// * This method requires the `admin:write` scope.
/// * This method requires the pleroma API flavour.
func adminGetOauthApps(_ page: Int = 1, params: ListOauthAppsParams? = nil) async throws -> [PleromaOauthApp]? {
guard flavour == .pleroma else {
throw TootSDKError.unsupportedFlavour(current: flavour, required: [.pleroma])
}

try requireFlavour([.pleroma])

let req = HTTPRequestBuilder {
$0.url = getURL(["api", "v1", "pleroma", "admin", "oauth_app"])
$0.method = .get
Expand Down Expand Up @@ -49,9 +47,7 @@ public extension TootClient {
/// * This method requires the `admin:write` scope.
/// * This method requires the pleroma API flavour.
func adminDeleteOauthApp(appId: Int) async throws {
if ![TootSDKFlavour.pleroma].contains(flavour) {
throw TootSDKError.unsupportedFlavour(current: flavour, required: [.pleroma])
}
try requireFlavour([.pleroma])

let req = HTTPRequestBuilder {
$0.url = getURL(["api", "v1", "pleroma", "admin", "oauth_app", "\(appId)"])
Expand Down
2 changes: 1 addition & 1 deletion Sources/TootSDK/TootClient/TootClient+Post.swift
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ public extension TootClient {

/// Obtain the source properties for a status so that it can be edited.
func getPostSource(id: String) async throws -> PostSource {
guard flavour != .friendica else { throw TootSDKError.unsupportedFlavour(current: flavour, required: TootSDKFlavour.allCases.filter({$0 != .friendica})) }
try requireFlavour(otherThan: [.friendica])

let req = HTTPRequestBuilder {
$0.url = getURL(["api", "v1", "statuses", id, "source"])
Expand Down
24 changes: 14 additions & 10 deletions Sources/TootSDK/TootClient/TootClient+Relationships.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,30 @@ extension TootClient {
return try await followAccount(by: accountLookup.id)
}

/// Mastodon Specific. Looks up an account based on it's account name or URI, and returns a payload that containts the instance's account id
/// - Parameter uri: account name on the instance you're on or a users URI (e.g @test@instance.test)
/// - Returns: AccountLookup, a payload containing information about the account looked up
public func lookupAccount(uri: String) async throws -> AccountLookup {
guard flavour == .mastodon else { throw TootSDKError.unsupportedFlavour(current: flavour, required: [.mastodon]) }

/// Looks up an account based on it's account name or URI, and returns a payload that contains the instance's account id
/// - Parameter uri: account name on the instance you're on or a users URI (e.g test@instance.test)
/// - Returns: the looked up account, or an error if unable to retrieve
public func lookupAccount(uri: String) async throws -> Account {
try requireFlavour(otherThan: [.pixelfed, .friendica])

if flavour == .pleroma || flavour == .akkoma {
return try await getAccount(by: uri)
}

let req = HTTPRequestBuilder {
$0.url = getURL(["api", "v1", "accounts", "lookup"])
$0.method = .get
$0.addQueryParameter(name: "acct", value: uri)
}

return try await fetch(AccountLookup.self, req)
return try await fetch(Account.self, req)
}

/// Pleroma Specific. This follows an account by URI and returns the account being followed
/// - Parameter uri: account name on the instance you're on or a users URI (e.g @test@instance.test)
/// - Returns: the Account being followed
private func pleromaFollowAccountURI(by uri: String) async throws -> Relationship {
guard flavour == .pleroma else { throw TootSDKError.unsupportedFlavour(current: flavour, required: [.pleroma]) }
try requireFlavour([.pleroma])

let params = PleromaFollowByURIParams(uri: uri)

Expand All @@ -85,8 +89,8 @@ extension TootClient {
/// - Parameter id: the ID of the Account in the instance database.
/// - Returns: the relationship to the account requested, or an error if unable to retrieve
public func removeAccountFromFollowers(by id: String) async throws -> Relationship {
guard flavour != .friendica else { throw TootSDKError.unsupportedFlavour(current: flavour, required: TootSDKFlavour.allCases.filter({$0 != .friendica})) }
try requireFlavour(otherThan: [.friendica])

let req = HTTPRequestBuilder {
$0.url = getURL(["api", "v1", "accounts", id, "remove_from_followers"])
$0.method = .post
Expand Down
2 changes: 1 addition & 1 deletion Sources/TootSDK/TootClient/TootClient+Search.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public extension TootClient {
/// - Returns: Search results.
func search(params: SearchParams, _ pageInfo: PagedInfo? = nil, limit: Int? = nil, offset: Int? = nil) async throws -> Search {
if params.excludeUnreviewed != nil && flavour != .mastodon {
throw TootSDKError.unsupportedFlavour(current: flavour, required: [.mastodon])
try requireFlavour([.mastodon])
}
let req = HTTPRequestBuilder {
$0.url = getURL(["api", "v2", "search"])
Expand Down
11 changes: 11 additions & 0 deletions Sources/TootSDK/TootClient/TootClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@ extension TootClient {

return (data, httpResponse)
}

internal func requireFlavour(_ supportedFlavours: Set<TootSDKFlavour>) throws {
if !supportedFlavours.contains(flavour) {
throw TootSDKError.unsupportedFlavour(current: flavour, required: supportedFlavours)
}
}

internal func requireFlavour(otherThan unsupportedFalvours: Set<TootSDKFlavour>) throws {
let supportedFlavours = Set(TootSDKFlavour.allCases).subtracting(unsupportedFalvours)
try requireFlavour(supportedFlavours)
}
}

extension TootClient: Equatable {
Expand Down

0 comments on commit 91f0149

Please sign in to comment.