2019-01-18 11:12:08 +01:00
|
|
|
/*
|
2019-01-25 14:04:59 +01:00
|
|
|
* Copyright 2019 New Vector Ltd
|
2019-01-18 11:12:08 +01:00
|
|
|
*
|
2019-01-25 14:04:59 +01: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 11:12:08 +01:00
|
|
|
*
|
2019-01-25 14:04:59 +01: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 11:12:08 +01:00
|
|
|
*/
|
|
|
|
|
2018-10-12 19:26:22 +02:00
|
|
|
package im.vector.matrix.android.internal.database.mapper
|
|
|
|
|
2019-06-07 10:01:42 +02:00
|
|
|
import com.squareup.moshi.JsonDataException
|
2019-07-08 10:58:41 +02:00
|
|
|
import im.vector.matrix.android.api.session.crypto.MXCryptoError
|
2018-11-07 20:36:19 +01:00
|
|
|
import im.vector.matrix.android.api.session.events.model.Event
|
2018-10-17 18:21:09 +02:00
|
|
|
import im.vector.matrix.android.api.session.events.model.UnsignedData
|
2019-07-08 11:18:27 +02:00
|
|
|
import im.vector.matrix.android.internal.crypto.algorithms.olm.OlmDecryptionResult
|
2018-10-12 19:26:22 +02:00
|
|
|
import im.vector.matrix.android.internal.database.model.EventEntity
|
2019-05-16 09:21:10 +02:00
|
|
|
import im.vector.matrix.android.internal.di.MoshiProvider
|
2019-07-08 10:58:41 +02:00
|
|
|
import timber.log.Timber
|
2018-10-12 19:26:22 +02:00
|
|
|
|
2018-11-08 11:04:40 +01:00
|
|
|
internal object EventMapper {
|
2018-10-12 19:26:22 +02:00
|
|
|
|
|
|
|
|
2019-01-03 17:10:02 +01:00
|
|
|
fun map(event: Event, roomId: String): EventEntity {
|
2019-05-16 09:21:10 +02:00
|
|
|
val uds = if (event.unsignedData == null) null
|
|
|
|
else MoshiProvider.providesMoshi().adapter(UnsignedData::class.java).toJson(event.unsignedData)
|
2018-10-12 19:26:22 +02:00
|
|
|
val eventEntity = EventEntity()
|
2019-07-10 19:14:06 +02:00
|
|
|
eventEntity.eventId = event.eventId ?: ""
|
2019-01-03 17:10:02 +01:00
|
|
|
eventEntity.roomId = event.roomId ?: roomId
|
2018-12-17 18:24:01 +01:00
|
|
|
eventEntity.content = ContentMapper.map(event.content)
|
|
|
|
val resolvedPrevContent = event.prevContent ?: event.unsignedData?.prevContent
|
|
|
|
eventEntity.prevContent = ContentMapper.map(resolvedPrevContent)
|
|
|
|
eventEntity.stateKey = event.stateKey
|
2019-05-26 19:21:45 +02:00
|
|
|
eventEntity.type = event.getClearType()
|
2019-06-18 15:58:19 +02:00
|
|
|
eventEntity.sender = event.senderId
|
2018-12-17 18:24:01 +01:00
|
|
|
eventEntity.originServerTs = event.originServerTs
|
|
|
|
eventEntity.redacts = event.redacts
|
|
|
|
eventEntity.age = event.unsignedData?.age ?: event.originServerTs
|
2019-05-16 09:21:10 +02:00
|
|
|
eventEntity.unsignedData = uds
|
2018-10-12 19:26:22 +02:00
|
|
|
return eventEntity
|
|
|
|
}
|
|
|
|
|
2018-11-08 11:04:40 +01:00
|
|
|
fun map(eventEntity: EventEntity): Event {
|
2019-06-07 10:01:42 +02:00
|
|
|
val ud = if (eventEntity.unsignedData.isNullOrBlank()) {
|
|
|
|
null
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
MoshiProvider.providesMoshi().adapter(UnsignedData::class.java).fromJson(eventEntity.unsignedData)
|
|
|
|
} catch (t: JsonDataException) {
|
|
|
|
null
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-10-16 15:52:30 +02:00
|
|
|
return Event(
|
2018-11-07 20:36:19 +01:00
|
|
|
type = eventEntity.type,
|
|
|
|
eventId = eventEntity.eventId,
|
2018-12-17 18:24:01 +01:00
|
|
|
content = ContentMapper.map(eventEntity.content),
|
|
|
|
prevContent = ContentMapper.map(eventEntity.prevContent),
|
2018-11-07 20:36:19 +01:00
|
|
|
originServerTs = eventEntity.originServerTs,
|
2019-06-18 15:58:19 +02:00
|
|
|
senderId = eventEntity.sender,
|
2018-11-07 20:36:19 +01:00
|
|
|
stateKey = eventEntity.stateKey,
|
2019-01-03 17:10:02 +01:00
|
|
|
roomId = eventEntity.roomId,
|
2019-05-16 09:21:10 +02:00
|
|
|
unsignedData = ud,
|
2018-11-07 20:36:19 +01:00
|
|
|
redacts = eventEntity.redacts
|
2019-07-08 10:58:41 +02:00
|
|
|
).also {
|
2019-07-26 16:02:20 +02:00
|
|
|
it.sendState = eventEntity.sendState
|
2019-07-08 10:58:41 +02:00
|
|
|
eventEntity.decryptionResultJson?.let { json ->
|
|
|
|
try {
|
2019-07-08 11:18:27 +02:00
|
|
|
it.mxDecryptionResult = MoshiProvider.providesMoshi().adapter(OlmDecryptionResult::class.java).fromJson(json)
|
2019-07-08 10:58:41 +02:00
|
|
|
} catch (t: JsonDataException) {
|
|
|
|
Timber.e(t, "Failed to parse decryption result")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//TODO get the full crypto error object
|
2019-07-08 11:18:27 +02:00
|
|
|
it.mCryptoError = eventEntity.decryptionErrorCode?.let { MXCryptoError.ErrorType.valueOf(it) }
|
2019-07-08 10:58:41 +02:00
|
|
|
}
|
2018-10-16 15:52:30 +02:00
|
|
|
}
|
2018-11-15 18:32:39 +01:00
|
|
|
|
2019-01-03 17:10:02 +01:00
|
|
|
}
|
2018-11-15 18:32:39 +01:00
|
|
|
|
2018-11-08 11:04:40 +01:00
|
|
|
internal fun EventEntity.asDomain(): Event {
|
2018-10-16 15:52:30 +02:00
|
|
|
return EventMapper.map(this)
|
|
|
|
}
|
|
|
|
|
2019-01-03 17:10:02 +01:00
|
|
|
internal fun Event.toEntity(roomId: String): EventEntity {
|
|
|
|
return EventMapper.map(this, roomId)
|
2018-11-15 18:32:39 +01:00
|
|
|
}
|
2019-01-03 17:10:02 +01:00
|
|
|
|