Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverting RTE to use SDK markdown when in markdown mode #2327

Merged
merged 3 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ElementX/Sources/Application/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,9 @@ class AppCoordinator: AppCoordinatorProtocol, AuthenticationCoordinatorDelegate,
return
}
let roomProxy = await userSession.clientProxy.roomForIdentifier(roomID)
let messageContent = messageEventContentFromMarkdown(md: replyText)
switch await roomProxy?.timeline.sendMessageEventContent(messageContent) {
switch await roomProxy?.timeline.sendMessage(replyText,
html: nil,
intentionalMentions: .empty) {
case .success:
break
default:
Expand Down
16 changes: 8 additions & 8 deletions ElementX/Sources/Mocks/Generated/GeneratedMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2487,12 +2487,12 @@ class TimelineProxyMock: TimelineProxyProtocol {
var editMessageHtmlOriginalIntentionalMentionsCalled: Bool {
return editMessageHtmlOriginalIntentionalMentionsCallsCount > 0
}
var editMessageHtmlOriginalIntentionalMentionsReceivedArguments: (message: String, html: String, eventID: String, intentionalMentions: IntentionalMentions)?
var editMessageHtmlOriginalIntentionalMentionsReceivedInvocations: [(message: String, html: String, eventID: String, intentionalMentions: IntentionalMentions)] = []
var editMessageHtmlOriginalIntentionalMentionsReceivedArguments: (message: String, html: String?, eventID: String, intentionalMentions: IntentionalMentions)?
var editMessageHtmlOriginalIntentionalMentionsReceivedInvocations: [(message: String, html: String?, eventID: String, intentionalMentions: IntentionalMentions)] = []
var editMessageHtmlOriginalIntentionalMentionsReturnValue: Result<Void, TimelineProxyError>!
var editMessageHtmlOriginalIntentionalMentionsClosure: ((String, String, String, IntentionalMentions) async -> Result<Void, TimelineProxyError>)?
var editMessageHtmlOriginalIntentionalMentionsClosure: ((String, String?, String, IntentionalMentions) async -> Result<Void, TimelineProxyError>)?

func editMessage(_ message: String, html: String, original eventID: String, intentionalMentions: IntentionalMentions) async -> Result<Void, TimelineProxyError> {
func editMessage(_ message: String, html: String?, original eventID: String, intentionalMentions: IntentionalMentions) async -> Result<Void, TimelineProxyError> {
editMessageHtmlOriginalIntentionalMentionsCallsCount += 1
editMessageHtmlOriginalIntentionalMentionsReceivedArguments = (message: message, html: html, eventID: eventID, intentionalMentions: intentionalMentions)
editMessageHtmlOriginalIntentionalMentionsReceivedInvocations.append((message: message, html: html, eventID: eventID, intentionalMentions: intentionalMentions))
Expand Down Expand Up @@ -2767,12 +2767,12 @@ class TimelineProxyMock: TimelineProxyProtocol {
var sendMessageHtmlInReplyToIntentionalMentionsCalled: Bool {
return sendMessageHtmlInReplyToIntentionalMentionsCallsCount > 0
}
var sendMessageHtmlInReplyToIntentionalMentionsReceivedArguments: (message: String, html: String, eventID: String?, intentionalMentions: IntentionalMentions)?
var sendMessageHtmlInReplyToIntentionalMentionsReceivedInvocations: [(message: String, html: String, eventID: String?, intentionalMentions: IntentionalMentions)] = []
var sendMessageHtmlInReplyToIntentionalMentionsReceivedArguments: (message: String, html: String?, eventID: String?, intentionalMentions: IntentionalMentions)?
var sendMessageHtmlInReplyToIntentionalMentionsReceivedInvocations: [(message: String, html: String?, eventID: String?, intentionalMentions: IntentionalMentions)] = []
var sendMessageHtmlInReplyToIntentionalMentionsReturnValue: Result<Void, TimelineProxyError>!
var sendMessageHtmlInReplyToIntentionalMentionsClosure: ((String, String, String?, IntentionalMentions) async -> Result<Void, TimelineProxyError>)?
var sendMessageHtmlInReplyToIntentionalMentionsClosure: ((String, String?, String?, IntentionalMentions) async -> Result<Void, TimelineProxyError>)?

func sendMessage(_ message: String, html: String, inReplyTo eventID: String?, intentionalMentions: IntentionalMentions) async -> Result<Void, TimelineProxyError> {
func sendMessage(_ message: String, html: String?, inReplyTo eventID: String?, intentionalMentions: IntentionalMentions) async -> Result<Void, TimelineProxyError> {
sendMessageHtmlInReplyToIntentionalMentionsCallsCount += 1
sendMessageHtmlInReplyToIntentionalMentionsReceivedArguments = (message: message, html: html, eventID: eventID, intentionalMentions: intentionalMentions)
sendMessageHtmlInReplyToIntentionalMentionsReceivedInvocations.append((message: message, html: html, eventID: eventID, intentionalMentions: intentionalMentions))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ enum ComposerToolbarVoiceMessageAction {
}

enum ComposerToolbarViewModelAction {
case sendMessage(plain: String, html: String, mode: RoomScreenComposerMode, intentionalMentions: IntentionalMentions)
case sendMessage(plain: String, html: String?, mode: RoomScreenComposerMode, intentionalMentions: IntentionalMentions)
case attach(ComposerAttachmentType)

case handlePasteOrDrop(provider: NSItemProvider)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,15 @@ final class ComposerToolbarViewModel: ComposerToolbarViewModelType, ComposerTool
}
.store(in: &cancellables)

appSettings.$richTextEditorEnabled
.map { !$0 }
.assign(to: &wysiwygViewModel.$plainTextMode)

completionSuggestionService.suggestionsPublisher
.combineLatest(appSettings.$richTextEditorEnabled)
.map { suggestions, richTextEditorEnabled in
// We ignore user suggestions when RTE is disabled since mentions would not work
guard richTextEditorEnabled else {
return []
}
return suggestions
}
.weakAssign(to: \.state.suggestions, on: self)
.store(in: &cancellables)

Expand All @@ -111,8 +115,9 @@ final class ComposerToolbarViewModel: ComposerToolbarViewModelType, ComposerTool
case .previewVoiceMessage:
actionsSubject.send(.voiceMessage(.send))
default:
let sendHTML = appSettings.richTextEditorEnabled
actionsSubject.send(.sendMessage(plain: wysiwygViewModel.content.markdown,
html: wysiwygViewModel.content.html,
html: sendHTML ? wysiwygViewModel.content.html : nil,
mode: state.composerMode,
intentionalMentions: wysiwygViewModel
.getMentionsState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ class RoomScreenViewModel: RoomScreenViewModelType, RoomScreenViewModelProtocol
state.showLoading = false
}

private func sendCurrentMessage(_ message: String, html: String, mode: RoomScreenComposerMode, intentionalMentions: IntentionalMentions) async {
private func sendCurrentMessage(_ message: String, html: String?, mode: RoomScreenComposerMode, intentionalMentions: IntentionalMentions) async {
guard !message.isEmpty else {
fatalError("This message should never be empty")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ struct IntentionalMentions: Equatable {
}

extension IntentionalMentions {
static var empty: Self {
IntentionalMentions(userIDs: .init(), atRoom: false)
}

func toRustMentions() -> Mentions {
Mentions(userIds: Array(userIDs), room: atRoom)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ class MockRoomTimelineController: RoomTimelineControllerProtocol {
func processItemDisappearance(_ itemID: TimelineItemIdentifier) async { }

func sendMessage(_ message: String,
html: String,
html: String?,
inReplyTo itemID: TimelineItemIdentifier?,
intentionalMentions: IntentionalMentions) async { }

func toggleReaction(_ reaction: String, to itemID: TimelineItemIdentifier) async { }

func editMessage(_ newMessage: String,
html: String,
html: String?,
original itemID: TimelineItemIdentifier,
intentionalMentions: IntentionalMentions) async { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class RoomTimelineController: RoomTimelineControllerProtocol {
func processItemDisappearance(_ itemID: TimelineItemIdentifier) { }

func sendMessage(_ message: String,
html: String,
html: String?,
inReplyTo itemID: TimelineItemIdentifier?,
intentionalMentions: IntentionalMentions) async {
var inReplyTo: String?
Expand Down Expand Up @@ -155,7 +155,7 @@ class RoomTimelineController: RoomTimelineControllerProtocol {
}

func editMessage(_ newMessage: String,
html: String,
html: String?,
original itemID: TimelineItemIdentifier,
intentionalMentions: IntentionalMentions) async {
MXLog.info("Edit message in \(roomID)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ protocol RoomTimelineControllerProtocol {
func sendReadReceipt(for itemID: TimelineItemIdentifier) async -> Result<Void, RoomTimelineControllerError>

func sendMessage(_ message: String,
html: String,
html: String?,
inReplyTo itemID: TimelineItemIdentifier?,
intentionalMentions: IntentionalMentions) async

func editMessage(_ newMessage: String,
html: String,
html: String?,
original itemID: TimelineItemIdentifier,
intentionalMentions: IntentionalMentions) async

Expand All @@ -78,7 +78,7 @@ protocol RoomTimelineControllerProtocol {

extension RoomTimelineControllerProtocol {
func sendMessage(_ message: String,
html: String,
html: String?,
intentionalMentions: IntentionalMentions) async {
await sendMessage(message,
html: html,
Expand Down
20 changes: 14 additions & 6 deletions ElementX/Sources/Services/Timeline/TimelineProxy.swift
Velin92 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ final class TimelineProxy: TimelineProxyProtocol {
}

func editMessage(_ message: String,
html: String,
html: String?,
original eventID: String,
intentionalMentions: IntentionalMentions) async -> Result<Void, TimelineProxyError> {
MXLog.info("Editing message with original event ID: \(eventID)")

sendMessageBackgroundTask = await backgroundTaskService.startBackgroundTask(withName: backgroundTaskName, isReusable: true)
defer {
sendMessageBackgroundTask?.stop()
Expand Down Expand Up @@ -390,7 +390,7 @@ final class TimelineProxy: TimelineProxyProtocol {
}

func sendMessage(_ message: String,
html: String,
html: String?,
inReplyTo eventID: String? = nil,
intentionalMentions: IntentionalMentions) async -> Result<Void, TimelineProxyError> {
if let eventID {
Expand Down Expand Up @@ -573,18 +573,26 @@ final class TimelineProxy: TimelineProxyProtocol {
// MARK: - Private

private func buildMessageContentFor(_ message: String,
html: String,
html: String?,
intentionalMentions: Mentions) -> RoomMessageEventContentWithoutRelation {
let emoteSlashCommand = "/me "
let isEmote: Bool = message.starts(with: emoteSlashCommand)

let content: RoomMessageEventContentWithoutRelation
if isEmote {
let emoteMessage = String(message.dropFirst(emoteSlashCommand.count))
let emoteHtml = String(html.dropFirst(emoteSlashCommand.count))

var emoteHtml: String?
if let html {
emoteHtml = String(html.dropFirst(emoteSlashCommand.count))
}
content = buildEmoteMessageContentFor(emoteMessage, html: emoteHtml)
} else {
content = messageEventContentFromHtml(body: message, htmlBody: html)
if let html {
content = messageEventContentFromHtml(body: message, htmlBody: html)
} else {
content = messageEventContentFromMarkdown(md: message)
}
}
return content.withMentions(mentions: intentionalMentions)
}
Expand Down
15 changes: 13 additions & 2 deletions ElementX/Sources/Services/Timeline/TimelineProxyProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protocol TimelineProxyProtocol {
func cancelSend(transactionID: String) async

func editMessage(_ message: String,
html: String,
html: String?,
original eventID: String,
intentionalMentions: IntentionalMentions) async -> Result<Void, TimelineProxyError>

Expand Down Expand Up @@ -107,7 +107,7 @@ protocol TimelineProxyProtocol {
func sendMessageEventContent(_ messageContent: RoomMessageEventContentWithoutRelation) async -> Result<Void, TimelineProxyError>

func sendMessage(_ message: String,
html: String,
html: String?,
inReplyTo eventID: String?,
intentionalMentions: IntentionalMentions) async -> Result<Void, TimelineProxyError>

Expand All @@ -125,3 +125,14 @@ protocol TimelineProxyProtocol {

func sendPollResponse(pollStartID: String, answers: [String]) async -> Result<Void, TimelineProxyError>
}

extension TimelineProxyProtocol {
func sendMessage(_ message: String,
html: String?,
intentionalMentions: IntentionalMentions) async -> Result<Void, TimelineProxyError> {
await sendMessage(message,
html: html,
inReplyTo: nil,
intentionalMentions: intentionalMentions)
}
}
14 changes: 14 additions & 0 deletions UnitTests/Sources/ComposerToolbarViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,18 @@ class ComposerToolbarViewModelTests: XCTestCase {

try await deferred.fulfill()
}

func testSuggestionsAreIgnoredWhenRTEDisabled() async throws {
appSettings.richTextEditorEnabled = false
let suggestions: [SuggestionItem] = [.user(item: MentionSuggestionItem(id: "@user_mention_1:matrix.org", displayName: "User 1", avatarURL: nil)),
.user(item: MentionSuggestionItem(id: "@user_mention_2:matrix.org", displayName: "User 2", avatarURL: URL.documentsDirectory))]
let mockCompletionSuggestionService = CompletionSuggestionServiceMock(configuration: .init(suggestions: suggestions))
viewModel = ComposerToolbarViewModel(wysiwygViewModel: wysiwygViewModel,
completionSuggestionService: mockCompletionSuggestionService,
mediaProvider: MockMediaProvider(),
appSettings: ServiceLocator.shared.settings,
mentionDisplayHelper: ComposerMentionDisplayHelper.mock)

XCTAssertEqual(viewModel.state.suggestions, [])
}
}
1 change: 1 addition & 0 deletions changelog.d/pr-2327.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved plain text mode stability.