Commit 18e83a4b authored by Leonardo Aramaki's avatar Leonardo Aramaki

Observe room changes to the current channel

parent 946b42be
...@@ -13,6 +13,7 @@ import chat.rocket.android.db.DatabaseManagerFactory ...@@ -13,6 +13,7 @@ import chat.rocket.android.db.DatabaseManagerFactory
import chat.rocket.android.db.UserDao import chat.rocket.android.db.UserDao
import chat.rocket.android.server.domain.GetCurrentServerInteractor import chat.rocket.android.server.domain.GetCurrentServerInteractor
import chat.rocket.android.server.domain.GetCurrentUserInteractor import chat.rocket.android.server.domain.GetCurrentUserInteractor
import chat.rocket.android.server.domain.PermissionsInteractor
import chat.rocket.android.server.domain.SettingsRepository import chat.rocket.android.server.domain.SettingsRepository
import chat.rocket.android.server.domain.TokenRepository import chat.rocket.android.server.domain.TokenRepository
import dagger.Module import dagger.Module
...@@ -69,8 +70,9 @@ class ChatRoomFragmentModule { ...@@ -69,8 +70,9 @@ class ChatRoomFragmentModule {
context: Application, context: Application,
repository: SettingsRepository, repository: SettingsRepository,
userInteractor: GetCurrentUserInteractor, userInteractor: GetCurrentUserInteractor,
@Named("currentServer") serverUrl: String @Named("currentServer") serverUrl: String,
permissionsInteractor: PermissionsInteractor
): RoomUiModelMapper { ): RoomUiModelMapper {
return RoomUiModelMapper(context, repository.get(serverUrl), userInteractor, serverUrl) return RoomUiModelMapper(context, repository.get(serverUrl), userInteractor, serverUrl, permissionsInteractor)
} }
} }
...@@ -38,7 +38,6 @@ import chat.rocket.android.server.infraestructure.state ...@@ -38,7 +38,6 @@ import chat.rocket.android.server.infraestructure.state
import chat.rocket.android.util.extension.getByteArray import chat.rocket.android.util.extension.getByteArray
import chat.rocket.android.util.extension.launchUI import chat.rocket.android.util.extension.launchUI
import chat.rocket.android.util.extensions.avatarUrl import chat.rocket.android.util.extensions.avatarUrl
import chat.rocket.android.util.extensions.exhaustive
import chat.rocket.android.util.retryIO import chat.rocket.android.util.retryIO
import chat.rocket.common.RocketChatException import chat.rocket.common.RocketChatException
import chat.rocket.common.model.RoomType import chat.rocket.common.model.RoomType
...@@ -142,7 +141,6 @@ class ChatRoomPresenter @Inject constructor( ...@@ -142,7 +141,6 @@ class ChatRoomPresenter @Inject constructor(
val canModerate = isOwnerOrMod() val canModerate = isOwnerOrMod()
// Can post anyway if has the 'post-readonly' permission on server. // Can post anyway if has the 'post-readonly' permission on server.
val room = dbManager.getRoom(roomId) val room = dbManager.getRoom(roomId)
val canPost = canModerate || permissions.canPostToReadOnlyChannels()
room?.let { room?.let {
chatIsBroadcast = it.chatRoom.broadcast ?: false chatIsBroadcast = it.chatRoom.broadcast ?: false
val roomUiModel = roomMapper.map(it, true) val roomUiModel = roomMapper.map(it, true)
...@@ -150,7 +148,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -150,7 +148,7 @@ class ChatRoomPresenter @Inject constructor(
view.onRoomUpdated(roomUiModel = roomUiModel.copy( view.onRoomUpdated(roomUiModel = roomUiModel.copy(
broadcast = chatIsBroadcast, broadcast = chatIsBroadcast,
canModerate = canModerate, canModerate = canModerate,
canPost = canPost writable = roomUiModel.writable || canModerate
)) ))
} }
} }
...@@ -1010,7 +1008,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -1010,7 +1008,7 @@ class ChatRoomPresenter @Inject constructor(
val canPost = permissions.canPostToReadOnlyChannels() val canPost = permissions.canPostToReadOnlyChannels()
dbManager.getRoom(chatRoomId)?.let { dbManager.getRoom(chatRoomId)?.let {
val roomUiModel = roomMapper.map(it, true).copy( val roomUiModel = roomMapper.map(it, true).copy(
canPost = canPost) writable = canPost)
view.onJoined(roomUiModel = roomUiModel) view.onJoined(roomUiModel = roomUiModel)
view.onRoomUpdated(roomUiModel = roomUiModel) view.onRoomUpdated(roomUiModel = roomUiModel)
} }
......
...@@ -767,7 +767,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR ...@@ -767,7 +767,7 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
} }
private fun setupMessageComposer(roomUiModel: RoomUiModel) { private fun setupMessageComposer(roomUiModel: RoomUiModel) {
if (isReadOnly && !roomUiModel.canPost) { if (isReadOnly || !roomUiModel.writable) {
text_room_is_read_only.isVisible = true text_room_is_read_only.isVisible = true
input_container.isVisible = false input_container.isVisible = false
text_room_is_read_only.setText( text_room_is_read_only.setText(
...@@ -783,6 +783,8 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR ...@@ -783,6 +783,8 @@ class ChatRoomFragment : Fragment(), ChatRoomView, EmojiKeyboardListener, EmojiR
button_join_chat.isVisible = true button_join_chat.isVisible = true
button_join_chat.setOnClickListener { presenter.joinChat(chatRoomId) } button_join_chat.setOnClickListener { presenter.joinChat(chatRoomId) }
} else { } else {
input_container.isVisible = true
text_room_is_read_only.isVisible = false
button_send.isVisible = false button_send.isVisible = false
button_show_attachment_options.alpha = 1f button_show_attachment_options.alpha = 1f
button_show_attachment_options.isVisible = true button_show_attachment_options.isVisible = true
......
...@@ -9,6 +9,7 @@ import chat.rocket.android.R ...@@ -9,6 +9,7 @@ import chat.rocket.android.R
import chat.rocket.android.chatrooms.adapter.model.RoomUiModel import chat.rocket.android.chatrooms.adapter.model.RoomUiModel
import chat.rocket.android.db.model.ChatRoom import chat.rocket.android.db.model.ChatRoom
import chat.rocket.android.server.domain.GetCurrentUserInteractor import chat.rocket.android.server.domain.GetCurrentUserInteractor
import chat.rocket.android.server.domain.PermissionsInteractor
import chat.rocket.android.server.domain.PublicSettings import chat.rocket.android.server.domain.PublicSettings
import chat.rocket.android.server.domain.showLastMessage import chat.rocket.android.server.domain.showLastMessage
import chat.rocket.android.server.domain.useRealName import chat.rocket.android.server.domain.useRealName
...@@ -28,7 +29,8 @@ class RoomUiModelMapper( ...@@ -28,7 +29,8 @@ class RoomUiModelMapper(
private val context: Application, private val context: Application,
private val settings: PublicSettings, private val settings: PublicSettings,
private val userInteractor: GetCurrentUserInteractor, private val userInteractor: GetCurrentUserInteractor,
private val serverUrl: String private val serverUrl: String,
private val permissions: PermissionsInteractor
) { ) {
private val nameUnreadColor = ContextCompat.getColor(context, R.color.colorPrimaryText) private val nameUnreadColor = ContextCompat.getColor(context, R.color.colorPrimaryText)
private val nameColor = ContextCompat.getColor(context, R.color.colorSecondaryText) private val nameColor = ContextCompat.getColor(context, R.color.colorSecondaryText)
...@@ -95,12 +97,13 @@ class RoomUiModelMapper( ...@@ -95,12 +97,13 @@ class RoomUiModelMapper(
avatar = serverUrl.avatarUrl(name!!, isGroupOrChannel = true), avatar = serverUrl.avatarUrl(name!!, isGroupOrChannel = true),
lastMessage = if(showLastMessage) { mapLastMessage(lastMessage?.sender?.id, lastMessage?.sender?.username, lastMessage = if(showLastMessage) { mapLastMessage(lastMessage?.sender?.id, lastMessage?.sender?.username,
lastMessage?.sender?.name, lastMessage?.message, lastMessage?.sender?.name, lastMessage?.message,
isDirectMessage = type is RoomType.DirectMessage)} else { null } isDirectMessage = type is RoomType.DirectMessage)} else { null },
muted = muted.orEmpty(),
writable = isChannelWritable(muted)
) )
} }
} }
@Synchronized
fun map(chatRoom: ChatRoom, showLastMessage:Boolean = true): RoomUiModel { fun map(chatRoom: ChatRoom, showLastMessage:Boolean = true): RoomUiModel {
return with(chatRoom.chatRoom) { return with(chatRoom.chatRoom) {
val isUnread = alert || unread > 0 val isUnread = alert || unread > 0
...@@ -132,11 +135,18 @@ class RoomUiModelMapper( ...@@ -132,11 +135,18 @@ class RoomUiModelMapper(
alert = isUnread, alert = isUnread,
lastMessage = lastMessageMarkdown, lastMessage = lastMessageMarkdown,
status = status, status = status,
username = if (type is RoomType.DirectMessage) name else null username = if (type is RoomType.DirectMessage) name else null,
muted = muted.orEmpty(),
writable = isChannelWritable(muted)
) )
} }
} }
private fun isChannelWritable(muted: List<String>?): Boolean {
val canWriteToReadOnlyChannels = permissions.canPostToReadOnlyChannels()
return canWriteToReadOnlyChannels || !muted.orEmpty().contains(currentUser?.username)
}
private fun roomType(type: String): String { private fun roomType(type: String): String {
val resources = context.resources val resources = context.resources
return when (type) { return when (type) {
......
...@@ -17,6 +17,6 @@ data class RoomUiModel( ...@@ -17,6 +17,6 @@ data class RoomUiModel(
val username: String? = null, val username: String? = null,
val broadcast: Boolean = false, val broadcast: Boolean = false,
val canModerate: Boolean = false, val canModerate: Boolean = false,
val canPost: Boolean = true, val writable: Boolean = true,
val muted: List<String> = emptyList() val muted: List<String> = emptyList()
) )
...@@ -12,6 +12,7 @@ import chat.rocket.android.db.DatabaseManager ...@@ -12,6 +12,7 @@ import chat.rocket.android.db.DatabaseManager
import chat.rocket.android.db.UserDao import chat.rocket.android.db.UserDao
import chat.rocket.android.infrastructure.LocalRepository import chat.rocket.android.infrastructure.LocalRepository
import chat.rocket.android.server.domain.GetCurrentUserInteractor import chat.rocket.android.server.domain.GetCurrentUserInteractor
import chat.rocket.android.server.domain.PermissionsInteractor
import chat.rocket.android.server.domain.PublicSettings import chat.rocket.android.server.domain.PublicSettings
import chat.rocket.android.server.domain.SettingsRepository import chat.rocket.android.server.domain.SettingsRepository
import chat.rocket.android.server.domain.TokenRepository import chat.rocket.android.server.domain.TokenRepository
...@@ -88,9 +89,10 @@ class ChatRoomsFragmentModule { ...@@ -88,9 +89,10 @@ class ChatRoomsFragmentModule {
context: Application, context: Application,
repository: SettingsRepository, repository: SettingsRepository,
userInteractor: GetCurrentUserInteractor, userInteractor: GetCurrentUserInteractor,
@Named("currentServer") serverUrl: String @Named("currentServer") serverUrl: String,
permissionsInteractor: PermissionsInteractor
): RoomUiModelMapper { ): RoomUiModelMapper {
return RoomUiModelMapper(context, repository.get(serverUrl), userInteractor, serverUrl) return RoomUiModelMapper(context, repository.get(serverUrl), userInteractor, serverUrl, permissionsInteractor)
} }
@Provides @Provides
......
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