diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index de9cd011a0..d4c45e5a4b 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -56,7 +56,7 @@ class TelegramHelper private constructor() { private val downloadUserFilesMap = ConcurrentHashMap() // value.content can be TdApi.MessageLocation or MessageOsmAndBotLocation - private val usersLocationMessages = ConcurrentHashMap() + private val usersLocationMessages = ConcurrentHashMap() private val usersFullInfo = ConcurrentHashMap() private val basicGroupsFullInfo = ConcurrentHashMap() @@ -103,13 +103,11 @@ class TelegramHelper private constructor() { fun getUser(id: Int) = users[id] - fun getUserMessage(user: TdApi.User) = usersLocationMessages[user.id] + fun getUserMessage(user: TdApi.User) = + usersLocationMessages.values.firstOrNull { it.senderUserId == user.id } - fun getMessageById(id: Long) = usersLocationMessages.values.firstOrNull { it.id == id } - - fun getChatMessages(chatTitle: String): List { - return usersLocationMessages.values.filter { chats[it.chatId]?.title == chatTitle } - } + fun getChatMessages(chatTitle: String) = + usersLocationMessages.values.filter { chats[it.chatId]?.title == chatTitle } fun getMessages() = usersLocationMessages.values.toList() @@ -363,7 +361,8 @@ class TelegramHelper private constructor() { if (oldContent is TdApi.MessageText) { message.content = parseOsmAndBotLocation(oldContent.text.text) } - usersLocationMessages[message.senderUserId] = message + removeOldMessages(message.senderUserId) + usersLocationMessages[message.id] = message val chatTitle = chats[message.chatId]?.title if (chatTitle != null) { incomingMessagesListeners.forEach { @@ -373,6 +372,16 @@ class TelegramHelper private constructor() { } } + private fun removeOldMessages(userId: Int) { + users[userId]?.also { user -> + if (user.username != OSMAND_BOT_USERNAME) { + usersLocationMessages.values.filter { it.senderUserId == userId }.forEach { + usersLocationMessages.remove(it.id) + } + } + } + } + /** * @chatId Id of the chat * @livePeriod Period for which the location can be updated, in seconds; should be between 60 and 86400 for a live location and 0 otherwise. @@ -829,7 +838,7 @@ class TelegramHelper private constructor() { } TdApi.UpdateMessageContent.CONSTRUCTOR -> { val updateMessageContent = obj as TdApi.UpdateMessageContent - val message = getMessageById(updateMessageContent.messageId) + val message = usersLocationMessages[updateMessageContent.messageId] if (message == null) { updateMessageContent.apply { requestMessage(chatId, messageId, this@TelegramHelper::addNewMessage)