Refactoring of receiving the location messages in progress

This commit is contained in:
Alex Sytnyk 2018-07-02 17:22:32 +03:00
parent 5144c91fd1
commit c5ce8cd2c5

View file

@ -24,6 +24,8 @@ class TelegramHelper private constructor() {
private const val CHAT_LIVE_USERS_LIMIT = 100 private const val CHAT_LIVE_USERS_LIMIT = 100
private const val IGNORED_ERROR_CODE = 406 private const val IGNORED_ERROR_CODE = 406
private const val UPDATE_LIVE_MESSAGES_INTERVAL_SEC = 30L private const val UPDATE_LIVE_MESSAGES_INTERVAL_SEC = 30L
private const val MESSAGE_ACTIVE_TIME_SEC = 24 * 60 * 60 // 24 hours
private const val OSMAND_BOT_USERNAME = "osmand_bot"
private var helper: TelegramHelper? = null private var helper: TelegramHelper? = null
@ -51,6 +53,9 @@ class TelegramHelper private constructor() {
private val usersLiveMessages = ConcurrentHashMap<Long, TdApi.Message>() private val usersLiveMessages = ConcurrentHashMap<Long, TdApi.Message>()
// Can contain TdApi.MessageLocation or TdApi.MessageText from osmand_bot
private val usersLocationMessages = ConcurrentHashMap<Long, TdApi.Message>()
private val usersFullInfo = ConcurrentHashMap<Int, TdApi.UserFullInfo>() private val usersFullInfo = ConcurrentHashMap<Int, TdApi.UserFullInfo>()
private val basicGroupsFullInfo = ConcurrentHashMap<Int, TdApi.BasicGroupFullInfo>() private val basicGroupsFullInfo = ConcurrentHashMap<Int, TdApi.BasicGroupFullInfo>()
private val supergroupsFullInfo = ConcurrentHashMap<Int, TdApi.SupergroupFullInfo>() private val supergroupsFullInfo = ConcurrentHashMap<Int, TdApi.SupergroupFullInfo>()
@ -606,6 +611,29 @@ class TelegramHelper private constructor() {
listener?.onTelegramStatusChanged(prevAuthState, newAuthState) listener?.onTelegramStatusChanged(prevAuthState, newAuthState)
} }
private fun isAppropriateMessage(message: TdApi.Message): Boolean {
if (message.isOutgoing || message.isChannelPost) {
return false
}
val lastEdited = Math.max(message.date, message.editDate)
if (TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) - lastEdited > MESSAGE_ACTIVE_TIME_SEC) {
return false
}
val content = message.content
return when (content) {
is TdApi.MessageLocation -> true
is TdApi.MessageText -> {
if (content.text.text.startsWith("{")) {
if (users[message.senderUserId]?.username == OSMAND_BOT_USERNAME) {
return true
}
}
false
}
else -> false
}
}
class OrderedChat internal constructor(internal val order: Long, internal val chatId: Long, internal val isChannel: Boolean) : Comparable<OrderedChat> { class OrderedChat internal constructor(internal val order: Long, internal val chatId: Long, internal val isChannel: Boolean) : Comparable<OrderedChat> {
override fun compareTo(other: OrderedChat): Int { override fun compareTo(other: OrderedChat): Int {
@ -789,8 +817,8 @@ class TelegramHelper private constructor() {
} }
TdApi.UpdateMessageContent.CONSTRUCTOR -> { TdApi.UpdateMessageContent.CONSTRUCTOR -> {
val updateMessageContent = obj as TdApi.UpdateMessageContent val updateMessageContent = obj as TdApi.UpdateMessageContent
val message = usersLiveMessages[updateMessageContent.messageId] val message = usersLocationMessages[updateMessageContent.messageId]
if (message != null && !message.isOutgoing) { if (message != null) {
synchronized(message) { synchronized(message) {
message.content = updateMessageContent.newContent message.content = updateMessageContent.newContent
} }
@ -800,14 +828,13 @@ class TelegramHelper private constructor() {
it.onReceiveChatLocationMessages(chatTitle, message) it.onReceiveChatLocationMessages(chatTitle, message)
} }
} }
} }
} }
TdApi.UpdateNewMessage.CONSTRUCTOR -> { TdApi.UpdateNewMessage.CONSTRUCTOR -> {
val updateNewMessage = obj as TdApi.UpdateNewMessage val updateNewMessage = obj as TdApi.UpdateNewMessage
val message = updateNewMessage.message val message = updateNewMessage.message
if (!message.isOutgoing && message.content is TdApi.MessageLocation) { if (isAppropriateMessage(message)) {
usersLiveMessages[message.id] = message usersLocationMessages[message.id] = message
val chatTitle = chats[message.chatId]?.title val chatTitle = chats[message.chatId]?.title
if (chatTitle != null) { if (chatTitle != null) {
incomingMessagesListeners.forEach { incomingMessagesListeners.forEach {