|
| 1 | +import Foundation |
| 2 | +import UniformTypeIdentifiers |
| 3 | + |
| 4 | +/// Represents a file attachment for Slack messages |
| 5 | +public struct SlackAttachment: Sendable { |
| 6 | + /// The type of attachment |
| 7 | + public let type: AttachmentType |
| 8 | + /// Optional title for the attachment |
| 9 | + public let title: String? |
| 10 | + /// Optional fallback text for the attachment |
| 11 | + public let fallback: String? |
| 12 | + /// Optional color for the attachment (hex color code) |
| 13 | + public let color: String? |
| 14 | + /// Optional text content for the attachment |
| 15 | + public let text: String? |
| 16 | + |
| 17 | + public init( |
| 18 | + type: AttachmentType, |
| 19 | + title: String? = nil, |
| 20 | + fallback: String? = nil, |
| 21 | + color: String? = nil, |
| 22 | + text: String? = nil |
| 23 | + ) { |
| 24 | + self.type = type |
| 25 | + self.title = title |
| 26 | + self.fallback = fallback |
| 27 | + self.color = color |
| 28 | + self.text = text |
| 29 | + } |
| 30 | + |
| 31 | + /// JSON representation of the attachment for Slack API |
| 32 | + public var json: String { |
| 33 | + var components: [String] = [] |
| 34 | + |
| 35 | + // Add title if present |
| 36 | + if let title = title { |
| 37 | + components.append("\"title\": \"\(title)\"") |
| 38 | + } |
| 39 | + |
| 40 | + // Add fallback if present |
| 41 | + if let fallback = fallback { |
| 42 | + components.append("\"fallback\": \"\(fallback)\"") |
| 43 | + } |
| 44 | + |
| 45 | + // Add color if present |
| 46 | + if let color = color { |
| 47 | + components.append("\"color\": \"\(color)\"") |
| 48 | + } |
| 49 | + |
| 50 | + // Add text if present |
| 51 | + if let text = text { |
| 52 | + components.append("\"text\": \"\(text)\"") |
| 53 | + } |
| 54 | + |
| 55 | + // Add type-specific fields |
| 56 | + switch type { |
| 57 | + case .image(let url, let altText): |
| 58 | + components.append("\"image_url\": \"\(url)\"") |
| 59 | + if let altText = altText { |
| 60 | + components.append("\"alt_text\": \"\(altText)\"") |
| 61 | + } |
| 62 | + case .file(let url, let filename, let fileType): |
| 63 | + components.append("\"file_url\": \"\(url)\"") |
| 64 | + components.append("\"filename\": \"\(filename)\"") |
| 65 | + components.append("\"filetype\": \"\(fileType.rawValue)\"") |
| 66 | + case .fileData(_, let filename, let fileType): |
| 67 | + components.append("\"filename\": \"\(filename)\"") |
| 68 | + components.append("\"filetype\": \"\(fileType.rawValue)\"") |
| 69 | + } |
| 70 | + |
| 71 | + return "{ \(components.joined(separator: ", ")) }" |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/// Types of attachments supported |
| 76 | +public enum AttachmentType: Sendable { |
| 77 | + /// Image attachment from URL |
| 78 | + case image(url: String, altText: String? = nil) |
| 79 | + /// File attachment from URL |
| 80 | + case file(url: String, filename: String, fileType: FileType) |
| 81 | + /// File attachment from local data |
| 82 | + case fileData(data: Data, filename: String, fileType: FileType) |
| 83 | +} |
| 84 | + |
| 85 | +/// Supported file types for attachments |
| 86 | +public enum FileType: String, CaseIterable, Sendable { |
| 87 | + case csv, pdf, txt, json, xml, zip, jpeg, png, gif, mp4, mov, mp3 |
| 88 | + |
| 89 | + /// MIME type for the file type |
| 90 | + public var mimeType: String? { |
| 91 | + switch self { |
| 92 | + case .csv: UTType.commaSeparatedText.preferredMIMEType |
| 93 | + case .pdf: UTType.pdf.preferredMIMEType |
| 94 | + case .txt: UTType.plainText.preferredMIMEType |
| 95 | + case .json: UTType.json.preferredMIMEType |
| 96 | + case .xml: UTType.xml.preferredMIMEType |
| 97 | + case .zip: UTType.zip.preferredMIMEType |
| 98 | + case .jpeg: UTType.jpeg.preferredMIMEType |
| 99 | + case .png: UTType.png.preferredMIMEType |
| 100 | + case .gif: UTType.gif.preferredMIMEType |
| 101 | + case .mp4: UTType.mpeg4Movie.preferredMIMEType |
| 102 | + case .mov: UTType.quickTimeMovie.preferredMIMEType |
| 103 | + case .mp3: UTType.mp3.preferredMIMEType |
| 104 | + } |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +// MARK: - Convenience Initializers |
| 109 | + |
| 110 | +public extension SlackAttachment { |
| 111 | + |
| 112 | + static func image(url: String, altText: String? = nil, title: String? = nil, fallback: String? = nil) -> SlackAttachment { |
| 113 | + SlackAttachment( |
| 114 | + type: .image(url: url, altText: altText), |
| 115 | + title: title, |
| 116 | + fallback: fallback |
| 117 | + ) |
| 118 | + } |
| 119 | + |
| 120 | + static func file(url: String, filename: String, fileType: FileType, title: String? = nil, fallback: String? = nil) -> SlackAttachment { |
| 121 | + SlackAttachment( |
| 122 | + type: .file(url: url, filename: filename, fileType: fileType), |
| 123 | + title: title, |
| 124 | + fallback: fallback |
| 125 | + ) |
| 126 | + } |
| 127 | + |
| 128 | + static func file(data: Data, filename: String, fileType: FileType, title: String? = nil, fallback: String? = nil) -> SlackAttachment { |
| 129 | + SlackAttachment( |
| 130 | + type: .fileData(data: data, filename: filename, fileType: fileType), |
| 131 | + title: title, |
| 132 | + fallback: fallback |
| 133 | + ) |
| 134 | + } |
| 135 | +} |
0 commit comments