mirror of
https://github.com/element-hq/element-android
synced 2024-11-28 05:31:21 +03:00
Improve code
This commit is contained in:
parent
c4a019f0d3
commit
0702eee179
5 changed files with 23 additions and 18 deletions
|
@ -37,6 +37,6 @@ class SenderNotificationPermissionCondition(
|
|||
|
||||
fun isSatisfied(event: Event, powerLevels: PowerLevelsContent): Boolean {
|
||||
val powerLevelsHelper = PowerLevelsHelper(powerLevels)
|
||||
return event.senderId != null && powerLevelsHelper.getUserPowerLevelValue(event.senderId) >= powerLevelsHelper.notificationLevel(key)
|
||||
return event.senderId != null && powerLevelsHelper.getUserPowerLevelValue(event.senderId) >= powerLevels.notificationLevel(key)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,4 +53,23 @@ data class PowerLevelsContent(
|
|||
}
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the notification level for a dedicated key.
|
||||
*
|
||||
* @param key the notification key
|
||||
* @return the level, default to Moderator if the key is not found
|
||||
*/
|
||||
fun notificationLevel(key: String): Int {
|
||||
return when (val value = notifications[key]) {
|
||||
// the first implementation was a string value
|
||||
is String -> value.toInt()
|
||||
is Int -> value
|
||||
else -> Role.Moderator.value
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
const val NOTIFICATIONS_ROOM_KEY = "room"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -108,19 +108,4 @@ class PowerLevelsHelper(private val powerLevelsContent: PowerLevelsContent) {
|
|||
val powerLevel = getUserPowerLevelValue(userId)
|
||||
return powerLevel >= powerLevelsContent.redact
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the notification level for a dedicated key.
|
||||
*
|
||||
* @param key the notification key
|
||||
* @return the level
|
||||
*/
|
||||
fun notificationLevel(key: String): Int {
|
||||
return when (val value = powerLevelsContent.notifications[key]) {
|
||||
// the first implementation was a string value
|
||||
is String -> value.toInt()
|
||||
is Int -> value
|
||||
else -> Role.Moderator.value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -123,7 +123,7 @@ class RoomPermissionsController @Inject constructor(
|
|||
is EditablePermission.KickUsers -> content.kick
|
||||
is EditablePermission.BanUsers -> content.ban
|
||||
is EditablePermission.RemoveMessagesSentByOthers -> content.redact
|
||||
is EditablePermission.NotifyEveryone -> (content.notifications["room"] as? Int) ?: Role.Moderator.value
|
||||
is EditablePermission.NotifyEveryone -> content.notificationLevel(PowerLevelsContent.NOTIFICATIONS_ROOM_KEY)
|
||||
}
|
||||
|
||||
return Role.fromValue(
|
||||
|
|
|
@ -30,6 +30,7 @@ import kotlinx.coroutines.launch
|
|||
import org.matrix.android.sdk.api.session.Session
|
||||
import org.matrix.android.sdk.api.session.events.model.EventType
|
||||
import org.matrix.android.sdk.api.session.events.model.toContent
|
||||
import org.matrix.android.sdk.api.session.room.model.PowerLevelsContent
|
||||
import org.matrix.android.sdk.api.session.room.powerlevels.PowerLevelsHelper
|
||||
import org.matrix.android.sdk.rx.rx
|
||||
import org.matrix.android.sdk.rx.unwrap
|
||||
|
@ -118,7 +119,7 @@ class RoomPermissionsViewModel @AssistedInject constructor(@Assisted initialStat
|
|||
is EditablePermission.RemoveMessagesSentByOthers -> currentPowerLevel.copy(redact = action.powerLevel)
|
||||
is EditablePermission.NotifyEveryone -> currentPowerLevel.copy(
|
||||
notifications = currentPowerLevel.notifications.toMutableMap().apply {
|
||||
put("room", action.powerLevel)
|
||||
put(PowerLevelsContent.NOTIFICATIONS_ROOM_KEY, action.powerLevel)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue