From 2610ef7a8b3f7226b9abb5dec8f223d9d72b0af3 Mon Sep 17 00:00:00 2001 From: Christopher Jr Riley Date: Sun, 20 Oct 2024 02:42:03 -0400 Subject: [PATCH] Progress toward adding new lexicons --- .../AddValuesAsAdmin.swift | 53 +++++++++++++++ .../GetRepositoriesAsAdmin.swift | 66 +++++++++++++++++++ .../ToolsOzoneModerationGetRepos.swift | 35 ++++++++++ .../ToolsOzoneModerationSetAddValues.swift | 27 ++++++++ .../tools.ozone/Set/ToolsOzoneSetDefs.swift | 65 ++++++++++++++++++ 5 files changed, 246 insertions(+) create mode 100644 Sources/ATProtoKit/APIReference/AdminAndModeratorAPI/AddValuesAsAdmin.swift create mode 100644 Sources/ATProtoKit/APIReference/AdminAndModeratorAPI/GetRepositoriesAsAdmin.swift create mode 100644 Sources/ATProtoKit/Models/Lexicons/tools.ozone/Moderation/ToolsOzoneModerationGetRepos.swift create mode 100644 Sources/ATProtoKit/Models/Lexicons/tools.ozone/Set/ToolsOzoneModerationSetAddValues.swift create mode 100644 Sources/ATProtoKit/Models/Lexicons/tools.ozone/Set/ToolsOzoneSetDefs.swift diff --git a/Sources/ATProtoKit/APIReference/AdminAndModeratorAPI/AddValuesAsAdmin.swift b/Sources/ATProtoKit/APIReference/AdminAndModeratorAPI/AddValuesAsAdmin.swift new file mode 100644 index 0000000000..7c018d3d49 --- /dev/null +++ b/Sources/ATProtoKit/APIReference/AdminAndModeratorAPI/AddValuesAsAdmin.swift @@ -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 + } + } +} diff --git a/Sources/ATProtoKit/APIReference/AdminAndModeratorAPI/GetRepositoriesAsAdmin.swift b/Sources/ATProtoKit/APIReference/AdminAndModeratorAPI/GetRepositoriesAsAdmin.swift new file mode 100644 index 0000000000..ebb672d209 --- /dev/null +++ b/Sources/ATProtoKit/APIReference/AdminAndModeratorAPI/GetRepositoriesAsAdmin.swift @@ -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 + } + } +} diff --git a/Sources/ATProtoKit/Models/Lexicons/tools.ozone/Moderation/ToolsOzoneModerationGetRepos.swift b/Sources/ATProtoKit/Models/Lexicons/tools.ozone/Moderation/ToolsOzoneModerationGetRepos.swift new file mode 100644 index 0000000000..c8ab7159da --- /dev/null +++ b/Sources/ATProtoKit/Models/Lexicons/tools.ozone/Moderation/ToolsOzoneModerationGetRepos.swift @@ -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" + ]) +} diff --git a/Sources/ATProtoKit/Models/Lexicons/tools.ozone/Set/ToolsOzoneModerationSetAddValues.swift b/Sources/ATProtoKit/Models/Lexicons/tools.ozone/Set/ToolsOzoneModerationSetAddValues.swift new file mode 100644 index 0000000000..30a89df45f --- /dev/null +++ b/Sources/ATProtoKit/Models/Lexicons/tools.ozone/Set/ToolsOzoneModerationSetAddValues.swift @@ -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] + } +} diff --git a/Sources/ATProtoKit/Models/Lexicons/tools.ozone/Set/ToolsOzoneSetDefs.swift b/Sources/ATProtoKit/Models/Lexicons/tools.ozone/Set/ToolsOzoneSetDefs.swift new file mode 100644 index 0000000000..09f3738889 --- /dev/null +++ b/Sources/ATProtoKit/Models/Lexicons/tools.ozone/Set/ToolsOzoneSetDefs.swift @@ -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) + } + } +}