import ATProtoKit import Darwin import Foundation import OSLog import Security import UIKit extension ATProtoBlueskyService { static func imageItems(from media: [String: Any]) -> [[String: Any]] { if media["$type"] as? String == "app.bsky.embed.gallery#view" { return (media["items"] as? [[String: Any]] ?? []).filter { $0["$type"] as? String == "app.bsky.embed.gallery#viewImage" } } return media["images"] as? [[String: Any]] ?? [] } static func imageURL(from image: [String: Any]) -> URL? { let raw = image["fullsize"] as? String ?? image["fullSize"] as? String ?? image["thumb"] as? String ?? image[ "thumbnail"] as? String return raw.flatMap(URL.init(string:)) } static func imageThumbnailURLs(from embed: [String: Any]) -> [URL: URL] { let media = embed["media"] as? [String: Any] ?? embed return imageItems(from: media).reduce(into: [:]) { result, image in guard let fullSize = imageURL(from: image), let raw = image["thumb"] as? String ?? image["thumbnail"] as? String, let thumbnail = URL(string: raw) else { return } result[fullSize] = thumbnail } } struct AttachedMedia { var images: [URL] var videos: [URL] var gifs: [URL] var inlineGIFVideos: [URL] } static func attachedMedia(from embed: [String: Any]) -> AttachedMedia { let media = embed["media"] as? [String: Any] ?? embed let images = imageItems(from: media).compactMap { imageURL(from: $0) } let playlist = [media["playlist"] as? String].compactMap { $0 }.compactMap(URL.init(string:)) let videos = media["presentation"] as? String == "gif" ? [] : playlist let inlineGIFVideos = media["presentation"] as? String == "gif" ? playlist : [] let external = media["external"] as? [String: Any] let externalURL = (external?["uri"] as? String).flatMap(URL.init(string:)) let gifs = [externalURL].compactMap { $0 }.filter { ["http", "https"].contains($0.scheme?.lowercased() ?? "") && Post.destination(for: $0) == .gif } return AttachedMedia(images: images, videos: videos, gifs: gifs, inlineGIFVideos: inlineGIFVideos) } static func externalLinks(from embeds: [[String: Any]], excluding existing: Set) -> [URL] { let existingKeys = Set(existing.map(externalLinkKey)) return uniqueURLs( embeds.compactMap { embed in let media = embed["media"] as? [String: Any] ?? embed guard let external = media["external"] as? [String: Any], let raw = external["uri"] as? String, let url = URL(string: raw), ["http", "https"].contains(url.scheme?.lowercased() ?? ""), url.host != nil, Post.destination(for: url) != .gif, !existingKeys.contains(externalLinkKey(url)) else { return nil } return url }) } private static func externalLinkKey(_ url: URL) -> String { guard var components = URLComponents(url: url, resolvingAgainstBaseURL: false), ["youtube.com", "www.youtube.com", "m.youtube.com"].contains(components.host?.lowercased() ?? ""), components.path == "/watch", components.queryItems?.contains(where: { $0.name == "v" && !($0.value ?? "").isEmpty }) == true else { return url.absoluteString } // Playlist context does not change the video in a watch URL. components.host = "youtube.com" components.queryItems = components.queryItems? .filter { $0.name != "list" && $0.name != "index" } .sorted { if $0.name != $1.name { return $0.name < $1.name } return ($0.value ?? "") < ($1.value ?? "") } return components.string ?? url.absoluteString } static func externalMetadata(from embed: [String: Any]) -> (title: String?, description: String?)? { let media = embed["media"] as? [String: Any] ?? embed guard let external = media["external"] as? [String: Any] else { return nil } return (external["title"] as? String, external["description"] as? String) } static func imageDescriptions(from embed: [String: Any]) -> [URL: String] { let media = embed["media"] as? [String: Any] ?? embed var descriptions: [URL: String] = [:] for image in imageItems(from: media) { guard let url = imageURL(from: image), let alt = image["alt"] as? String, !alt.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { continue } descriptions[url] = alt } if let external = media["external"] as? [String: Any], let rawURL = external["uri"] as? String, let url = URL(string: rawURL), Post.destination(for: url) == .gif, let description = external["description"] as? String, !description.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { descriptions[url] = description } if let rawURL = media["playlist"] as? String, let url = URL(string: rawURL), let alt = media["alt"] as? String, !alt.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { descriptions[url] = alt } return descriptions } }