From d8092abc4e55917ef3cecd7516be5caf3e1c6890 Mon Sep 17 00:00:00 2001 From: Valere Date: Mon, 15 Jul 2019 17:18:31 +0200 Subject: [PATCH] fix / strip reply prefix on history --- CHANGES.md | 3 +- .../matrix/android/api/util/ContentUtils.kt | 45 +++++++++++++++++++ .../internal/session/room/send/TextContent.kt | 26 +---------- .../action/ViewEditHistoryEpoxyController.kt | 14 +++--- .../action/ViewEditHistoryViewModel.kt | 10 ++++- 5 files changed, 67 insertions(+), 31 deletions(-) create mode 100644 matrix-sdk-android/src/main/java/im/vector/matrix/android/api/util/ContentUtils.kt diff --git a/CHANGES.md b/CHANGES.md index ee6d2c8c06..2b666e88f1 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,11 +2,12 @@ Changes in RiotX 0.2.1 (2019-XX-XX) =================================================== Features: - - Message Editing: View edit history + - Message Editing: View edit history (#121) - Rooms filtering (#304) Improvements: - Handle click on redacted events: view source and create permalink + - Improve edit of replies Other changes: - diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/util/ContentUtils.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/util/ContentUtils.kt new file mode 100644 index 0000000000..0a395deeda --- /dev/null +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/util/ContentUtils.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2019 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package im.vector.matrix.android.api.util + + +object ContentUtils { + fun extractUsefulTextFromReply(repliedBody: String): String { + val lines = repliedBody.lines() + var wellFormed = repliedBody.startsWith(">") + var endOfPreviousFound = false + val usefullines = ArrayList() + lines.forEach { + if (it == "") { + endOfPreviousFound = true + return@forEach + } + if (!endOfPreviousFound) { + wellFormed = wellFormed && it.startsWith(">") + } else { + usefullines.add(it) + } + } + return usefullines.joinToString("\n").takeIf { wellFormed } ?: repliedBody + } + + fun extractUsefulTextFromHtmlReply(repliedBody: String): String { + if (repliedBody.startsWith("")) { + return repliedBody.substring(repliedBody.lastIndexOf("") + "".length).trim() + } + return repliedBody + } +} \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/TextContent.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/TextContent.kt index 435efa6e33..bf7cb36188 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/TextContent.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/TextContent.kt @@ -18,6 +18,8 @@ package im.vector.matrix.android.internal.session.room.send import im.vector.matrix.android.api.session.room.model.message.MessageTextContent import im.vector.matrix.android.api.session.room.model.message.MessageType +import im.vector.matrix.android.api.util.ContentUtils.extractUsefulTextFromHtmlReply +import im.vector.matrix.android.api.util.ContentUtils.extractUsefulTextFromReply /** * Contains a text and eventually a formatted text @@ -47,28 +49,4 @@ fun TextContent.removeInReplyFallbacks(): TextContent { ) } -fun extractUsefulTextFromReply(repliedBody: String): String { - val lines = repliedBody.lines() - var wellFormed = repliedBody.startsWith(">") - var endOfPreviousFound = false - val usefullines = ArrayList() - lines.forEach { - if (it == "") { - endOfPreviousFound = true - return@forEach - } - if (!endOfPreviousFound) { - wellFormed = wellFormed && it.startsWith(">") - } else { - usefullines.add(it) - } - } - return usefullines.joinToString("\n").takeIf { wellFormed } ?: repliedBody -} -fun extractUsefulTextFromHtmlReply(repliedBody: String): String { - if (repliedBody.startsWith("")) { - return repliedBody.substring(repliedBody.lastIndexOf("") + "".length).trim() - } - return repliedBody -} diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/action/ViewEditHistoryEpoxyController.kt b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/action/ViewEditHistoryEpoxyController.kt index 4ae62fbd93..34e340731f 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/action/ViewEditHistoryEpoxyController.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/action/ViewEditHistoryEpoxyController.kt @@ -26,6 +26,7 @@ import com.airbnb.mvrx.Success import im.vector.matrix.android.api.session.events.model.Event import im.vector.matrix.android.api.session.events.model.toModel import im.vector.matrix.android.api.session.room.model.message.MessageTextContent +import im.vector.matrix.android.api.util.ContentUtils.extractUsefulTextFromReply import im.vector.riotx.R import im.vector.riotx.core.extensions.localDateTime import im.vector.riotx.core.ui.list.genericFooterItem @@ -60,13 +61,13 @@ class ViewEditHistoryEpoxyController(private val context: Context, } } is Success -> { - state.editList()?.let { renderEvents(it) } + state.editList()?.let { renderEvents(it, state.isOriginalAReply) } } } } - private fun renderEvents(sourceEvents: List) { + private fun renderEvents(sourceEvents: List, isOriginalReply: Boolean) { if (sourceEvents.isEmpty()) { genericItem { id("footer") @@ -92,7 +93,7 @@ class ViewEditHistoryEpoxyController(private val context: Context, } } lastDate = evDate - val cContent = getCorrectContent(timelineEvent) + val cContent = getCorrectContent(timelineEvent, isOriginalReply) val body = cContent.second?.let { eventHtmlRenderer.render(it) } ?: cContent.first @@ -101,7 +102,7 @@ class ViewEditHistoryEpoxyController(private val context: Context, var spannedDiff: Spannable? = null if (nextEvent != null && cContent.second == null /*No diff for html*/) { //compares the body - val nContent = getCorrectContent(nextEvent) + val nContent = getCorrectContent(nextEvent, isOriginalReply) val nextBody = nContent.second?.let { eventHtmlRenderer.render(it) } ?: nContent.first val dmp = diff_match_patch() @@ -144,11 +145,14 @@ class ViewEditHistoryEpoxyController(private val context: Context, } } - private fun getCorrectContent(event: Event): Pair { + private fun getCorrectContent(event: Event, isOriginalReply: Boolean): Pair { val clearContent = event.getClearContent().toModel() val newContent = clearContent ?.newContent ?.toModel() + if (isOriginalReply) { + return extractUsefulTextFromReply(newContent?.body ?: clearContent?.body ?: "") to null + } return (newContent?.body ?: clearContent?.body ?: "") to (newContent?.formattedBody ?: clearContent?.formattedBody) } diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/action/ViewEditHistoryViewModel.kt b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/action/ViewEditHistoryViewModel.kt index 64005c3fa3..bda46d46fb 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/action/ViewEditHistoryViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/action/ViewEditHistoryViewModel.kt @@ -21,6 +21,8 @@ import com.squareup.inject.assisted.AssistedInject import im.vector.matrix.android.api.MatrixCallback import im.vector.matrix.android.api.session.Session import im.vector.matrix.android.api.session.events.model.Event +import im.vector.matrix.android.api.session.events.model.toModel +import im.vector.matrix.android.api.session.room.model.message.MessageContent import im.vector.riotx.core.platform.VectorViewModel import im.vector.riotx.features.home.room.detail.timeline.helper.TimelineDateFormatter @@ -28,6 +30,7 @@ import im.vector.riotx.features.home.room.detail.timeline.helper.TimelineDateFor data class ViewEditHistoryViewState( val eventId: String, val roomId: String, + val isOriginalAReply: Boolean = false, val editList: Async> = Uninitialized) : MvRxState { @@ -77,11 +80,16 @@ class ViewEditHistoryViewModel @AssistedInject constructor(@Assisted override fun onSuccess(data: List) { //TODO until supported by API Add original event manually val withOriginal = data.toMutableList() + var originalIsReply = false room.getTimeLineEvent(eventId)?.let { withOriginal.add(it.root) + originalIsReply = it.root.getClearContent().toModel()?.relatesTo?.inReplyTo?.eventId != null } setState { - copy(editList = Success(withOriginal)) + copy( + editList = Success(withOriginal), + isOriginalAReply = originalIsReply + ) } } })