Guard against invalid emoji URLs

This commit is contained in:
Justin Mazzocchi 2021-02-27 12:27:49 -08:00
parent 9f350d1305
commit 014278f855
No known key found for this signature in database
GPG Key ID: E223E6937AAFB01C
4 changed files with 23 additions and 12 deletions

View File

@ -89,7 +89,10 @@ extension CollectionItem {
private extension Account {
func mediaPrefetchURLs(identityContext: IdentityContext) -> Set<URL> {
var urls = Set(emojis.map(\.url))
var urls = Set(emojis.compactMap {
identityContext.appPreferences.animateCustomEmojis ? $0.url : $0.staticUrl
}
.compactMap(URL.init(string:)))
if !identityContext.appPreferences.shouldReduceMotion
&& identityContext.appPreferences.animateAvatars == .everywhere {
@ -106,6 +109,9 @@ private extension Status {
func mediaPrefetchURLs(identityContext: IdentityContext) -> Set<URL> {
displayStatus.account.mediaPrefetchURLs(identityContext: identityContext)
.union(displayStatus.mediaAttachments.compactMap(\.previewUrl))
.union(displayStatus.emojis.map(\.url))
.union(displayStatus.emojis.compactMap {
identityContext.appPreferences.animateCustomEmojis ? $0.url : $0.staticUrl
}
.compactMap(URL.init(string:)))
}
}

View File

@ -12,13 +12,16 @@ extension NSMutableAttributedString {
while let tokenRange = string.range(of: token) {
let attachment = AnimatedTextAttachment()
let imageURL: URL
let imageURL: URL?
if !identityContext.appPreferences.shouldReduceMotion,
identityContext.appPreferences.animateCustomEmojis {
imageURL = emoji.url
identityContext.appPreferences.animateCustomEmojis,
let urlString = emoji.url {
imageURL = URL(string: urlString)
} else if let staticURLString = emoji.staticUrl {
imageURL = URL(string: staticURLString)
} else {
imageURL = emoji.staticUrl
imageURL = nil
}
attachment.imageView.sd_setImage(with: imageURL) { image, _, _, _ in

View File

@ -4,8 +4,8 @@ import Foundation
public struct Emoji: Codable, Hashable {
public let shortcode: String
public let staticUrl: URL
public let url: URL
public let staticUrl: String?
public let url: String?
public let visibleInPicker: Bool
public let category: String?
}

View File

@ -21,10 +21,12 @@ public extension EmojiViewModel {
var url: URL? {
guard case let .custom(emoji, _) = emoji else { return nil }
if identityContext.appPreferences.animateCustomEmojis {
return emoji.url
} else {
return emoji.staticUrl
if identityContext.appPreferences.animateCustomEmojis, let urlString = emoji.url {
return URL(string: urlString)
} else if let staticURLString = emoji.staticUrl {
return URL(string: staticURLString)
}
return nil
}
}