Skip to content

Commit

Permalink
Progress toward adding new lexicons
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterJ93 committed Oct 20, 2024
1 parent e67d5fe commit 2610ef7
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// AddValuesAsAdmin.swift
//
// Created by Christopher Jr Riley on 2024-10-19.
//

import Foundation

extension ATProtoAdmin {

/// Adds values to a specific set as an administrator.
///
/// - Note: According to the AT Protocol specifications: "Add values to a specific set.
/// Attempting to add values to a set that does not exist will result in an error."
///
/// - SeeAlso: This is based on the [`tools.ozone.set.addValues`][github] lexicon.
///
/// [github]: https://github.com/bluesky-social/atproto/blob/main/lexicons/tools/ozone/set/addValues.json
///
public func addValues(setName: String, values: [String]) async throws {
guard session != nil,
let accessToken = session?.accessToken else {
throw ATRequestPrepareError.missingActiveSession
}

guard let sessionURL = session?.pdsURL,
let requestURL = URL(string: "\(sessionURL)/xrpc/tools.ozone.set.addValues") else {
throw ATRequestPrepareError.invalidRequestURL
}

let requestBody = ToolsOzoneLexicon.Set.AddValuesRequestBody(
name: setName,
values: values
)

do {
let request = APIClientService.createRequest(
forRequest: requestURL,
andMethod: .post,
acceptValue: "application/json",
contentTypeValue: "application/json",
authorizationValue: "Bearer \(accessToken)"
)

try await APIClientService.shared.sendRequest(
request,
withEncodingBody: requestBody
)
} catch {
throw error
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// GetRepositoriesAsAdmin.swift.swift
//
// Created by Christopher Jr Riley on 2024-10-19.
//

import Foundation

extension ATProtoAdmin {

/// Gets an array of repositories as a moderator.
///
/// - Note: According to the AT Protocol specifications: "Get details about some repositories."
///
/// - SeeAlso: This is based on the [`tools.ozone.moderation.getRepos`][github] lexicon.
///
/// [github]: https://github.com/bluesky-social/atproto/blob/main/lexicons/tools/ozone/moderation/getRepos.json
///
/// - Parameter dids: An array of Decentralized Identifiers (DIDs) to extract the
/// repository information. Limits to 100 items.
/// - Returns: An aray of repositories based on the decentralized identifiers (DIDs) given.
///
/// - Throws: An ``ATProtoError``-conforming error type, depending on the issue. Go to
/// ``ATAPIError`` and ``ATRequestPrepareError`` for more details.
public func getRepositories(from dids: [String]) async throws -> ToolsOzoneLexicon.Moderation.GetRepositoriesOutput {
guard session != nil,
let accessToken = session?.accessToken else {
throw ATRequestPrepareError.missingActiveSession
}

guard let sessionURL = session?.pdsURL,
let requestURL = URL(string: "\(sessionURL)/xrpc/tools.ozone.moderation.getRepos") else {
throw ATRequestPrepareError.invalidRequestURL
}

var queryItems = [(String, String)]()

let cappedDIDArray = dids.prefix(25)
queryItems += cappedDIDArray.map { ("dids", $0) }

let queryURL: URL

do {
queryURL = try APIClientService.setQueryItems(
for: requestURL,
with: queryItems
)

let request = APIClientService.createRequest(
forRequest: queryURL,
andMethod: .get,
acceptValue: "application/json",
contentTypeValue: nil,
authorizationValue: "Bearer \(accessToken)"
)
let response = try await APIClientService.shared.sendRequest(
request,
decodeTo: ToolsOzoneLexicon.Moderation.GetRepositoriesOutput.self
)

return response
} catch {
throw error
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// ToolsOzoneModerationGetRepos.swift
//
// Created by Christopher Jr Riley on 2024-10-19.
//

import Foundation
import ATMacro

extension ToolsOzoneLexicon.Moderation {

/// An output model for getting an array of repositories as a moderator.
///
/// - Note: According to the AT Protocol specifications: "Get details about some repositories."
///
/// - SeeAlso: This is based on the [`tools.ozone.moderation.getRepos`][github] lexicon.
///
/// [github]: https://github.com/bluesky-social/atproto/blob/main/lexicons/tools/ozone/moderation/getRepos.json
public struct GetRepositoriesOutput: Codable {

/// An array of repositories.
public let repositories: [String]

enum CodingKeys: String, CodingKey {
case repositories = "repos"
}
}
}

extension ATUnion {
#ATUnionBuilder(named: "ModerationGetRepositoriesOutputUnion", containing: [
"repoViewDetail" : "ToolsOzoneLexicon.Moderation.RepositoryViewDetailDefinition",
"repoViewNotFound" : "ToolsOzoneLexicon.Moderation.RepositoryViewNotFoundDefinition"
])
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// ToolsOzoneModerationSetAddValues.swift
//
// Created by Christopher Jr Riley on 2024-10-19.
//

import Foundation

extension ToolsOzoneLexicon.Set {

/// A request body model for adding values to a specific set as an administrator.
///
/// - Note: According to the AT Protocol specifications: "Add values to a specific set.
/// Attempting to add values to a set that does not exist will result in an error."
///
/// - SeeAlso: This is based on the [`tools.ozone.set.addValues`][github] lexicon.
///
/// [github]: https://github.com/bluesky-social/atproto/blob/main/lexicons/tools/ozone/set/addValues.json
public struct AddValuesRequestBody: Codable {

/// The name of the set.
public let name: String

/// An array of values. Limited to 100 items.
public let values: [String]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// ToolsOzoneSetDefs.swift
//
// Created by Christopher Jr Riley on 2024-10-19.
//

import Foundation

extension ToolsOzoneLexicon.Set {

/// A definition model for a set.
///
/// - SeeAlso: This is based on the [`tools.ozone.set.defs`][github] lexicon.
///
/// [github]: https://github.com/bluesky-social/atproto/blob/main/lexicons/tools/ozone/set/defs.json
public struct SetDefinition: Codable {

/// The name of the set.
///
/// A minimum of 3 characters and maximum of 128 characters is required.
public let name: String

/// The description of the set.
///
/// A maximum of 1,024 characters can be made.
public let description: String?
}

/// A definition model for a set view.
///
/// - SeeAlso: This is based on the [`tools.ozone.set.defs`][github] lexicon.
///
/// [github]: https://github.com/bluesky-social/atproto/blob/main/lexicons/tools/ozone/set/defs.json
public struct SetViewDefinition: Codable {

/// The name of the set.
///
/// A minimum of 3 characters and maximum of 128 characters is required.
public let name: String

/// The description of the set.
///
/// A maximum of 1,024 characters can be made.
public let description: String?

/// The size of the set.
public let setSize: Int

/// The date and time the set was created.
@DateFormatting public var createdAt: Date

/// The date and time the set was updated.
@DateFormatting public var updatedAt: Date

public func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(self.name, forKey: .name)
try container.encodeIfPresent(self.description, forKey: .description)
try container.encode(self.setSize, forKey: .setSize)
try container.encode(self.createdAt, forKey: .createdAt)
try container.encode(self.updatedAt, forKey: .updatedAt)
}
}
}

0 comments on commit 2610ef7

Please sign in to comment.