From 945331944f6a821e1712105041c0187211b373b8 Mon Sep 17 00:00:00 2001 From: CMK Date: Thu, 4 Feb 2021 15:28:25 +0800 Subject: [PATCH 1/2] fix: poll option entity decode issue --- .../Sources/MastodonSDK/Entity/Mastodon+Entity+Poll.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Poll.swift b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Poll.swift index a9ed197fc..5a672e014 100644 --- a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Poll.swift +++ b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Poll.swift @@ -13,7 +13,7 @@ extension Mastodon.Entity { /// - Since: 2.8.0 /// - Version: 3.3.0 /// # Last Update - /// 2021/1/28 + /// 2021/2/4 /// # Reference /// [Document](https://docs.joinmastodon.org/entities/poll/) public struct Poll: Codable { @@ -51,7 +51,7 @@ extension Mastodon.Entity.Poll { public let title: String /// nil if results are not published yet public let votesCount: Int? - public let emojis: [Mastodon.Entity.Emoji] + public let emojis: [Mastodon.Entity.Emoji]? enum CodingKeys: String, CodingKey { case title From 45301fb72cd77e46ed2e9499d5a2d1670182fc60 Mon Sep 17 00:00:00 2001 From: CMK Date: Thu, 4 Feb 2021 16:29:03 +0800 Subject: [PATCH 2/2] fix: update ActiveLabel to v4.0.0 to fix url active entity may raise crash issue --- Mastodon.xcodeproj/project.pbxproj | 2 +- .../xcschemes/xcschememanagement.plist | 2 +- .../xcshareddata/swiftpm/Package.resolved | 4 ++-- Mastodon/Extension/MastodonContent.swift | 22 +++++++++++++++---- 4 files changed, 22 insertions(+), 8 deletions(-) diff --git a/Mastodon.xcodeproj/project.pbxproj b/Mastodon.xcodeproj/project.pbxproj index 11403cdc8..0c8d53cda 100644 --- a/Mastodon.xcodeproj/project.pbxproj +++ b/Mastodon.xcodeproj/project.pbxproj @@ -1634,7 +1634,7 @@ repositoryURL = "https://github.com/TwidereProject/ActiveLabel.swift"; requirement = { kind = upToNextMajorVersion; - minimumVersion = 3.0.0; + minimumVersion = 4.0.0; }; }; 2D61336725C18A4F00CAE157 /* XCRemoteSwiftPackageReference "AlamofireNetworkActivityIndicator" */ = { diff --git a/Mastodon.xcodeproj/xcuserdata/mainasuk.xcuserdatad/xcschemes/xcschememanagement.plist b/Mastodon.xcodeproj/xcuserdata/mainasuk.xcuserdatad/xcschemes/xcschememanagement.plist index 6f7ab5892..24ba6665b 100644 --- a/Mastodon.xcodeproj/xcuserdata/mainasuk.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/Mastodon.xcodeproj/xcuserdata/mainasuk.xcuserdatad/xcschemes/xcschememanagement.plist @@ -7,7 +7,7 @@ CoreDataStack.xcscheme_^#shared#^_ orderHint - 8 + 7 Mastodon.xcscheme_^#shared#^_ diff --git a/Mastodon.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Mastodon.xcworkspace/xcshareddata/swiftpm/Package.resolved index c1cd92950..f783b7981 100644 --- a/Mastodon.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Mastodon.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -6,8 +6,8 @@ "repositoryURL": "https://github.com/TwidereProject/ActiveLabel.swift", "state": { "branch": null, - "revision": "3d8115c992c44358eabbb21ffc4616f4d56028b1", - "version": "3.0.0" + "revision": "d6cf96e0ca4f2269021bcf8f11381ab57897f84a", + "version": "4.0.0" } }, { diff --git a/Mastodon/Extension/MastodonContent.swift b/Mastodon/Extension/MastodonContent.swift index 3e6b072d6..b1b1a635f 100755 --- a/Mastodon/Extension/MastodonContent.swift +++ b/Mastodon/Extension/MastodonContent.swift @@ -24,7 +24,8 @@ enum TootContent { switch entity.type { case .url: guard let href = entity.href else { continue } - activeEntities.append(ActiveEntity(range: range, type: .url(original: href, trimmed: entity.hrefEllipsis ?? href))) + let text = String(entity.text) + activeEntities.append(ActiveEntity(range: range, type: .url(text, trimmed: entity.hrefEllipsis ?? text, url: href))) case .hashtag: var userInfo: [AnyHashable: Any] = [:] entity.href.flatMap { href in @@ -54,17 +55,17 @@ enum TootContent { document: toot, original: text, trimmed: trimmed, - activeEntities: activeEntities + activeEntities: validate(text: trimmed, activeEntities: activeEntities) ? activeEntities : [] ) } static func trimEntity(toot: inout String, activeEntity: ActiveEntity, activeEntities: [ActiveEntity]) { - guard case let .url(original, trimmed, _) = activeEntity.type else { return } + guard case let .url(text, trimmed, _, _) = activeEntity.type else { return } guard let index = activeEntities.firstIndex(where: { $0.range == activeEntity.range }) else { return } guard let range = Range(activeEntity.range, in: toot) else { return } toot.replaceSubrange(range, with: trimmed) - let offset = trimmed.count - original.count + let offset = trimmed.count - text.count activeEntity.range.length += offset let moveActiveEntities = Array(activeEntities[index...].dropFirst()) @@ -72,6 +73,19 @@ enum TootContent { moveActiveEntity.range.location += offset } } + + private static func validate(text: String, activeEntities: [ActiveEntity]) -> Bool { + for activeEntity in activeEntities { + let count = text.utf16.count + let endIndex = activeEntity.range.location + activeEntity.range.length + guard endIndex <= count else { + assertionFailure("Please file issue") + return false + } + } + + return true + } }