TeraAddCardIntents.swift (2676B)
1 import Foundation 2 3 /// Existing native card-to-intent translation. Rust retains authorization, 4 /// validation, immutable operation identity and all publication policy. 5 enum TeraAddCardIntents { 6 struct RevisionEditing { 7 let target: TeraRevisionTarget 8 let form: TeraAddForm 9 } 10 11 struct Retraction { 12 let id: String 13 let input: TeraRetractionDraftInput 14 } 15 16 static func revision(_ card: TeraTodayCard, author: String?, client: TeraRuntimeClient) async throws -> RevisionEditing { 17 guard let author, author == card.authorPublicKey else { 18 throw TeraRuntimeFailure.local(operation: "add.revise", code: "ios.add.revision_not_authorized", 19 safeMessage: "Only your own post can be revised.") 20 } 21 guard let id = card.localSourceDraftID ?? card.localOperationID else { 22 throw TeraRuntimeFailure.local(operation: "add.revise", code: "ios.add.revision_source_unavailable", 23 safeMessage: "This post cannot be revised losslessly on this device.") 24 } 25 let form = try await client.revisionSourceForm(card: card, sourceDraftID: id) 26 return RevisionEditing(target: TeraRevisionTarget(cardID: card.id, sourceEventID: card.sourceEventID, 27 sourceAddress: card.sourceAddress, authorPublicKey: card.authorPublicKey), form: form) 28 } 29 30 static func retraction(_ card: TeraTodayCard, author: String?, identifier: () -> String) throws -> Retraction { 31 guard let author, author == card.authorPublicKey else { 32 throw TeraRuntimeFailure.local(operation: "add.retract", code: "ios.add.retraction_not_authorized", 33 safeMessage: "Only your own post can be retracted.") 34 } 35 guard let kind = card.retractionTargetKind else { 36 throw TeraRuntimeFailure.local(operation: "add.retract", code: "ios.add.retraction_target_invalid", 37 safeMessage: "This post cannot be retracted safely.") 38 } 39 let id = identifier() 40 guard TeraAddPresentation.isValidIdentifier(id) else { 41 throw TeraRuntimeFailure.local(operation: "add.retract", code: "ios.add.identifier_invalid", 42 safeMessage: "The local operation identifier is invalid.") 43 } 44 return Retraction(id: id, input: TeraRetractionDraftInput(commandType: card.type.addCommandType, 45 targetCardID: card.id, targetEventID: card.sourceEventID, 46 targetKind: kind, targetAddress: card.sourceAddress, reason: "Removed by author.")) 47 } 48 }