From 56c8dad895bce7d545769d74bbb0921552c3f66a Mon Sep 17 00:00:00 2001 From: Maurice Parker Date: Mon, 20 Apr 2020 10:21:29 -0500 Subject: [PATCH] Improve tweet formatting when they contain emoji with multiple scalars --- .../Twitter/TwitterFeedProvider.swift | 8 ++-- .../FeedProvider/Twitter/TwitterStatus.swift | 48 ++++++++++++------- submodules/RSCore | 2 +- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/Frameworks/Account/FeedProvider/Twitter/TwitterFeedProvider.swift b/Frameworks/Account/FeedProvider/Twitter/TwitterFeedProvider.swift index 4bcfac8fe..10892371f 100644 --- a/Frameworks/Account/FeedProvider/Twitter/TwitterFeedProvider.swift +++ b/Frameworks/Account/FeedProvider/Twitter/TwitterFeedProvider.swift @@ -254,10 +254,10 @@ private extension TwitterFeedProvider { dateFormatter.dateFormat = Self.dateFormat decoder.dateDecodingStrategy = .formatted(dateFormatter) -// let jsonString = String(data: response.data, encoding: .utf8) -// let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("twitter.json") -// print("******** writing to: \(url.path)") -// try? jsonString?.write(toFile: url.path, atomically: true, encoding: .utf8) + let jsonString = String(data: response.data, encoding: .utf8) + let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!.appendingPathComponent("twitter.json") + print("******** writing to: \(url.path)") + try? jsonString?.write(toFile: url.path, atomically: true, encoding: .utf8) do { let tweets: [TwitterStatus] diff --git a/Frameworks/Account/FeedProvider/Twitter/TwitterStatus.swift b/Frameworks/Account/FeedProvider/Twitter/TwitterStatus.swift index 30d532c19..713537b7e 100644 --- a/Frameworks/Account/FeedProvider/Twitter/TwitterStatus.swift +++ b/Frameworks/Account/FeedProvider/Twitter/TwitterStatus.swift @@ -71,33 +71,47 @@ private extension TwitterStatus { } var displayHTML: String? { - if let text = fullText, let displayRange = displayTextRange, displayRange.count > 1, - let displayStartIndex = text.index(text.startIndex, offsetBy: displayRange[0], limitedBy: text.endIndex), - let displayEndIndex = text.index(text.startIndex, offsetBy: displayRange[1], limitedBy: text.endIndex), - let entities = entities?.combineAndSort() { + if let text = fullText, let displayRange = displayTextRange, displayRange.count > 1, let entities = entities?.combineAndSort() { + let displayStartIndex = text.index(text.startIndex, offsetBy: displayRange[0], limitedBy: text.endIndex) ?? text.startIndex + let displayEndIndex = text.index(text.startIndex, offsetBy: displayRange[1], limitedBy: text.endIndex) ?? text.endIndex + var html = String() var prevIndex = displayStartIndex for entity in entities { - if let entityStartIndex = text.index(text.startIndex, offsetBy: entity.startIndex, limitedBy: text.endIndex), - let entityEndIndex = text.index(text.startIndex, offsetBy: entity.endIndex, limitedBy: text.endIndex) { - - if prevIndex < entityStartIndex { - html += String(text[prevIndex..") + } + + // We drop off any URL which is just pointing to the quoted status. It is redundant. + if let twitterURL = entity as? TwitterURL, let expandedURL = twitterURL.expandedURL, let quotedURL = quotedStatus?.url { + if expandedURL.caseInsensitiveCompare(quotedURL) != .orderedSame { html += entity.renderAsHTML() } - - prevIndex = entityEndIndex + } else { + html += entity.renderAsHTML() } + + prevIndex = entityEndIndex + } if prevIndex < displayEndIndex { diff --git a/submodules/RSCore b/submodules/RSCore index a742db73c..3dfa570a4 160000 --- a/submodules/RSCore +++ b/submodules/RSCore @@ -1 +1 @@ -Subproject commit a742db73c4f4007f0d7097746c88ce2074400045 +Subproject commit 3dfa570a4600690290cd946b8e122b0b99da0a13