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-17 18:21:09 +02:00
|
|
|
package im.vector.matrix.android.internal.session.room
|
|
|
|
|
2019-01-16 19:25:43 +01:00
|
|
|
import androidx.lifecycle.LiveData
|
|
|
|
import androidx.lifecycle.Transformations
|
2018-11-27 12:06:40 +01:00
|
|
|
import com.zhuinden.monarchy.Monarchy
|
2019-05-21 15:42:09 +02:00
|
|
|
import im.vector.matrix.android.api.session.crypto.CryptoService
|
2018-10-17 18:21:09 +02:00
|
|
|
import im.vector.matrix.android.api.session.room.Room
|
2019-05-07 19:33:58 +02:00
|
|
|
import im.vector.matrix.android.api.session.room.members.MembershipService
|
2018-10-29 14:57:36 +01:00
|
|
|
import im.vector.matrix.android.api.session.room.model.RoomSummary
|
2019-05-28 10:45:58 +02:00
|
|
|
import im.vector.matrix.android.api.session.room.model.relation.RelationService
|
2019-01-28 17:56:23 +01:00
|
|
|
import im.vector.matrix.android.api.session.room.read.ReadService
|
|
|
|
import im.vector.matrix.android.api.session.room.send.SendService
|
2019-04-09 18:33:28 +02:00
|
|
|
import im.vector.matrix.android.api.session.room.state.StateService
|
2019-01-14 16:18:39 +01:00
|
|
|
import im.vector.matrix.android.api.session.room.timeline.TimelineService
|
2019-03-21 20:21:45 +01:00
|
|
|
import im.vector.matrix.android.internal.database.RealmLiveData
|
2019-07-01 12:35:48 +02:00
|
|
|
import im.vector.matrix.android.internal.database.mapper.RoomSummaryMapper
|
2018-10-23 18:25:28 +02:00
|
|
|
import im.vector.matrix.android.internal.database.model.RoomSummaryEntity
|
2018-10-30 18:22:29 +01:00
|
|
|
import im.vector.matrix.android.internal.database.model.RoomSummaryEntityFields
|
2018-10-18 18:38:11 +02:00
|
|
|
import im.vector.matrix.android.internal.database.query.where
|
2019-06-16 17:00:47 +02:00
|
|
|
import javax.inject.Inject
|
2018-10-17 18:21:09 +02:00
|
|
|
|
2019-06-20 19:26:59 +02:00
|
|
|
internal class DefaultRoom @Inject constructor(override val roomId: String,
|
|
|
|
private val monarchy: Monarchy,
|
2019-07-01 12:35:48 +02:00
|
|
|
private val roomSummaryMapper: RoomSummaryMapper,
|
2019-06-20 19:26:59 +02:00
|
|
|
private val timelineService: TimelineService,
|
|
|
|
private val sendService: SendService,
|
|
|
|
private val stateService: StateService,
|
|
|
|
private val readService: ReadService,
|
|
|
|
private val cryptoService: CryptoService,
|
|
|
|
private val relationService: RelationService,
|
|
|
|
private val roomMembersService: MembershipService
|
2019-01-28 18:42:29 +01:00
|
|
|
) : Room,
|
2019-07-02 19:59:01 +02:00
|
|
|
TimelineService by timelineService,
|
|
|
|
SendService by sendService,
|
|
|
|
StateService by stateService,
|
|
|
|
ReadService by readService,
|
|
|
|
RelationService by relationService,
|
|
|
|
MembershipService by roomMembersService {
|
2018-10-22 19:36:29 +02:00
|
|
|
|
2019-07-08 15:32:24 +02:00
|
|
|
override fun liveRoomSummary(): LiveData<RoomSummary> {
|
2019-03-21 20:21:45 +01:00
|
|
|
val liveRealmData = RealmLiveData<RoomSummaryEntity>(monarchy.realmConfiguration) { realm ->
|
|
|
|
RoomSummaryEntity.where(realm, roomId).isNotEmpty(RoomSummaryEntityFields.DISPLAY_NAME)
|
|
|
|
}
|
2019-07-02 19:59:01 +02:00
|
|
|
return Transformations.map(liveRealmData) { results ->
|
2019-08-28 16:31:29 +02:00
|
|
|
val roomSummaries = results.map { roomSummaryMapper.map(it, this) }
|
2019-04-17 15:48:59 +02:00
|
|
|
|
|
|
|
if (roomSummaries.isEmpty()) {
|
|
|
|
// Create a dummy RoomSummary to avoid Crash during Sign Out or clear cache
|
|
|
|
RoomSummary(roomId)
|
|
|
|
} else {
|
|
|
|
roomSummaries.first()
|
|
|
|
}
|
2018-10-29 14:57:36 +01:00
|
|
|
}
|
2018-10-18 18:38:11 +02:00
|
|
|
}
|
|
|
|
|
2019-07-08 15:32:24 +02:00
|
|
|
override fun roomSummary(): RoomSummary? {
|
2019-07-02 19:59:01 +02:00
|
|
|
return monarchy.fetchAllMappedSync(
|
2019-07-03 11:59:45 +02:00
|
|
|
{ realm -> RoomSummaryEntity.where(realm, roomId).isNotEmpty(RoomSummaryEntityFields.DISPLAY_NAME) },
|
2019-08-28 16:31:29 +02:00
|
|
|
{ roomSummaryMapper.map(it, this) }
|
2019-07-02 19:59:01 +02:00
|
|
|
).firstOrNull()
|
|
|
|
}
|
2019-06-22 17:08:45 +02:00
|
|
|
|
2019-05-21 15:42:09 +02:00
|
|
|
override fun isEncrypted(): Boolean {
|
|
|
|
return cryptoService.isRoomEncrypted(roomId)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun encryptionAlgorithm(): String? {
|
|
|
|
return cryptoService.getEncryptionAlgorithm(roomId)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun shouldEncryptForInvitedMembers(): Boolean {
|
|
|
|
return cryptoService.shouldEncryptForInvitedMembers(roomId)
|
|
|
|
}
|
|
|
|
|
2018-10-18 18:38:11 +02:00
|
|
|
}
|