Commit 7ec6d60b authored by Leonardo Aramaki's avatar Leonardo Aramaki

Add a UserHelper class to assist on getting relevant and correctly formatted user information

parent c419e7b9
......@@ -12,8 +12,8 @@ import chat.rocket.android.chatroom.viewmodel.suggestion.CommandSuggestionViewMo
import chat.rocket.android.chatroom.viewmodel.suggestion.PeopleSuggestionViewModel
import chat.rocket.android.core.behaviours.showMessage
import chat.rocket.android.core.lifecycle.CancelStrategy
import chat.rocket.android.helper.UserHelper
import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.infrastructure.username
import chat.rocket.android.server.domain.*
import chat.rocket.android.server.infraestructure.ConnectionManagerFactory
import chat.rocket.android.server.infraestructure.state
......@@ -54,6 +54,7 @@ class ChatRoomPresenter @Inject constructor(
private val usersRepository: UsersRepository,
private val roomsRepository: RoomRepository,
private val localRepository: LocalRepository,
private val userHelper: UserHelper,
factory: ConnectionManagerFactory,
private val mapper: ViewModelMapper,
private val jobSchedulerInteractor: JobSchedulerInteractor
......@@ -134,7 +135,7 @@ class ChatRoomPresenter @Inject constructor(
// ignore message for now, will receive it on the stream
val id = UUID.randomUUID().toString()
val message = if (messageId == null) {
val username = localRepository.username()
val username = userHelper.username()
val newMessage = Message(
id = id,
roomId = chatRoomId,
......@@ -553,7 +554,7 @@ class ChatRoomPresenter @Inject constructor(
fun react(messageId: String, emoji: String) {
launchUI(strategy) {
try {
retryIO("toogleEmoji($messageId, $emoji)") {
retryIO("toggleEmoji($messageId, $emoji)") {
client.toggleReaction(messageId, emoji.removeSurrounding(":"))
}
} catch (ex: RocketChatException) {
......
package chat.rocket.android.helper
import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.server.domain.GetCurrentServerInteractor
import chat.rocket.android.server.domain.PublicSettings
import chat.rocket.android.server.domain.SettingsRepository
import chat.rocket.android.server.domain.useRealName
import chat.rocket.common.model.User
import javax.inject.Inject
class UserHelper @Inject constructor(
private val localRepository: LocalRepository,
private val getCurrentServerInteractor: GetCurrentServerInteractor,
settingsRepository: SettingsRepository
) {
private val settings: PublicSettings = settingsRepository.get(getCurrentServerInteractor.get()!!)
/**
* Return the display name for the given [user].
* If setting 'Use_Real_Name' is true then the real name will be given, or else
* the username without the '@' is yielded. The fallback for any case is the username, which
* could be null.
*/
fun displayName(user: User): String? {
return if (settings.useRealName()) user.name ?: user.username else user.username
}
/**
* Return current logged user's display name.
*
* @see displayName
*/
fun displayName(): String? {
user()?.let {
return displayName(it)
}
return null
}
/**
* Return current logged [User].
*/
fun user(): User? {
return localRepository.getCurrentUser(serverUrl())
}
/**
* Return the username for the current logged [User].
*/
fun username(): String? = user()?.username
/**
* Whether current [User] is admin on the current server.
*/
fun isAdmin(): Boolean {
return user()?.roles?.find { it.equals("admin", ignoreCase = true) } != null
}
private fun serverUrl(): String {
return getCurrentServerInteractor.get()!!
}
}
\ No newline at end of file
package chat.rocket.android.server.domain
import chat.rocket.android.helper.UserHelper
import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.common.model.User
import chat.rocket.core.model.Permission
import javax.inject.Inject
......@@ -20,14 +20,14 @@ const val POST_READONLY = "post-readonly"
class PermissionsInteractor @Inject constructor(
private val settingsRepository: SettingsRepository,
private val permissionsRepository: PermissionsRepository,
private val localRepository: LocalRepository,
private val getCurrentServerInteractor: GetCurrentServerInteractor
private val getCurrentServerInteractor: GetCurrentServerInteractor,
private val userHelper: UserHelper
) {
private fun publicSettings(): PublicSettings? = settingsRepository.get(getCurrentServerUrl()!!)
private fun publicSettings(): PublicSettings? = settingsRepository.get(currentServerUrl()!!)
fun saveAll(permissions: List<Permission>) {
val url = getCurrentServerUrl()!!
val url = currentServerUrl()!!
permissions.forEach { permissionsRepository.save(url, it) }
}
......@@ -57,25 +57,16 @@ class PermissionsInteractor @Inject constructor(
fun showEditedStatus() = publicSettings()?.showEditedStatus() ?: false
fun canPostToReadOnlyChannels(): Boolean {
val url = getCurrentServerUrl()!!
val currentUserRoles = currentUser()?.roles
val url = getCurrentServerInteractor.get()!!
val currentUserRoles = userHelper.user()?.roles
return permissionsRepository.get(url, POST_READONLY)?.let { permission ->
currentUserRoles?.isNotEmpty() == true && permission.roles.any {
currentUserRoles.contains(it)
}
} == true || isAdmin()
} == true || userHelper.isAdmin()
}
private fun currentUser(): User? {
val url = getCurrentServerUrl()!!
return localRepository.getCurrentUser(url)
}
private fun isAdmin(): Boolean {
return currentUser()?.roles?.find { it.equals("admin", ignoreCase = true) } != null
}
private fun getCurrentServerUrl(): String? {
private fun currentServerUrl(): String? {
return getCurrentServerInteractor.get()
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment