2019-01-18 10:12:08 +00:00
|
|
|
/*
|
2019-01-25 13:04:59 +00:00
|
|
|
* Copyright 2019 New Vector Ltd
|
2019-01-18 10:12:08 +00:00
|
|
|
*
|
2019-01-25 13:04:59 +00:00
|
|
|
* 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
|
2019-01-18 10:12:08 +00:00
|
|
|
*
|
2019-01-25 13:04:59 +00:00
|
|
|
* 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.
|
2019-01-18 10:12:08 +00:00
|
|
|
*/
|
|
|
|
|
2019-01-18 15:26:17 +00:00
|
|
|
package im.vector.matrix.android.api.session.room.timeline
|
2018-10-22 17:36:29 +00:00
|
|
|
|
2019-01-18 15:26:17 +00:00
|
|
|
import im.vector.matrix.android.api.session.events.model.Event
|
2019-05-07 12:02:15 +00:00
|
|
|
import im.vector.matrix.android.api.session.events.model.EventType
|
2019-07-10 15:37:22 +00:00
|
|
|
import im.vector.matrix.android.api.session.events.model.toModel
|
2019-05-16 07:21:10 +00:00
|
|
|
import im.vector.matrix.android.api.session.room.model.EventAnnotationsSummary
|
2019-07-10 15:37:22 +00:00
|
|
|
import im.vector.matrix.android.api.session.room.model.message.MessageContent
|
2019-04-09 17:57:43 +00:00
|
|
|
import im.vector.matrix.android.api.session.room.send.SendState
|
2018-10-31 12:03:36 +00:00
|
|
|
|
2019-01-18 15:26:17 +00:00
|
|
|
/**
|
|
|
|
* This data class is a wrapper around an Event. It allows to get useful data in the context of a timeline.
|
2019-06-20 14:27:43 +00:00
|
|
|
* This class is used by [TimelineService]
|
2019-01-18 15:26:17 +00:00
|
|
|
* Users can also enrich it with metadata.
|
|
|
|
*/
|
2019-01-14 15:18:39 +00:00
|
|
|
data class TimelineEvent(
|
2018-12-18 11:13:46 +00:00
|
|
|
val root: Event,
|
2019-07-10 17:14:06 +00:00
|
|
|
val localId: Long,
|
2019-03-15 18:27:56 +00:00
|
|
|
val displayIndex: Int,
|
2019-04-30 17:55:55 +00:00
|
|
|
val senderName: String?,
|
2019-07-17 12:56:00 +00:00
|
|
|
val isUniqueDisplayName: Boolean,
|
2019-04-30 17:55:55 +00:00
|
|
|
val senderAvatar: String?,
|
2019-05-16 07:21:10 +00:00
|
|
|
val sendState: SendState,
|
|
|
|
val annotations: EventAnnotationsSummary? = null
|
2018-12-18 11:13:46 +00:00
|
|
|
) {
|
2018-10-22 17:36:29 +00:00
|
|
|
|
2018-10-31 10:22:42 +00:00
|
|
|
val metadata = HashMap<String, Any>()
|
2018-10-22 17:36:29 +00:00
|
|
|
|
2019-01-18 15:26:17 +00:00
|
|
|
/**
|
|
|
|
* The method to enrich this timeline event.
|
|
|
|
* If you provides multiple data with the same key, only first one will be kept.
|
|
|
|
* @param key the key to associate data with.
|
|
|
|
* @param data the data to enrich with.
|
|
|
|
*/
|
2018-11-14 18:32:42 +00:00
|
|
|
fun enrichWith(key: String?, data: Any?) {
|
|
|
|
if (key == null || data == null) {
|
2018-10-22 17:36:29 +00:00
|
|
|
return
|
|
|
|
}
|
2018-10-31 10:22:42 +00:00
|
|
|
if (!metadata.containsKey(key)) {
|
|
|
|
metadata[key] = data
|
|
|
|
}
|
2018-10-22 17:36:29 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 13:44:11 +00:00
|
|
|
fun getDisambiguatedDisplayName(): String {
|
|
|
|
return if (isUniqueDisplayName) {
|
2019-06-20 15:27:15 +00:00
|
|
|
senderName
|
2019-06-18 13:44:11 +00:00
|
|
|
} else {
|
2019-06-20 15:27:15 +00:00
|
|
|
senderName?.let { name ->
|
|
|
|
"$name (${root.senderId})"
|
2019-06-18 13:44:11 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-20 15:27:15 +00:00
|
|
|
?: root.senderId
|
2019-06-18 13:44:11 +00:00
|
|
|
?: ""
|
|
|
|
}
|
|
|
|
|
2019-01-18 15:26:17 +00:00
|
|
|
/**
|
|
|
|
* Get the metadata associated with a key.
|
|
|
|
* @param key the key to get the metadata
|
|
|
|
* @return the metadata
|
|
|
|
*/
|
2018-10-31 10:22:42 +00:00
|
|
|
inline fun <reified T> getMetadata(key: String): T? {
|
|
|
|
return metadata[key] as T?
|
2018-10-22 17:36:29 +00:00
|
|
|
}
|
2019-05-07 12:02:15 +00:00
|
|
|
|
2019-06-18 11:14:39 +00:00
|
|
|
fun isEncrypted(): Boolean {
|
|
|
|
// warning: Do not use getClearType here
|
|
|
|
return EventType.ENCRYPTED == root.type
|
2019-05-07 12:02:15 +00:00
|
|
|
}
|
2018-10-31 12:03:36 +00:00
|
|
|
}
|
2019-07-10 15:37:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get last MessageContent, after a possible edition
|
|
|
|
*/
|
|
|
|
fun TimelineEvent.getLastMessageContent(): MessageContent? = annotations?.editSummary?.aggregatedContent?.toModel()
|
|
|
|
?: root.getClearContent().toModel()
|