BayernMessenger/vector/src/main/java/im/vector/riotx/features/home/room/detail/timeline/action/MessageMenuViewModel.kt

283 lines
12 KiB
Kotlin
Raw Normal View History

2019-05-09 16:43:10 +00:00
/*
* 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.riotx.features.home.room.detail.timeline.action
2019-05-07 12:02:15 +00:00
2019-08-05 15:35:26 +00:00
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
2019-06-28 17:28:38 +00:00
import com.airbnb.mvrx.*
import com.squareup.inject.assisted.Assisted
import com.squareup.inject.assisted.AssistedInject
2019-05-07 12:02:15 +00:00
import im.vector.matrix.android.api.session.Session
import im.vector.matrix.android.api.session.events.model.EventType
import im.vector.matrix.android.api.session.events.model.isTextMessage
2019-05-07 12:02:15 +00:00
import im.vector.matrix.android.api.session.events.model.toModel
import im.vector.matrix.android.api.session.room.model.message.MessageContent
import im.vector.matrix.android.api.session.room.model.message.MessageImageContent
import im.vector.matrix.android.api.session.room.model.message.MessageType
import im.vector.matrix.android.api.session.room.send.SendState
import im.vector.matrix.android.api.session.room.timeline.TimelineEvent
import im.vector.matrix.android.api.session.room.timeline.hasBeenEdited
2019-06-28 17:28:38 +00:00
import im.vector.matrix.rx.RxRoom
import im.vector.riotx.R
2019-07-10 10:10:55 +00:00
import im.vector.riotx.core.extensions.canReact
import im.vector.riotx.core.platform.VectorViewModel
import im.vector.riotx.core.resources.StringProvider
import im.vector.riotx.features.home.room.detail.timeline.item.MessageInformationData
2019-05-07 12:02:15 +00:00
2019-06-28 09:28:41 +00:00
2019-08-05 15:35:26 +00:00
sealed class SimpleAction(@StringRes val titleRes: Int, @DrawableRes val iconResId: Int) {
data class AddReaction(val eventId: String) : SimpleAction(R.string.message_add_reaction, R.drawable.ic_add_reaction)
data class Copy(val content: String) : SimpleAction(R.string.copy, R.drawable.ic_copy)
data class Edit(val eventId: String) : SimpleAction(R.string.edit, R.drawable.ic_edit)
data class Quote(val eventId: String) : SimpleAction(R.string.quote, R.drawable.ic_quote)
data class Reply(val eventId: String) : SimpleAction(R.string.reply, R.drawable.ic_reply)
data class Share(val imageUrl: String?) : SimpleAction(R.string.share, R.drawable.ic_share)
data class Resend(val eventId: String) : SimpleAction(R.string.global_retry, R.drawable.ic_refresh_cw)
data class Remove(val eventId: String) : SimpleAction(R.string.remove, R.drawable.ic_trash)
data class Delete(val eventId: String) : SimpleAction(R.string.delete, R.drawable.ic_delete)
data class Cancel(val eventId: String) : SimpleAction(R.string.cancel, R.drawable.ic_close_round)
data class ViewSource(val content: String) : SimpleAction(R.string.view_source, R.drawable.ic_view_source)
data class ViewDecryptedSource(val content: String) : SimpleAction(R.string.view_decrypted_source, R.drawable.ic_view_source)
data class CopyPermalink(val eventId: String) : SimpleAction(R.string.permalink, R.drawable.ic_permalink)
data class Flag(val eventId: String) : SimpleAction(R.string.report_content, R.drawable.ic_flag)
data class QuickReact(val eventId: String, val clickedOn: String, val add: Boolean) : SimpleAction(0, 0)
data class ViewReactions(val messageInformationData: MessageInformationData) : SimpleAction(R.string.message_view_reaction, R.drawable.ic_view_reactions)
data class ViewEditHistory(val messageInformationData: MessageInformationData) :
SimpleAction(R.string.message_view_edit_history, R.drawable.ic_view_edit_history)
2019-08-05 15:35:26 +00:00
}
2019-05-07 12:02:15 +00:00
data class MessageMenuState(
val roomId: String,
val eventId: String,
val informationData: MessageInformationData,
2019-06-28 17:28:38 +00:00
val actions: Async<List<SimpleAction>> = Uninitialized
) : MvRxState {
constructor(args: TimelineEventFragmentArgs) : this(roomId = args.roomId, eventId = args.eventId, informationData = args.informationData)
}
2019-05-07 12:02:15 +00:00
2019-05-09 16:43:10 +00:00
/**
* Manages list actions for a given message (copy / paste / forward...)
*/
class MessageMenuViewModel @AssistedInject constructor(@Assisted initialState: MessageMenuState,
private val session: Session,
private val stringProvider: StringProvider) : VectorViewModel<MessageMenuState>(initialState) {
@AssistedInject.Factory
interface Factory {
fun create(initialState: MessageMenuState): MessageMenuViewModel
}
2019-05-07 12:02:15 +00:00
2019-06-28 17:28:38 +00:00
private val room = session.getRoom(initialState.roomId)
?: throw IllegalStateException("Shouldn't use this ViewModel without a room")
private val eventId = initialState.eventId
private val informationData: MessageInformationData = initialState.informationData
2019-05-07 12:02:15 +00:00
companion object : MvRxViewModelFactory<MessageMenuViewModel, MessageMenuState> {
override fun create(viewModelContext: ViewModelContext, state: MessageMenuState): MessageMenuViewModel? {
val fragment: MessageMenuFragment = (viewModelContext as FragmentViewModelContext).fragment()
return fragment.messageMenuViewModelFactory.create(state)
}
}
init {
2019-06-28 17:28:38 +00:00
observeEvent()
}
private fun observeEvent() {
RxRoom(room)
.liveTimelineEvent(eventId)
2019-08-05 15:03:19 +00:00
.map {
2019-06-28 17:28:38 +00:00
actionsForEvent(it)
}
2019-08-05 15:03:19 +00:00
.execute {
2019-06-28 17:28:38 +00:00
copy(actions = it)
}
}
2019-06-28 17:28:38 +00:00
private fun actionsForEvent(event: TimelineEvent): List<SimpleAction> {
val messageContent: MessageContent? = event.annotations?.editSummary?.aggregatedContent.toModel()
?: event.root.getClearContent().toModel()
val type = messageContent?.type
2019-08-05 15:03:19 +00:00
return arrayListOf<SimpleAction>().apply {
if (event.root.sendState.hasFailed()) {
if (canRetry(event)) {
2019-08-05 15:35:26 +00:00
add(SimpleAction.Resend(eventId))
}
2019-08-05 15:35:26 +00:00
add(SimpleAction.Remove(eventId))
2019-08-05 15:03:19 +00:00
} else if (event.root.sendState.isSending()) {
//TODO is uploading attachment?
if (canCancel(event)) {
2019-08-05 15:35:26 +00:00
add(SimpleAction.Cancel(eventId))
}
2019-08-05 15:03:19 +00:00
} else {
2019-07-12 11:51:05 +00:00
if (!event.root.isRedacted()) {
if (canReply(event, messageContent)) {
2019-08-05 15:35:26 +00:00
add(SimpleAction.Reply(eventId))
2019-07-12 11:51:05 +00:00
}
if (canEdit(event, session.myUserId)) {
2019-08-05 15:35:26 +00:00
add(SimpleAction.Edit(eventId))
2019-07-12 11:51:05 +00:00
}
2019-05-23 14:44:51 +00:00
if (canRedact(event, session.myUserId)) {
2019-08-05 15:35:26 +00:00
add(SimpleAction.Delete(eventId))
2019-07-12 11:51:05 +00:00
}
2019-05-17 15:15:44 +00:00
if (canCopy(type)) {
//TODO copy images? html? see ClipBoard
2019-08-05 15:35:26 +00:00
add(SimpleAction.Copy(messageContent!!.body))
}
if (event.canReact()) {
2019-08-05 15:35:26 +00:00
add(SimpleAction.AddReaction(eventId))
}
2019-07-12 11:51:05 +00:00
if (canQuote(event, messageContent)) {
2019-08-05 15:35:26 +00:00
add(SimpleAction.Quote(eventId))
2019-07-12 11:51:05 +00:00
}
2019-05-07 12:02:15 +00:00
2019-07-12 11:51:05 +00:00
if (canViewReactions(event)) {
2019-08-05 15:35:26 +00:00
add(SimpleAction.ViewReactions(informationData))
2019-07-12 11:51:05 +00:00
}
if (event.hasBeenEdited()) {
add(SimpleAction.ViewEditHistory(informationData))
}
2019-07-12 11:51:05 +00:00
if (canShare(type)) {
if (messageContent is MessageImageContent) {
2019-08-05 15:35:26 +00:00
add(SimpleAction.Share(session.contentUrlResolver().resolveFullSize(messageContent.url)))
2019-07-12 11:51:05 +00:00
}
//TODO
2019-05-07 12:02:15 +00:00
}
if (event.root.sendState == SendState.SENT) {
2019-05-07 12:02:15 +00:00
2019-07-12 11:51:05 +00:00
//TODO Can be redacted
2019-05-07 12:02:15 +00:00
2019-07-12 11:51:05 +00:00
//TODO sent by me or sufficient power level
}
2019-05-07 12:02:15 +00:00
}
2019-08-05 15:35:26 +00:00
add(SimpleAction.ViewSource(event.root.toContentStringWithIndent()))
2019-05-07 12:02:15 +00:00
if (event.isEncrypted()) {
2019-07-09 13:26:32 +00:00
val decryptedContent = event.root.toClearContentStringWithIndent()
?: stringProvider.getString(R.string.encryption_information_decryption_error)
2019-08-05 15:35:26 +00:00
add(SimpleAction.ViewDecryptedSource(decryptedContent))
2019-05-07 12:02:15 +00:00
}
2019-08-05 15:35:26 +00:00
add(SimpleAction.CopyPermalink(eventId))
if (session.myUserId != event.root.senderId && event.root.getClearType() == EventType.MESSAGE) {
//not sent by me
2019-08-05 15:35:26 +00:00
add(SimpleAction.Flag(eventId))
}
2019-05-07 12:02:15 +00:00
}
}
}
2019-05-07 12:02:15 +00:00
private fun canCancel(event: TimelineEvent): Boolean {
return false
}
2019-05-07 12:02:15 +00:00
private fun canReply(event: TimelineEvent, messageContent: MessageContent?): Boolean {
//Only event of type Event.EVENT_TYPE_MESSAGE are supported for the moment
if (event.root.getClearType() != EventType.MESSAGE) return false
return when (messageContent?.type) {
MessageType.MSGTYPE_TEXT,
MessageType.MSGTYPE_NOTICE,
MessageType.MSGTYPE_EMOTE,
MessageType.MSGTYPE_IMAGE,
MessageType.MSGTYPE_VIDEO,
MessageType.MSGTYPE_AUDIO,
MessageType.MSGTYPE_FILE -> true
else -> false
2019-06-07 08:45:24 +00:00
}
}
2019-06-07 08:45:24 +00:00
private fun canQuote(event: TimelineEvent, messageContent: MessageContent?): Boolean {
//Only event of type Event.EVENT_TYPE_MESSAGE are supported for the moment
if (event.root.getClearType() != EventType.MESSAGE) return false
return when (messageContent?.type) {
MessageType.MSGTYPE_TEXT,
MessageType.MSGTYPE_NOTICE,
MessageType.MSGTYPE_EMOTE,
MessageType.FORMAT_MATRIX_HTML,
MessageType.MSGTYPE_LOCATION -> {
true
2019-05-07 12:02:15 +00:00
}
else -> false
2019-05-07 12:02:15 +00:00
}
}
2019-05-07 12:02:15 +00:00
2019-06-28 09:28:41 +00:00
private fun canRedact(event: TimelineEvent, myUserId: String): Boolean {
//Only event of type Event.EVENT_TYPE_MESSAGE are supported for the moment
if (event.root.getClearType() != EventType.MESSAGE) return false
//TODO if user is admin or moderator
return event.root.senderId == myUserId
}
2019-05-17 15:15:44 +00:00
private fun canRetry(event: TimelineEvent): Boolean {
return event.root.sendState.hasFailed() && event.root.isTextMessage()
}
private fun canViewReactions(event: TimelineEvent): Boolean {
//Only event of type Event.EVENT_TYPE_MESSAGE are supported for the moment
if (event.root.getClearType() != EventType.MESSAGE) return false
//TODO if user is admin or moderator
2019-07-09 08:13:56 +00:00
return event.annotations?.reactionsSummary?.isNotEmpty() ?: false
}
2019-06-28 09:28:41 +00:00
private fun canEdit(event: TimelineEvent, myUserId: String): Boolean {
//Only event of type Event.EVENT_TYPE_MESSAGE are supported for the moment
if (event.root.getClearType() != EventType.MESSAGE) return false
//TODO if user is admin or moderator
val messageContent = event.root.getClearContent().toModel<MessageContent>()
2019-06-28 09:28:41 +00:00
return event.root.senderId == myUserId && (
messageContent?.type == MessageType.MSGTYPE_TEXT
|| messageContent?.type == MessageType.MSGTYPE_EMOTE
)
}
2019-05-23 14:44:51 +00:00
2019-05-17 15:15:44 +00:00
private fun canCopy(type: String?): Boolean {
return when (type) {
MessageType.MSGTYPE_TEXT,
MessageType.MSGTYPE_NOTICE,
MessageType.MSGTYPE_EMOTE,
MessageType.FORMAT_MATRIX_HTML,
2019-08-05 15:37:52 +00:00
MessageType.MSGTYPE_LOCATION -> true
else -> false
2019-05-07 12:02:15 +00:00
}
}
2019-05-07 12:02:15 +00:00
private fun canShare(type: String?): Boolean {
return when (type) {
MessageType.MSGTYPE_IMAGE,
MessageType.MSGTYPE_AUDIO,
2019-08-05 15:37:52 +00:00
MessageType.MSGTYPE_VIDEO -> true
else -> false
2019-05-07 12:02:15 +00:00
}
}
}