forked from GitHub-Mirror/riotX-android
131 lines
6.0 KiB
Kotlin
131 lines
6.0 KiB
Kotlin
|
/*
|
||
|
* Copyright 2018 New Vector Ltd
|
||
|
*
|
||
|
* 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
|
||
|
*
|
||
|
* 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.
|
||
|
*/
|
||
|
|
||
|
package im.vector.matrix.android.internal.session.room
|
||
|
|
||
|
import android.content.Context
|
||
|
import com.zhuinden.monarchy.Monarchy
|
||
|
import im.vector.matrix.android.R
|
||
|
import im.vector.matrix.android.api.session.events.model.EventType
|
||
|
import im.vector.matrix.android.api.session.room.Room
|
||
|
import im.vector.matrix.android.api.session.room.model.MyMembership
|
||
|
import im.vector.matrix.android.api.session.room.model.RoomAliasesContent
|
||
|
import im.vector.matrix.android.api.session.room.model.RoomCanonicalAliasContent
|
||
|
import im.vector.matrix.android.api.session.room.model.RoomNameContent
|
||
|
import im.vector.matrix.android.internal.auth.data.SessionParams
|
||
|
import im.vector.matrix.android.internal.database.mapper.asDomain
|
||
|
import im.vector.matrix.android.internal.database.model.EventEntity
|
||
|
import im.vector.matrix.android.internal.database.model.RoomSummaryEntity
|
||
|
import im.vector.matrix.android.internal.database.query.findAllRoomMembers
|
||
|
import im.vector.matrix.android.internal.database.query.last
|
||
|
import im.vector.matrix.android.internal.database.query.where
|
||
|
|
||
|
/**
|
||
|
* This class computes room display name
|
||
|
*/
|
||
|
class RoomDisplayNameResolver(private val monarchy: Monarchy,
|
||
|
private val roomMemberDisplayNameResolver: RoomMemberDisplayNameResolver,
|
||
|
private val sessionParams: SessionParams
|
||
|
) {
|
||
|
|
||
|
/**
|
||
|
* Compute the room display name
|
||
|
*
|
||
|
* @param context
|
||
|
* @param room: the room to resolve the name of.
|
||
|
* @return the room display name
|
||
|
*/
|
||
|
fun resolve(context: Context, room: Room): CharSequence {
|
||
|
// this algorithm is the one defined in
|
||
|
// https://github.com/matrix-org/matrix-js-sdk/blob/develop/lib/models/room.js#L617
|
||
|
// calculateRoomName(room, userId)
|
||
|
|
||
|
// For Lazy Loaded room, see algorithm here:
|
||
|
// https://docs.google.com/document/d/11i14UI1cUz-OJ0knD5BFu7fmT6Fo327zvMYqfSAR7xs/edit#heading=h.qif6pkqyjgzn
|
||
|
var name: CharSequence? = null
|
||
|
monarchy.doWithRealm { realm ->
|
||
|
val roomName = EventEntity.where(realm, room.roomId, EventType.STATE_ROOM_NAME).last()?.asDomain()
|
||
|
name = roomName?.content<RoomNameContent>()?.name
|
||
|
if (!name.isNullOrEmpty()) {
|
||
|
return@doWithRealm
|
||
|
}
|
||
|
|
||
|
val canonicalAlias = EventEntity.where(realm, room.roomId, EventType.STATE_CANONICAL_ALIAS).last()?.asDomain()
|
||
|
name = canonicalAlias?.content<RoomCanonicalAliasContent>()?.canonicalAlias
|
||
|
if (!name.isNullOrEmpty()) {
|
||
|
return@doWithRealm
|
||
|
}
|
||
|
|
||
|
val aliases = EventEntity.where(realm, room.roomId, EventType.STATE_ROOM_ALIASES).last()?.asDomain()
|
||
|
name = aliases?.content<RoomAliasesContent>()?.aliases?.firstOrNull()
|
||
|
if (!name.isNullOrEmpty()) {
|
||
|
return@doWithRealm
|
||
|
}
|
||
|
|
||
|
val otherRoomMembers = EventEntity
|
||
|
.findAllRoomMembers(realm, room.roomId)
|
||
|
.filterKeys { it != sessionParams.credentials.userId }
|
||
|
|
||
|
if (room.myMembership == MyMembership.INVITED) {
|
||
|
//TODO handle invited
|
||
|
/*
|
||
|
if (currentUser != null
|
||
|
&& !othersActiveMembers.isEmpty()
|
||
|
&& !TextUtils.isEmpty(currentUser!!.mSender)) {
|
||
|
// extract who invited us to the room
|
||
|
name = context.getString(R.string.room_displayname_invite_from, roomState.resolve(currentUser!!.mSender))
|
||
|
} else {
|
||
|
name = context.getString(R.string.room_displayname_room_invite)
|
||
|
}
|
||
|
*/
|
||
|
name = context.getString(R.string.room_displayname_room_invite)
|
||
|
} else {
|
||
|
|
||
|
val roomSummary = RoomSummaryEntity.where(realm, room.roomId).findFirst()
|
||
|
val memberIds = if (roomSummary?.heroes?.isNotEmpty() == true) {
|
||
|
roomSummary.heroes
|
||
|
} else {
|
||
|
otherRoomMembers.keys.toList()
|
||
|
}
|
||
|
|
||
|
val nbOfOtherMembers = memberIds.size
|
||
|
|
||
|
when (nbOfOtherMembers) {
|
||
|
0 -> name = context.getString(R.string.room_displayname_empty_room)
|
||
|
1 -> name = roomMemberDisplayNameResolver.resolve(memberIds[0], otherRoomMembers)
|
||
|
2 -> {
|
||
|
val member1 = memberIds[0]
|
||
|
val member2 = memberIds[1]
|
||
|
name = context.getString(R.string.room_displayname_two_members,
|
||
|
roomMemberDisplayNameResolver.resolve(member1, otherRoomMembers),
|
||
|
roomMemberDisplayNameResolver.resolve(member2, otherRoomMembers)
|
||
|
)
|
||
|
}
|
||
|
else -> {
|
||
|
val member = memberIds[0]
|
||
|
name = context.resources.getQuantityString(R.plurals.room_displayname_three_and_more_members,
|
||
|
room.getNumberOfJoinedMembers() - 1,
|
||
|
roomMemberDisplayNameResolver.resolve(member, otherRoomMembers),
|
||
|
room.getNumberOfJoinedMembers() - 1)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return@doWithRealm
|
||
|
}
|
||
|
return name ?: room.roomId
|
||
|
}
|
||
|
}
|