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-11-15 17:32:39 +00:00
|
|
|
package im.vector.matrix.android.internal.database.helper
|
|
|
|
|
2018-11-22 10:20:50 +00:00
|
|
|
import im.vector.matrix.android.api.session.events.model.Event
|
2019-04-09 17:57:43 +00:00
|
|
|
import im.vector.matrix.android.api.session.room.send.SendState
|
2019-01-03 16:10:02 +00:00
|
|
|
import im.vector.matrix.android.internal.database.mapper.toEntity
|
2018-11-15 17:32:39 +00:00
|
|
|
import im.vector.matrix.android.internal.database.model.ChunkEntity
|
|
|
|
import im.vector.matrix.android.internal.database.model.RoomEntity
|
2019-07-05 14:07:12 +00:00
|
|
|
import im.vector.matrix.android.internal.database.model.TimelineEventEntity
|
2019-02-26 13:50:53 +00:00
|
|
|
import im.vector.matrix.android.internal.database.query.fastContains
|
2019-03-27 17:03:36 +00:00
|
|
|
import im.vector.matrix.android.internal.extensions.assertIsManaged
|
2019-07-05 17:07:33 +00:00
|
|
|
import im.vector.matrix.android.internal.session.room.membership.RoomMembers
|
2018-11-15 17:32:39 +00:00
|
|
|
|
|
|
|
internal fun RoomEntity.deleteOnCascade(chunkEntity: ChunkEntity) {
|
|
|
|
chunks.remove(chunkEntity)
|
2018-11-28 17:28:35 +00:00
|
|
|
chunkEntity.deleteOnCascade()
|
2018-11-15 17:32:39 +00:00
|
|
|
}
|
2018-11-15 18:00:15 +00:00
|
|
|
|
|
|
|
internal fun RoomEntity.addOrUpdate(chunkEntity: ChunkEntity) {
|
|
|
|
if (!chunks.contains(chunkEntity)) {
|
|
|
|
chunks.add(chunkEntity)
|
|
|
|
}
|
2018-11-22 10:20:50 +00:00
|
|
|
}
|
|
|
|
|
2019-07-11 16:55:13 +00:00
|
|
|
internal fun RoomEntity.addStateEvent(stateEvent: Event,
|
|
|
|
stateIndex: Int = Int.MIN_VALUE,
|
|
|
|
filterDuplicates: Boolean = false,
|
|
|
|
isUnlinked: Boolean = false) {
|
2019-03-27 17:03:36 +00:00
|
|
|
assertIsManaged()
|
2019-07-11 16:55:13 +00:00
|
|
|
if (stateEvent.eventId == null || (filterDuplicates && fastContains(stateEvent.eventId))) {
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
val entity = stateEvent.toEntity(roomId).apply {
|
2019-03-13 21:30:05 +00:00
|
|
|
this.stateIndex = stateIndex
|
|
|
|
this.isUnlinked = isUnlinked
|
2019-07-02 10:21:27 +00:00
|
|
|
this.sendState = SendState.SYNCED
|
2019-03-13 21:30:05 +00:00
|
|
|
}
|
2019-07-11 16:55:13 +00:00
|
|
|
untimelinedStateEvents.add(entity)
|
|
|
|
}
|
|
|
|
}
|
2019-07-01 11:20:03 +00:00
|
|
|
internal fun RoomEntity.addSendingEvent(event: Event) {
|
2019-04-09 17:57:43 +00:00
|
|
|
assertIsManaged()
|
2019-07-05 17:07:33 +00:00
|
|
|
val senderId = event.senderId ?: return
|
2019-04-09 17:57:43 +00:00
|
|
|
val eventEntity = event.toEntity(roomId).apply {
|
|
|
|
this.sendState = SendState.UNSENT
|
|
|
|
}
|
2019-07-05 17:07:33 +00:00
|
|
|
val roomMembers = RoomMembers(realm, roomId)
|
|
|
|
val myUser = roomMembers.get(senderId)
|
2019-07-11 16:55:13 +00:00
|
|
|
val localId = TimelineEventEntity.nextId(realm)
|
2019-07-10 17:14:06 +00:00
|
|
|
val timelineEventEntity = TimelineEventEntity(localId).also {
|
2019-07-05 14:07:12 +00:00
|
|
|
it.root = eventEntity
|
|
|
|
it.eventId = event.eventId ?: ""
|
|
|
|
it.roomId = roomId
|
2019-07-05 17:07:33 +00:00
|
|
|
it.senderName = myUser?.displayName
|
|
|
|
it.senderAvatar = myUser?.avatarUrl
|
|
|
|
it.isUniqueDisplayName = roomMembers.isUniqueDisplayName(myUser?.displayName)
|
2019-07-08 13:32:24 +00:00
|
|
|
it.senderMembershipEvent = roomMembers.queryRoomMemberEvent(senderId).findFirst()
|
2019-07-05 14:07:12 +00:00
|
|
|
}
|
|
|
|
sendingTimelineEvents.add(0, timelineEventEntity)
|
2019-04-09 17:57:43 +00:00
|
|
|
}
|