From 014278f8552e9365b4d85ba5ab4747d97e45629a Mon Sep 17 00:00:00 2001 From: Justin Mazzocchi <2831158+jzzocc@users.noreply.github.com> Date: Sat, 27 Feb 2021 12:27:49 -0800 Subject: [PATCH] Guard against invalid emoji URLs --- Extensions/CollectionItem+Extensions.swift | 10 ++++++++-- Extensions/NSMutableAttributedString+Extensions.swift | 11 +++++++---- Mastodon/Sources/Mastodon/Entities/Emoji.swift | 4 ++-- .../ViewModels/View Models/EmojiViewModel.swift | 10 ++++++---- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/Extensions/CollectionItem+Extensions.swift b/Extensions/CollectionItem+Extensions.swift index 120dd1c..ec2d5b0 100644 --- a/Extensions/CollectionItem+Extensions.swift +++ b/Extensions/CollectionItem+Extensions.swift @@ -89,7 +89,10 @@ extension CollectionItem { private extension Account { func mediaPrefetchURLs(identityContext: IdentityContext) -> Set { - 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 { 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:))) } } diff --git a/Extensions/NSMutableAttributedString+Extensions.swift b/Extensions/NSMutableAttributedString+Extensions.swift index 2170849..9e29972 100644 --- a/Extensions/NSMutableAttributedString+Extensions.swift +++ b/Extensions/NSMutableAttributedString+Extensions.swift @@ -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 diff --git a/Mastodon/Sources/Mastodon/Entities/Emoji.swift b/Mastodon/Sources/Mastodon/Entities/Emoji.swift index e1bea17..e18da90 100644 --- a/Mastodon/Sources/Mastodon/Entities/Emoji.swift +++ b/Mastodon/Sources/Mastodon/Entities/Emoji.swift @@ -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? } diff --git a/ViewModels/Sources/ViewModels/View Models/EmojiViewModel.swift b/ViewModels/Sources/ViewModels/View Models/EmojiViewModel.swift index baacb91..60b48ee 100644 --- a/ViewModels/Sources/ViewModels/View Models/EmojiViewModel.swift +++ b/ViewModels/Sources/ViewModels/View Models/EmojiViewModel.swift @@ -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 } }