Mutualize code

This commit is contained in:
Benoit Marty 2020-06-27 11:43:13 +02:00
parent 30774957ba
commit cec79fed44
4 changed files with 18 additions and 12 deletions

View File

@ -35,4 +35,18 @@ data class PowerLevelsContent(
@Json(name = "users") val users: MutableMap<String, Int> = HashMap(),
@Json(name = "state_default") val stateDefault: Int = Role.Moderator.value,
@Json(name = "notifications") val notifications: Map<String, Any> = HashMap()
)
) {
/**
* Alter this content with a new power level for the specified user
*
* @param userId the userId to alter the power level of
* @param powerLevel the new power level, or null to set the default value.
*/
fun setUserPowerLevel(userId: String, powerLevel: Int?) {
if (powerLevel == null || powerLevel == usersDefault) {
users.remove(userId)
} else {
users[userId] = powerLevel
}
}
}

View File

@ -44,6 +44,7 @@ class PowerLevelsHelper(private val powerLevelsContent: PowerLevelsContent) {
*/
fun getUserRole(userId: String): Role {
val value = getUserPowerLevelValue(userId)
// I think we should use powerLevelsContent.usersDefault, but Ganfra told me that it was like that on riot-Web
return Role.fromValue(value, powerLevelsContent.eventsDefault)
}

View File

@ -675,12 +675,7 @@ class RoomDetailViewModel @AssistedInject constructor(
?.toModel<PowerLevelsContent>() ?: return
launchSlashCommandFlow {
if (setUserPowerLevel.powerLevel == null) {
currentPowerLevelsContent.users.remove(setUserPowerLevel.userId)
} else {
currentPowerLevelsContent.users[setUserPowerLevel.userId] = setUserPowerLevel.powerLevel
}
currentPowerLevelsContent.setUserPowerLevel(setUserPowerLevel.userId, setUserPowerLevel.powerLevel)
room.sendStateEvent(EventType.STATE_ROOM_POWER_LEVELS, null, currentPowerLevelsContent.toContent(), it)
}
}

View File

@ -162,11 +162,7 @@ class RoomMemberProfileViewModel @AssistedInject constructor(@Assisted private v
} else if (action.askForValidation && state.isMine) {
_viewEvents.post(RoomMemberProfileViewEvents.ShowPowerLevelDemoteWarning(action.previousValue, action.newValue))
} else {
if (action.newValue == currentPowerLevelsContent.usersDefault) {
currentPowerLevelsContent.users.remove(state.userId)
} else {
currentPowerLevelsContent.users[state.userId] = action.newValue
}
currentPowerLevelsContent.setUserPowerLevel(state.userId, action.newValue)
viewModelScope.launch {
_viewEvents.post(RoomMemberProfileViewEvents.Loading())
try {