From 904ff4eecf88987f2adc8aed872e97097b025a1b Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 11 Nov 2020 12:45:13 -0500 Subject: [PATCH] Fix crash when decoding emojis with spaces in URLs --- Pachyderm/Model/Emoji.swift | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Pachyderm/Model/Emoji.swift b/Pachyderm/Model/Emoji.swift index a0a6d50f..711ce2be 100644 --- a/Pachyderm/Model/Emoji.swift +++ b/Pachyderm/Model/Emoji.swift @@ -14,6 +14,25 @@ public class Emoji: Codable { public let staticURL: URL public let visibleInPicker: Bool + public required init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + + self.shortcode = try container.decode(String.self, forKey: .shortcode) + if let url = try? container.decode(URL.self, forKey: .url) { + self.url = url + } else { + let str = try container.decode(String.self, forKey: .url) + self.url = URL(string: str.replacingOccurrences(of: " ", with: "%20"))! + } + if let url = try? container.decode(URL.self, forKey: .staticURL) { + self.staticURL = url + } else { + let staticStr = try container.decode(String.self, forKey: .staticURL) + self.staticURL = URL(string: staticStr.replacingOccurrences(of: " ", with: "%20"))! + } + self.visibleInPicker = try container.decode(Bool.self, forKey: .visibleInPicker) + } + private enum CodingKeys: String, CodingKey { case shortcode case url