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