-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from andylin2004/main
Adding functions related to clients reposting a Bluesky post
- Loading branch information
Showing
4 changed files
with
153 additions
and
82 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
Sources/ATProtoKit/APIReference/ATProtoBlueskyAPI/HelperFunctions/DeleteActionRecord.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// | ||
// DeleteActionRecord.swift | ||
// ATProtoKit | ||
// | ||
// Created by Andy Lin on 11/27/24. | ||
// | ||
|
||
import Foundation | ||
|
||
extension ATProtoBluesky { | ||
|
||
/// Deletes a record. | ||
/// | ||
/// This can also be used to validate if a record has been deleted. | ||
/// - Parameter record: The record that needs to be deleted. | ||
/// | ||
/// This can be either the URI of the record, or the full record object itself. | ||
internal func deleteActionRecord(_ record: RecordIdentifier) async throws { | ||
guard let session else { | ||
throw ATRequestPrepareError.missingActiveSession | ||
} | ||
|
||
guard let sessionURL = session.pdsURL else { | ||
throw ATRequestPrepareError.invalidRequestURL | ||
} | ||
|
||
var actionRecord: ATProtoTools.RecordQuery? = nil | ||
try await resolveRecordIdentifierToQuery(record, sessionURL, &actionRecord) | ||
|
||
let requestBody = actionRecord | ||
|
||
guard let repositoryDID = requestBody?.repository, | ||
let actionCollection = requestBody?.collection, | ||
let actionRecordKey = requestBody?.recordKey else { | ||
throw ATRequestPrepareError.invalidRecord | ||
} | ||
|
||
try await atProtoKitInstance.deleteRecord( | ||
repositoryDID: repositoryDID, | ||
collection: actionCollection, | ||
recordKey: actionRecordKey, | ||
swapRecord: requestBody?.recordCID | ||
) | ||
} | ||
|
||
fileprivate func resolveRecordIdentifierToQuery(_ record: RecordIdentifier, _ sessionURL: String, | ||
_ actionRecord: inout ATProtoTools.RecordQuery?) async throws { | ||
switch record { | ||
case .recordQuery(let recordQuery): | ||
do { | ||
// Perform the fetch and validation based on recordQuery. | ||
let output = try await atProtoKitInstance.getRepositoryRecord(from: recordQuery.repository, | ||
collection: recordQuery.collection, | ||
recordKey: recordQuery.recordKey, | ||
recordCID: recordQuery.recordCID, | ||
pdsURL: sessionURL | ||
) | ||
|
||
let recordURI = "at://\(recordQuery.repository)/\(recordQuery.collection)/\(recordQuery.recordKey)" | ||
|
||
guard output.recordURI == recordURI else { | ||
throw ATRequestPrepareError.invalidRecord | ||
} | ||
} catch { | ||
throw error | ||
} | ||
|
||
case .recordURI(let recordURI): | ||
do { | ||
// Perform the fetch and validation based on the parsed URI. | ||
let parsedURI = try ATProtoTools().parseURI(recordURI) | ||
let output = try await atProtoKitInstance.getRepositoryRecord(from: parsedURI.repository, | ||
collection: parsedURI.collection, | ||
recordKey: parsedURI.recordKey, | ||
recordCID: parsedURI.recordCID, | ||
pdsURL: sessionURL | ||
) | ||
|
||
guard recordURI == output.recordURI else { | ||
throw ATRequestPrepareError.invalidRecord | ||
} | ||
|
||
actionRecord = parsedURI | ||
} catch { | ||
throw error | ||
} | ||
} | ||
} | ||
|
||
/// Identifies the record based on the specific information provided. | ||
/// | ||
/// `RecordIdentifier` provides a unified interface for specifying how the record is defined. | ||
/// This allows methods like ``deleteActionRecord(_:)`` to handle the backend of how to grab the | ||
/// details of the record so it can delete it. | ||
public enum RecordIdentifier { | ||
/// The record object itself. | ||
/// - Parameter recordQuery: the record object. | ||
case recordQuery(recordQuery: ATProtoTools.RecordQuery) | ||
/// The URI of the record. | ||
case recordURI(atURI: String) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters