Commit ccec99f5 authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Fix coding issues.

parent 42c34407
...@@ -118,7 +118,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -118,7 +118,7 @@ class ChatRoomPresenter @Inject constructor(
private var chatRoomId: String? = null private var chatRoomId: String? = null
private lateinit var chatRoomType: String private lateinit var chatRoomType: String
private lateinit var chatRoomName: String private lateinit var chatRoomName: String
private var chatIsBroadcast: Boolean = false private var isBroadcast: Boolean = false
private var chatRoles = emptyList<ChatRoomRole>() private var chatRoles = emptyList<ChatRoomRole>()
private val stateChannel = Channel<State>() private val stateChannel = Channel<State>()
private var typingStatusSubscriptionId: String? = null private var typingStatusSubscriptionId: String? = null
...@@ -145,14 +145,16 @@ class ChatRoomPresenter @Inject constructor( ...@@ -145,14 +145,16 @@ class ChatRoomPresenter @Inject constructor(
// 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)
room?.let { room?.let {
chatIsBroadcast = it.chatRoom.broadcast ?: false isBroadcast = it.chatRoom.broadcast ?: false
val roomUiModel = roomMapper.map(it, true) val roomUiModel = roomMapper.map(it, true)
launchUI(strategy) { launchUI(strategy) {
view.onRoomUpdated(roomUiModel = roomUiModel.copy( view.onRoomUpdated(
broadcast = chatIsBroadcast, roomUiModel = roomUiModel.copy(
broadcast = isBroadcast,
canModerate = canModerate, canModerate = canModerate,
writable = roomUiModel.writable || canModerate writable = roomUiModel.writable || canModerate
)) )
)
} }
} }
...@@ -160,15 +162,15 @@ class ChatRoomPresenter @Inject constructor( ...@@ -160,15 +162,15 @@ class ChatRoomPresenter @Inject constructor(
loadActiveMembers(roomId, chatRoomType, filterSelfOut = true) loadActiveMembers(roomId, chatRoomType, filterSelfOut = true)
chatRoomMessage?.let { messageHelper.messageIdFromPermalink(it) } chatRoomMessage?.let { messageHelper.messageIdFromPermalink(it) }
?.let { messageId -> ?.let { messageId ->
val name = messageHelper.roomNameFromPermalink(chatRoomMessage) val name = messageHelper.roomNameFromPermalink(chatRoomMessage)
citeMessage( citeMessage(
name!!, name!!,
messageHelper.roomTypeFromPermalink(chatRoomMessage)!!, messageHelper.roomTypeFromPermalink(chatRoomMessage)!!,
messageId, messageId,
true true
) )
} }
/*FIXME: Get chat role can cause unresponsive problems especially on slower connections /*FIXME: Get chat role can cause unresponsive problems especially on slower connections
...@@ -188,14 +190,16 @@ class ChatRoomPresenter @Inject constructor( ...@@ -188,14 +190,16 @@ class ChatRoomPresenter @Inject constructor(
} }
} }
private suspend fun getChatRole() : Boolean { private suspend fun getChatRole(): Boolean {
var returnVal = false
try { try {
if (roomTypeOf(chatRoomType) !is RoomType.DirectMessage) { if (roomTypeOf(chatRoomType) !is RoomType.DirectMessage) {
chatRoles = withContext(Dispatchers.IO + strategy.jobs) { chatRoles = withContext(Dispatchers.IO + strategy.jobs) {
client.chatRoomRoles(roomType = roomTypeOf(chatRoomType), roomName = chatRoomName) client.chatRoomRoles(
roomType = roomTypeOf(chatRoomType),
roomName = chatRoomName
)
} }
returnVal = true return true
} else { } else {
chatRoles = emptyList() chatRoles = emptyList()
} }
...@@ -203,7 +207,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -203,7 +207,7 @@ class ChatRoomPresenter @Inject constructor(
Timber.e(ex) Timber.e(ex)
chatRoles = emptyList() chatRoles = emptyList()
} }
return returnVal return false
} }
private suspend fun subscribeRoomChanges() { private suspend fun subscribeRoomChanges() {
...@@ -212,7 +216,12 @@ class ChatRoomPresenter @Inject constructor( ...@@ -212,7 +216,12 @@ class ChatRoomPresenter @Inject constructor(
manager.addRoomChannel(it, roomChangesChannel) manager.addRoomChannel(it, roomChangesChannel)
for (room in roomChangesChannel) { for (room in roomChangesChannel) {
dbManager.getRoom(room.id)?.let { chatRoom -> dbManager.getRoom(room.id)?.let { chatRoom ->
view.onRoomUpdated(roomMapper.map(chatRoom = chatRoom, showLastMessage = true)) view.onRoomUpdated(
roomMapper.map(
chatRoom = chatRoom,
showLastMessage = true
)
)
} }
} }
} }
...@@ -246,10 +255,10 @@ class ChatRoomPresenter @Inject constructor( ...@@ -246,10 +255,10 @@ class ChatRoomPresenter @Inject constructor(
val localMessages = messagesRepository.getRecentMessages(chatRoomId, 50) val localMessages = messagesRepository.getRecentMessages(chatRoomId, 50)
val oldMessages = mapper.map( val oldMessages = mapper.map(
localMessages, RoomUiModel( localMessages, RoomUiModel(
roles = chatRoles, roles = chatRoles,
// FIXME: Why are we fixing isRoom attribute to true here? // FIXME: Why are we fixing isRoom attribute to true here?
isBroadcast = chatIsBroadcast, isRoom = true isBroadcast = isBroadcast, isRoom = true
) )
) )
lastMessageId = localMessages.firstOrNull()?.id lastMessageId = localMessages.firstOrNull()?.id
val lastSyncDate = messagesRepository.getLastSyncDate(chatRoomId) val lastSyncDate = messagesRepository.getLastSyncDate(chatRoomId)
...@@ -309,7 +318,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -309,7 +318,7 @@ class ChatRoomPresenter @Inject constructor(
view.showMessages( view.showMessages(
mapper.map( mapper.map(
messages, messages,
RoomUiModel(roles = chatRoles, isBroadcast = chatIsBroadcast, isRoom = true) RoomUiModel(roles = chatRoles, isBroadcast = isBroadcast, isRoom = true)
), ),
clearDataSet clearDataSet
) )
...@@ -325,7 +334,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -325,7 +334,7 @@ class ChatRoomPresenter @Inject constructor(
view.showSearchedMessages( view.showSearchedMessages(
mapper.map( mapper.map(
messages, messages,
RoomUiModel(chatRoles, chatIsBroadcast, true) RoomUiModel(chatRoles, isBroadcast, true)
) )
) )
} catch (ex: Exception) { } catch (ex: Exception) {
...@@ -357,7 +366,11 @@ class ChatRoomPresenter @Inject constructor( ...@@ -357,7 +366,11 @@ class ChatRoomPresenter @Inject constructor(
timestamp = Instant.now().toEpochMilli(), timestamp = Instant.now().toEpochMilli(),
sender = SimpleUser(user?.id, user?.username ?: username, user?.name), sender = SimpleUser(user?.id, user?.username ?: username, user?.name),
attachments = null, attachments = null,
avatar = currentServer.avatarUrl(username!!, token?.userId, token?.authToken), avatar = currentServer.avatarUrl(
username!!,
token?.userId,
token?.authToken
),
channels = null, channels = null,
editedAt = null, editedAt = null,
editedBy = null, editedBy = null,
...@@ -379,7 +392,7 @@ class ChatRoomPresenter @Inject constructor( ...@@ -379,7 +392,7 @@ class ChatRoomPresenter @Inject constructor(
view.showNewMessage( view.showNewMessage(
mapper.map( mapper.map(
newMessage, newMessage,
RoomUiModel(roles = chatRoles, isBroadcast = chatIsBroadcast) RoomUiModel(roles = chatRoles, isBroadcast = isBroadcast)
), false ), false
) )
client.sendMessage(id, chatRoomId, text) client.sendMessage(id, chatRoomId, text)
...@@ -630,16 +643,21 @@ class ChatRoomPresenter @Inject constructor( ...@@ -630,16 +643,21 @@ class ChatRoomPresenter @Inject constructor(
Timber.d("History: $messages") Timber.d("History: $messages")
if (messages.result.isNotEmpty()) { if (messages.result.isNotEmpty()) {
val models = mapper.map(messages.result, RoomUiModel( val models = mapper.map(
roles = chatRoles, messages.result, RoomUiModel(
isBroadcast = chatIsBroadcast, roles = chatRoles,
// FIXME: Why are we fixing isRoom attribute to true here? isBroadcast = isBroadcast,
isRoom = true // FIXME: Why are we fixing isRoom attribute to true here?
)) isRoom = true
)
)
messagesRepository.saveAll(messages.result) messagesRepository.saveAll(messages.result)
//if success - saving last synced time //if success - saving last synced time
//assume that BE returns ordered messages, the first message is the latest one //assume that BE returns ordered messages, the first message is the latest one
messagesRepository.saveLastSyncDate(chatRoomId, messages.result.first().timestamp) messagesRepository.saveLastSyncDate(
chatRoomId,
messages.result.first().timestamp
)
launchUI(strategy) { launchUI(strategy) {
view.showNewMessage(models, true) view.showNewMessage(models, true)
...@@ -716,9 +734,9 @@ class ChatRoomPresenter @Inject constructor( ...@@ -716,9 +734,9 @@ class ChatRoomPresenter @Inject constructor(
replyMarkdown = "[ ]($currentServer/$chatRoomType/$room?msg=$id) $mention ", replyMarkdown = "[ ]($currentServer/$chatRoomType/$room?msg=$id) $mention ",
quotedMessage = mapper.map( quotedMessage = mapper.map(
message, RoomUiModel( message, RoomUiModel(
roles = chatRoles, roles = chatRoles,
isBroadcast = chatIsBroadcast isBroadcast = isBroadcast
) )
).last().preview?.message ?: "" ).last().preview?.message ?: ""
) )
} }
...@@ -844,7 +862,8 @@ class ChatRoomPresenter @Inject constructor( ...@@ -844,7 +862,8 @@ class ChatRoomPresenter @Inject constructor(
val sender = it.sender val sender = it.sender
val username = sender?.username ?: "" val username = sender?.username ?: ""
val name = sender?.name ?: "" val name = sender?.name ?: ""
val avatarUrl = currentServer.avatarUrl(username, token?.userId, token?.authToken) val avatarUrl =
currentServer.avatarUrl(username, token?.userId, token?.authToken)
val found = members.firstOrNull { member -> member.username == username } val found = members.firstOrNull { member -> member.username == username }
val status = if (found != null) found.status else UserStatus.Offline() val status = if (found != null) found.status else UserStatus.Offline()
val searchList = mutableListOf(username, name) val searchList = mutableListOf(username, name)
...@@ -865,7 +884,8 @@ class ChatRoomPresenter @Inject constructor( ...@@ -865,7 +884,8 @@ class ChatRoomPresenter @Inject constructor(
activeUsers.addAll(others.map { activeUsers.addAll(others.map {
val username = it.username ?: "" val username = it.username ?: ""
val name = it.name ?: "" val name = it.name ?: ""
val avatarUrl = currentServer.avatarUrl(username, token?.userId, token?.authToken) val avatarUrl =
currentServer.avatarUrl(username, token?.userId, token?.authToken)
val searchList = mutableListOf(username, name) val searchList = mutableListOf(username, name)
PeopleSuggestionUiModel( PeopleSuggestionUiModel(
avatarUrl, avatarUrl,
...@@ -997,48 +1017,49 @@ class ChatRoomPresenter @Inject constructor( ...@@ -997,48 +1017,49 @@ class ChatRoomPresenter @Inject constructor(
} }
// TODO: move this to new interactor or FetchChatRoomsInteractor? // TODO: move this to new interactor or FetchChatRoomsInteractor?
private suspend fun getChatRoomsAsync(name: String? = null): List<ChatRoom> = withContext(Dispatchers.IO) { private suspend fun getChatRoomsAsync(name: String? = null): List<ChatRoom> =
retryDB("getAllSync()") { withContext(Dispatchers.IO) {
dbManager.chatRoomDao().getAllSync().filter { retryDB("getAllSync()") {
if (name == null) { dbManager.chatRoomDao().getAllSync().filter {
return@filter true if (name == null) {
} return@filter true
it.chatRoom.name == name || it.chatRoom.fullname == name }
}.map { it.chatRoom.name == name || it.chatRoom.fullname == name
with(it.chatRoom) { }.map {
ChatRoom( with(it.chatRoom) {
id = id, ChatRoom(
subscriptionId = subscriptionId, id = id,
parentId = parentId, subscriptionId = subscriptionId,
type = roomTypeOf(type), parentId = parentId,
unread = unread, type = roomTypeOf(type),
broadcast = broadcast ?: false, unread = unread,
alert = alert, broadcast = broadcast ?: false,
fullName = fullname, alert = alert,
name = name ?: "", fullName = fullname,
favorite = favorite ?: false, name = name ?: "",
default = isDefault ?: false, favorite = favorite ?: false,
readonly = readonly, default = isDefault ?: false,
open = open, readonly = readonly,
lastMessage = null, open = open,
archived = false, lastMessage = null,
status = null, archived = false,
user = null, status = null,
userMentions = userMentions, user = null,
client = client, userMentions = userMentions,
announcement = null, client = client,
description = null, announcement = null,
groupMentions = groupMentions, description = null,
roles = null, groupMentions = groupMentions,
topic = null, roles = null,
lastSeen = this.lastSeen, topic = null,
timestamp = timestamp, lastSeen = this.lastSeen,
updatedAt = updatedAt timestamp = timestamp,
) updatedAt = updatedAt
)
}
} }
} }
} }
}
fun joinChat(chatRoomId: String) { fun joinChat(chatRoomId: String) {
launchUI(strategy) { launchUI(strategy) {
...@@ -1047,7 +1068,8 @@ class ChatRoomPresenter @Inject constructor( ...@@ -1047,7 +1068,8 @@ 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(
writable = canPost) writable = canPost
)
view.onJoined(roomUiModel = roomUiModel) view.onJoined(roomUiModel = roomUiModel)
view.onRoomUpdated(roomUiModel = roomUiModel) view.onRoomUpdated(roomUiModel = roomUiModel)
} }
...@@ -1308,8 +1330,8 @@ class ChatRoomPresenter @Inject constructor( ...@@ -1308,8 +1330,8 @@ class ChatRoomPresenter @Inject constructor(
launchUI(strategy) { launchUI(strategy) {
val viewModelStreamedMessage = mapper.map( val viewModelStreamedMessage = mapper.map(
streamedMessage, RoomUiModel( streamedMessage, RoomUiModel(
roles = chatRoles, isBroadcast = chatIsBroadcast, isRoom = true roles = chatRoles, isBroadcast = isBroadcast, isRoom = true
) )
) )
val roomMessages = messagesRepository.getByRoomId(streamedMessage.roomId) val roomMessages = messagesRepository.getByRoomId(streamedMessage.roomId)
val index = roomMessages.indexOfFirst { msg -> msg.id == streamedMessage.id } val index = roomMessages.indexOfFirst { msg -> msg.id == streamedMessage.id }
......
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