Fix live messages

This commit is contained in:
Alex Sytnyk 2018-08-16 15:15:50 +03:00
parent 7d3ea473e8
commit 04d6e82157
3 changed files with 40 additions and 27 deletions

View file

@ -491,7 +491,7 @@ class TelegramHelper private constructor() {
} else if (oldContent is TdApi.MessageLocation && (fromBot || viaBot)) {
message.content = parseOsmAndBotLocation(message)
}
removeOldMessages(message.senderUserId, message.chatId)
removeOldMessages(message, fromBot, viaBot)
usersLocationMessages[message.id] = message
incomingMessagesListeners.forEach {
it.onReceiveChatLocationMessages(message.chatId, message)
@ -499,12 +499,24 @@ class TelegramHelper private constructor() {
}
}
private fun removeOldMessages(userId: Int, chatId: Long) {
val user = users[userId]
if (user != null && user.username != OSMAND_BOT_USERNAME) {
usersLocationMessages.values.filter { it.senderUserId == userId && it.chatId == chatId }
.forEach {
usersLocationMessages.remove(it.id)
private fun removeOldMessages(newMessage: TdApi.Message, fromBot: Boolean, viaBot: Boolean) {
val iterator = usersLocationMessages.entries.iterator()
while (iterator.hasNext()) {
val message = iterator.next().value
if (newMessage.chatId == message.chatId) {
val sameSender = newMessage.senderUserId == message.senderUserId
val viaSameBot = newMessage.viaBotUserId == message.viaBotUserId
if ((fromBot && sameSender) || (viaBot && viaSameBot)) {
val newCont = newMessage.content
val cont = message.content
if (newCont is MessageOsmAndBotLocation && cont is MessageOsmAndBotLocation) {
if (newCont.name == cont.name) {
iterator.remove()
}
}
} else if (sameSender) {
iterator.remove()
}
}
}
}

View file

@ -52,10 +52,6 @@ object TelegramUiHelper {
placeholderId = R.drawable.img_user_picture
}
val type = chat.type
val message = messages.firstOrNull()
if (message != null) {
res.lastUpdated = helper.getLastUpdatedTime(message)
}
if (type is TdApi.ChatTypePrivate || type is TdApi.ChatTypeSecret) {
val userId = getUserIdFromChatType(type)
val chatWithBot = helper.isBot(userId)
@ -63,11 +59,15 @@ object TelegramUiHelper {
res.chatWithBot = chatWithBot
if (!chatWithBot) {
res.userId = userId
val content = message?.content
val message = messages.firstOrNull { it.viaBotUserId == 0 }
if (message != null) {
res.lastUpdated = helper.getLastUpdatedTime(message)
val content = message.content
if (content is TdApi.MessageLocation) {
res.latLon = LatLon(content.location.latitude, content.location.longitude)
}
}
}
} else if (type is TdApi.ChatTypeBasicGroup) {
res.placeholderId = R.drawable.img_group_picture
res.membersCount = helper.getBasicGroupFullInfo(type.basicGroupId)?.members?.size ?: 0

View file

@ -221,33 +221,34 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
for ((id, messages) in telegramHelper.getMessagesByChatIds()) {
telegramHelper.getChat(id)?.also { chat ->
res.add(TelegramUiHelper.chatToChatItem(telegramHelper, chat, messages))
if (needLocationItems(chat.type)) {
val type = chat.type
if (type is TdApi.ChatTypeBasicGroup || type is TdApi.ChatTypeSupergroup) {
res.addAll(convertToLocationItems(chat, messages))
} else if (type is TdApi.ChatTypePrivate) {
if (telegramHelper.isOsmAndBot(type.userId)) {
res.addAll(convertToLocationItems(chat, messages))
} else if (messages.firstOrNull { it.viaBotUserId != 0 } != null) {
res.addAll(convertToLocationItems(chat, messages, true))
}
}
}
}
adapter.items = res
}
private fun needLocationItems(type: TdApi.ChatType): Boolean {
return when (type) {
is TdApi.ChatTypeBasicGroup -> true
is TdApi.ChatTypeSupergroup -> true
is TdApi.ChatTypePrivate -> telegramHelper.isOsmAndBot(type.userId)
else -> false
}
}
private fun convertToLocationItems(
chat: TdApi.Chat,
messages: List<TdApi.Message>
messages: List<TdApi.Message>,
addOnlyViaBotMessages: Boolean = false
): List<LocationItem> {
val res = mutableListOf<LocationItem>()
messages.forEach { message ->
if (!addOnlyViaBotMessages || message.viaBotUserId != 0) {
TelegramUiHelper.messageToLocationItem(telegramHelper, chat, message)?.also {
res.add(it)
}
}
}
return res
}