1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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>) -> [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
}
}