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