Merge pull request #5627 from osmandapp/fix_telegram_helper
Fix telegram helper
This commit is contained in:
commit
114c951d31
1 changed files with 61 additions and 56 deletions
|
@ -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
|
||||||
|
|
||||||
|
@ -49,7 +51,8 @@ class TelegramHelper private constructor() {
|
||||||
private val downloadChatFilesMap = ConcurrentHashMap<String, TdApi.Chat>()
|
private val downloadChatFilesMap = ConcurrentHashMap<String, TdApi.Chat>()
|
||||||
private val downloadUserFilesMap = ConcurrentHashMap<String, TdApi.User>()
|
private val downloadUserFilesMap = ConcurrentHashMap<String, TdApi.User>()
|
||||||
|
|
||||||
private val usersLiveMessages = ConcurrentHashMap<Long, TdApi.Message>()
|
// Can contain TdApi.MessageLocation or TdApi.MessageText from osmand_bot
|
||||||
|
private val usersLocationMessages = ConcurrentHashMap<Int, 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>()
|
||||||
|
@ -102,18 +105,13 @@ class TelegramHelper private constructor() {
|
||||||
return users[id]
|
return users[id]
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getUserMessage(user: TdApi.User): TdApi.Message? {
|
fun getUserMessage(user: TdApi.User) = usersLocationMessages[user.id]
|
||||||
for (message in usersLiveMessages.values) {
|
|
||||||
if (message.senderUserId == user.id) {
|
fun getMessageById(id: Long) = usersLocationMessages.values.firstOrNull { it.id == id }
|
||||||
return message
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getChatMessages(chatTitle: String): List<TdApi.Message> {
|
fun getChatMessages(chatTitle: String): List<TdApi.Message> {
|
||||||
val res = mutableListOf<TdApi.Message>()
|
val res = mutableListOf<TdApi.Message>()
|
||||||
for (message in usersLiveMessages.values) {
|
for (message in usersLocationMessages.values) {
|
||||||
val title = chats[message.chatId]?.title
|
val title = chats[message.chatId]?.title
|
||||||
if (title == chatTitle) {
|
if (title == chatTitle) {
|
||||||
res.add(message)
|
res.add(message)
|
||||||
|
@ -123,12 +121,12 @@ class TelegramHelper private constructor() {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getMessages(): List<TdApi.Message> {
|
fun getMessages(): List<TdApi.Message> {
|
||||||
return usersLiveMessages.values.toList()
|
return usersLocationMessages.values.toList()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getMessagesByChatIds(): Map<Long, List<TdApi.Message>> {
|
fun getMessagesByChatIds(): Map<Long, List<TdApi.Message>> {
|
||||||
val res = mutableMapOf<Long, MutableList<TdApi.Message>>()
|
val res = mutableMapOf<Long, MutableList<TdApi.Message>>()
|
||||||
for (message in usersLiveMessages.values) {
|
for (message in usersLocationMessages.values) {
|
||||||
var messages = res[message.chatId]
|
var messages = res[message.chatId]
|
||||||
if (messages != null) {
|
if (messages != null) {
|
||||||
messages.add(message)
|
messages.add(message)
|
||||||
|
@ -356,40 +354,30 @@ class TelegramHelper private constructor() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
updateChatTitles()
|
updateChatTitles()
|
||||||
getChatRecentLocationMessages(chatTitles.keys)
|
|
||||||
listener?.onTelegramChatsRead()
|
listener?.onTelegramChatsRead()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getChatRecentLocationMessages(chatTitles: Set<String>) {
|
private fun requestMessage(chatId: Long, messageId: Long, onComplete: (TdApi.Message) -> Unit) {
|
||||||
if (haveAuthorization) {
|
client?.send(TdApi.GetMessage(chatId, messageId)) { obj ->
|
||||||
for (chatTitle in chatTitles) {
|
|
||||||
val chatId = this.chatTitles[chatTitle]
|
|
||||||
if (chatId != null) {
|
|
||||||
val chat = chats[chatId]
|
|
||||||
if (chat == null || isChannel(chat)) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
client?.send(TdApi.SearchChatRecentLocationMessages(chatId, CHAT_LIVE_USERS_LIMIT)) { obj ->
|
|
||||||
when (obj.constructor) {
|
when (obj.constructor) {
|
||||||
TdApi.Error.CONSTRUCTOR -> {
|
TdApi.Error.CONSTRUCTOR -> {
|
||||||
val error = obj as TdApi.Error
|
val error = obj as TdApi.Error
|
||||||
val code = error.code
|
if (error.code != IGNORED_ERROR_CODE) {
|
||||||
if (code != IGNORED_ERROR_CODE && code != 400) {
|
listener?.onTelegramError(error.code, error.message)
|
||||||
listener?.onTelegramError(code, error.message)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TdApi.Messages.CONSTRUCTOR -> {
|
TdApi.Message.CONSTRUCTOR -> onComplete(obj as TdApi.Message)
|
||||||
val messages = (obj as TdApi.Messages).messages
|
|
||||||
for (message in messages) {
|
|
||||||
if (!message.isOutgoing && message.content is TdApi.MessageLocation) {
|
|
||||||
usersLiveMessages[message.id] = message
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
incomingMessagesListeners.forEach { it.onReceiveChatLocationMessages(chatTitle, *messages) }
|
|
||||||
}
|
|
||||||
else -> listener?.onTelegramError(-1, "Receive wrong response from TDLib: $obj")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun addNewMessage(message: TdApi.Message) {
|
||||||
|
if (message.isAppropriate()) {
|
||||||
|
usersLocationMessages[message.senderUserId] = message
|
||||||
|
val chatTitle = chats[message.chatId]?.title
|
||||||
|
if (chatTitle != null) {
|
||||||
|
incomingMessagesListeners.forEach {
|
||||||
|
it.onReceiveChatLocationMessages(chatTitle, message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -606,6 +594,30 @@ class TelegramHelper private constructor() {
|
||||||
listener?.onTelegramStatusChanged(prevAuthState, newAuthState)
|
listener?.onTelegramStatusChanged(prevAuthState, newAuthState)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun TdApi.Message.isAppropriate(): Boolean {
|
||||||
|
if (isOutgoing || isChannelPost) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
val lastEdited = Math.max(date, editDate)
|
||||||
|
if (TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()) - lastEdited > MESSAGE_ACTIVE_TIME_SEC) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
val content = content
|
||||||
|
return when (content) {
|
||||||
|
is TdApi.MessageLocation -> true
|
||||||
|
is TdApi.MessageText -> {
|
||||||
|
if (content.text.text.startsWith("{")) {
|
||||||
|
// TODO: get user from library if null
|
||||||
|
if (users[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 +801,12 @@ 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 = getMessageById(updateMessageContent.messageId)
|
||||||
if (message != null && !message.isOutgoing) {
|
if (message == null) {
|
||||||
|
updateMessageContent.apply {
|
||||||
|
requestMessage(chatId, messageId, this@TelegramHelper::addNewMessage)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
synchronized(message) {
|
synchronized(message) {
|
||||||
message.content = updateMessageContent.newContent
|
message.content = updateMessageContent.newContent
|
||||||
}
|
}
|
||||||
|
@ -800,21 +816,10 @@ class TelegramHelper private constructor() {
|
||||||
it.onReceiveChatLocationMessages(chatTitle, message)
|
it.onReceiveChatLocationMessages(chatTitle, message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TdApi.UpdateNewMessage.CONSTRUCTOR -> {
|
TdApi.UpdateNewMessage.CONSTRUCTOR -> {
|
||||||
val updateNewMessage = obj as TdApi.UpdateNewMessage
|
addNewMessage((obj as TdApi.UpdateNewMessage).message)
|
||||||
val message = updateNewMessage.message
|
|
||||||
if (!message.isOutgoing && message.content is TdApi.MessageLocation) {
|
|
||||||
usersLiveMessages[message.id] = message
|
|
||||||
val chatTitle = chats[message.chatId]?.title
|
|
||||||
if (chatTitle != null) {
|
|
||||||
incomingMessagesListeners.forEach {
|
|
||||||
it.onReceiveChatLocationMessages(chatTitle, message)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
TdApi.UpdateMessageMentionRead.CONSTRUCTOR -> {
|
TdApi.UpdateMessageMentionRead.CONSTRUCTOR -> {
|
||||||
val updateChat = obj as TdApi.UpdateMessageMentionRead
|
val updateChat = obj as TdApi.UpdateMessageMentionRead
|
||||||
|
|
Loading…
Reference in a new issue