This repository was archived by the owner on Jun 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 544
Expand file tree
/
Copy pathUserHelper.kt
More file actions
49 lines (42 loc) · 1.62 KB
/
UserHelper.kt
File metadata and controls
49 lines (42 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package chat.rocket.android.helper
import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.server.domain.GetCurrentServerInteractor
import chat.rocket.android.server.domain.SettingsRepository
import chat.rocket.android.server.domain.useRealName
import chat.rocket.common.model.SimpleUser
import chat.rocket.common.model.User
import javax.inject.Inject
class UserHelper @Inject constructor(
private val localRepository: LocalRepository,
private val getCurrentServerInteractor: GetCurrentServerInteractor,
private val settingsRepository: SettingsRepository
) {
/**
* Return current logged [User].
*/
fun user(): User? = getCurrentServerInteractor.get()?.let { localRepository.getCurrentUser(it) }
/**
* Saves current User data.
*/
fun updateUser(url: String, user: User) = localRepository.saveCurrentUser(url, user)
/**
* Return the username for the current logged [User].
*/
fun username(): String? = localRepository.get(LocalRepository.CURRENT_USERNAME_KEY, null)
/**
* Return the display name for the given [user].
* If setting 'Use_Real_Name' is true then the real name will be given, otherwise the username
* without the '@' is yielded.
*/
fun displayName(user: SimpleUser) = getCurrentServerInteractor.get()?.let {
if (settingsRepository.get(it).useRealName()) {
user.name
} else {
user.username
}
}.orEmpty()
/**
* Whether current [User] is admin on the current server.
*/
fun isAdmin(): Boolean = user()?.roles?.find { it.equals("admin", true) } != null
}