Fix pending messages counter

This commit is contained in:
max-klaus 2020-01-26 15:01:23 +03:00
parent 1f7797da7e
commit e6c0d55c95
3 changed files with 57 additions and 116 deletions

View file

@ -13,10 +13,12 @@ import android.os.*
import android.util.Log
import android.widget.Toast
import net.osmand.PlatformUtil
import net.osmand.telegram.helpers.TelegramHelper.TelegramIncomingMessagesListener
import net.osmand.telegram.helpers.TelegramHelper.TelegramOutgoingMessagesListener
import net.osmand.telegram.TelegramSettings.ShareChatInfo
import net.osmand.telegram.helpers.TelegramHelper
import net.osmand.telegram.helpers.TelegramHelper.*
import net.osmand.telegram.notifications.TelegramNotification.NotificationType
import net.osmand.telegram.utils.AndroidUtils
import net.osmand.telegram.utils.OsmandLocationUtils
import org.drinkless.td.libcore.telegram.TdApi
import java.util.*
@ -369,8 +371,12 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis
app().settings.onDeleteLiveMessages(chatId, messages)
}
override fun onSendLiveLocationError(code: Int, message: String) {
override fun onSendLiveLocationError(code: Int, message: String, shareInfo: ShareChatInfo, messageType: Int) {
Log.d(PlatformUtil.TAG, "Send live location error: $code - $message")
when (messageType) {
TelegramHelper.MESSAGE_TYPE_TEXT -> shareInfo.pendingTdLibText--
TelegramHelper.MESSAGE_TYPE_MAP -> shareInfo.pendingTdLibMap--
}
}
companion object {

View file

@ -396,6 +396,9 @@ class TelegramSettings(private val app: TelegramApplication) {
} else if (state.constructor == TdApi.MessageSendingStateFailed.CONSTRUCTOR) {
shareInfo.hasSharingError = true
shareInfo.pendingMapMessage = false
if (!isOsmAndBot) {
shareInfo.pendingTdLibMap--
}
log.debug("updateShareInfo MAP ${message.id} MessageSendingStateFailed")
}
} else {
@ -427,6 +430,9 @@ class TelegramSettings(private val app: TelegramApplication) {
log.debug("updateShareInfo TEXT ${message.id} MessageSendingStateFailed")
shareInfo.hasSharingError = true
shareInfo.pendingTextMessage = false
if (!isOsmAndBot) {
shareInfo.pendingTdLibText--
}
}
} else {
shareInfo.currentTextMessageId = message.id
@ -531,8 +537,8 @@ class TelegramSettings(private val app: TelegramApplication) {
if (initTime && initSending) {
initializing = true
} else {
val textSharingError = !shareInfo.lastTextMessageProcessed && currentTime - shareInfo.lastSendTextMessageTime > WAITING_TDLIB_TIME
val mapSharingError = !shareInfo.lastMapMessageProcessed && currentTime - shareInfo.lastSendMapMessageTime > WAITING_TDLIB_TIME
val textSharingError = !shareInfo.lastTextMessageHandled && currentTime - shareInfo.lastSendTextMessageTime > WAITING_TDLIB_TIME
val mapSharingError = !shareInfo.lastMapMessageHandled && currentTime - shareInfo.lastSendMapMessageTime > WAITING_TDLIB_TIME
if (shareInfo.hasSharingError
|| (shareTypeValue == SHARE_TYPE_MAP_AND_TEXT && (textSharingError || mapSharingError))
|| textSharingError && (shareTypeValue == SHARE_TYPE_TEXT)
@ -1431,12 +1437,12 @@ class TelegramSettings(private val app: TelegramApplication) {
var lastSendTextMessageTime = -1
set(value) {
field = value
lastTextMessageProcessed = false
lastTextMessageHandled = false
}
var lastSendMapMessageTime = -1
set(value) {
field = value
lastMapMessageProcessed = false
lastMapMessageHandled = false
}
var sentMessages = 0
var pendingTdLibText = 0
@ -1447,8 +1453,8 @@ class TelegramSettings(private val app: TelegramApplication) {
var shouldSendViaBotMapMessage = false
var hasSharingError = false
var additionalActiveTime = ADDITIONAL_ACTIVE_TIME_VALUES_SEC[0]
var lastMapMessageProcessed = false
var lastTextMessageProcessed = false
var lastMapMessageHandled = false
var lastTextMessageHandled = false
fun getNextAdditionalActiveTime(): Long {
var index = ADDITIONAL_ACTIVE_TIME_VALUES_SEC.indexOf(additionalActiveTime)

View file

@ -6,6 +6,7 @@ import net.osmand.telegram.SHARE_TYPE_MAP
import net.osmand.telegram.SHARE_TYPE_MAP_AND_TEXT
import net.osmand.telegram.SHARE_TYPE_TEXT
import net.osmand.telegram.TelegramSettings
import net.osmand.telegram.TelegramSettings.ShareChatInfo
import net.osmand.telegram.helpers.TelegramHelper.TelegramAuthenticationParameterType.*
import net.osmand.telegram.utils.GRAYSCALE_PHOTOS_DIR
import net.osmand.telegram.utils.GRAYSCALE_PHOTOS_EXT
@ -43,6 +44,10 @@ class TelegramHelper private constructor() {
const val MAX_LOCATION_MESSAGE_HISTORY_SCAN_SEC = 60 * 60 * 24 // one day
const val MESSAGE_TYPE_MAP = 1
const val MESSAGE_TYPE_TEXT = 2
const val MESSAGE_TYPE_BOT = 3
private var helper: TelegramHelper? = null
val instance: TelegramHelper
@ -247,7 +252,7 @@ class TelegramHelper private constructor() {
interface TelegramOutgoingMessagesListener {
fun onUpdateMessages(messages: List<TdApi.Message>)
fun onDeleteMessages(chatId: Long, messages: List<Long>)
fun onSendLiveLocationError(code: Int, message: String)
fun onSendLiveLocationError(code: Int, message: String, shareInfo: ShareChatInfo, messageType: Int)
}
interface FullInfoUpdatesListener {
@ -487,7 +492,7 @@ class TelegramHelper private constructor() {
}
}
fun sendViaBotLocationMessage(userId: Int, shareInfo: TelegramSettings.ShareChatInfo, location: TdApi.Location, device: TelegramSettings.DeviceBot, shareType:String) {
fun sendViaBotLocationMessage(userId: Int, shareInfo: ShareChatInfo, location: TdApi.Location, device: TelegramSettings.DeviceBot, shareType:String) {
log.debug("sendViaBotLocationMessage - ${shareInfo.chatId}")
client?.send(TdApi.GetInlineQueryResults(userId, shareInfo.chatId, location, device.deviceName, "")) { obj ->
when (obj.constructor) {
@ -495,7 +500,6 @@ class TelegramHelper private constructor() {
val error = obj as TdApi.Error
if (error.code != IGNORED_ERROR_CODE) {
listener?.onTelegramError(error.code, error.message)
} else {
shareInfo.shouldSendViaBotTextMessage = true
shareInfo.shouldSendViaBotMapMessage = true
}
@ -508,7 +512,7 @@ class TelegramHelper private constructor() {
}
private fun sendViaBotMessageFromQueryResults(
shareInfo: TelegramSettings.ShareChatInfo,
shareInfo: ShareChatInfo,
inlineQueryResults: TdApi.InlineQueryResults,
deviceId: String,
shareType: String
@ -528,10 +532,10 @@ class TelegramHelper private constructor() {
}
}
resultArticles.forEach {
shareInfo.lastTextMessageProcessed = false
shareInfo.lastTextMessageHandled = false
client?.send(TdApi.SendInlineQueryResultMessage(shareInfo.chatId, 0, true,
true, inlineQueryResults.inlineQueryId, it.id, false)) { obj ->
handleTextLocationMessageUpdate(obj, shareInfo)
handleTextLocationMessageUpdate(obj, shareInfo, true)
}
}
}
@ -765,8 +769,8 @@ class TelegramHelper private constructor() {
fun createPrivateChatWithUser(
userId: Int,
shareInfo: TelegramSettings.ShareChatInfo,
shareChatsInfo: ConcurrentHashMap<Long, TelegramSettings.ShareChatInfo>
shareInfo: ShareChatInfo,
shareChatsInfo: ConcurrentHashMap<Long, ShareChatInfo>
) {
client?.send(TdApi.CreatePrivateChat(userId, false)) { obj ->
when (obj.constructor) {
@ -833,101 +837,24 @@ class TelegramHelper private constructor() {
}
}
fun stopSendingLiveLocationToChat(shareInfo: TelegramSettings.ShareChatInfo) {
fun stopSendingLiveLocationToChat(shareInfo: ShareChatInfo) {
if (shareInfo.currentMapMessageId != -1L && shareInfo.chatId != -1L) {
shareInfo.lastSendMapMessageTime = (System.currentTimeMillis() / 1000).toInt()
client?.send(
TdApi.EditMessageLiveLocation(shareInfo.chatId, shareInfo.currentMapMessageId, null, null)) { obj ->
handleMapLocationMessageUpdate(obj, shareInfo)
handleMapLocationMessageUpdate(obj, shareInfo, false)
}
}
needRefreshActiveLiveLocationMessages = true
}
fun stopSendingLiveLocationMessages(chatsShareInfo: Map<Long, TelegramSettings.ShareChatInfo>) {
fun stopSendingLiveLocationMessages(chatsShareInfo: Map<Long, ShareChatInfo>) {
chatsShareInfo.forEach { (_, chatInfo) ->
stopSendingLiveLocationToChat(chatInfo)
}
}
fun getActiveLiveLocationMessages(onComplete: (() -> Unit)?) {
requestingActiveLiveLocationMessages = true
client?.send(TdApi.GetActiveLiveLocationMessages()) { obj ->
when (obj.constructor) {
TdApi.Error.CONSTRUCTOR -> {
val error = obj as TdApi.Error
if (error.code != IGNORED_ERROR_CODE) {
needRefreshActiveLiveLocationMessages = true
outgoingMessagesListeners.forEach {
it.onSendLiveLocationError(error.code, error.message)
}
}
}
TdApi.Messages.CONSTRUCTOR -> {
val messages = (obj as TdApi.Messages).messages
if (messages.isNotEmpty()) {
log.debug("getActiveLiveLocationMessages: $messages")
outgoingMessagesListeners.forEach {
it.onUpdateMessages(messages.asList())
}
}
onComplete?.invoke()
}
else -> outgoingMessagesListeners.forEach {
it.onSendLiveLocationError(-1, "Receive wrong response from TDLib: $obj")
}
}
requestingActiveLiveLocationMessages = false
}
}
private fun recreateLiveLocationMessage(shareInfo: TelegramSettings.ShareChatInfo, content: TdApi.InputMessageContent) {
if (shareInfo.chatId != -1L) {
val array = LongArray(1)
if (content is TdApi.InputMessageLocation) {
array[0] = shareInfo.currentMapMessageId
} else if (content is TdApi.InputMessageText) {
array[0] = shareInfo.currentTextMessageId
}
if (array[0] != 0L) {
log.debug("recreateLiveLocationMessage - ${array[0]}")
client?.send(TdApi.DeleteMessages(shareInfo.chatId, array, true)) { obj ->
when (obj.constructor) {
TdApi.Ok.CONSTRUCTOR -> sendNewLiveLocationMessage(shareInfo, content)
TdApi.Error.CONSTRUCTOR -> {
val error = obj as TdApi.Error
if (error.code != IGNORED_ERROR_CODE) {
needRefreshActiveLiveLocationMessages = true
outgoingMessagesListeners.forEach {
it.onSendLiveLocationError(error.code, error.message)
}
}
}
}
}
}
}
needRefreshActiveLiveLocationMessages = true
}
private fun sendNewLiveLocationMessage(shareInfo: TelegramSettings.ShareChatInfo, content: TdApi.InputMessageContent) {
needRefreshActiveLiveLocationMessages = true
log.debug("sendNewLiveLocationMessage")
if (content is TdApi.InputMessageText) {
shareInfo.lastSendTextMessageTime = (System.currentTimeMillis() / 1000).toInt()
} else if (content is TdApi.InputMessageLocation) {
shareInfo.lastSendMapMessageTime = (System.currentTimeMillis() / 1000).toInt()
}
client?.send(TdApi.SendMessage(shareInfo.chatId, 0, false, true, null, content)) { obj ->
if (content is TdApi.InputMessageText) {
handleTextLocationMessageUpdate(obj, shareInfo)
} else if (content is TdApi.InputMessageLocation) {
handleMapLocationMessageUpdate(obj, shareInfo)
}
}
}
fun sendNewTextLocation(shareInfo: TelegramSettings.ShareChatInfo, content: TdApi.InputMessageText) {
fun sendNewTextLocation(shareInfo: ShareChatInfo, content: TdApi.InputMessageText) {
shareInfo.updateTextMessageId = 1
if (!shareInfo.pendingTextMessage) {
shareInfo.pendingTextMessage = true
@ -935,23 +862,23 @@ class TelegramHelper private constructor() {
shareInfo.lastSendTextMessageTime = (System.currentTimeMillis() / 1000).toInt()
log.error("sendNewTextLocation ${shareInfo.pendingTdLibText}")
client?.send(TdApi.SendMessage(shareInfo.chatId, 0, false, true, null, content)) { obj ->
handleTextLocationMessageUpdate(obj, shareInfo)
handleTextLocationMessageUpdate(obj, shareInfo, false)
}
}
}
fun editTextLocation(shareInfo: TelegramSettings.ShareChatInfo, content: TdApi.InputMessageText) {
fun editTextLocation(shareInfo: ShareChatInfo, content: TdApi.InputMessageText) {
if (shareInfo.currentTextMessageId != -1L) {
shareInfo.pendingTdLibText++
shareInfo.lastSendTextMessageTime = (System.currentTimeMillis() / 1000).toInt()
log.info("editTextLocation ${shareInfo.currentTextMessageId} pendingTdLibText: ${shareInfo.pendingTdLibText}")
client?.send(TdApi.EditMessageText(shareInfo.chatId, shareInfo.currentTextMessageId, null, content)) { obj ->
handleTextLocationMessageUpdate(obj, shareInfo)
handleTextLocationMessageUpdate(obj, shareInfo, false)
}
}
}
fun sendNewMapLocation(shareInfo: TelegramSettings.ShareChatInfo, locationMessage: LocationMessages.BufferMessage) {
fun sendNewMapLocation(shareInfo: ShareChatInfo, locationMessage: LocationMessages.BufferMessage) {
needRefreshActiveLiveLocationMessages = true
val location = TdApi.Location(locationMessage.lat, locationMessage.lon)
val livePeriod =
@ -967,12 +894,12 @@ class TelegramHelper private constructor() {
shareInfo.lastSendMapMessageTime = (System.currentTimeMillis() / 1000).toInt()
log.error("sendNewMapLocation ${shareInfo.pendingTdLibMap}")
client?.send(TdApi.SendMessage(shareInfo.chatId, 0, false, true, null, content)) { obj ->
handleMapLocationMessageUpdate(obj, shareInfo)
handleMapLocationMessageUpdate(obj, shareInfo, false)
}
}
}
fun editMapLocation(shareInfo: TelegramSettings.ShareChatInfo, locationMessage: LocationMessages.BufferMessage) {
fun editMapLocation(shareInfo: ShareChatInfo, locationMessage: LocationMessages.BufferMessage) {
needRefreshActiveLiveLocationMessages = true
val location = TdApi.Location(locationMessage.lat, locationMessage.lon)
if (shareInfo.currentMapMessageId!=-1L) {
@ -980,23 +907,24 @@ class TelegramHelper private constructor() {
shareInfo.lastSendMapMessageTime = (System.currentTimeMillis() / 1000).toInt()
log.info("editMapLocation ${shareInfo.currentMapMessageId} pendingTdLibMap: ${shareInfo.pendingTdLibMap}")
client?.send(TdApi.EditMessageLiveLocation(shareInfo.chatId, shareInfo.currentMapMessageId, null, location)) { obj ->
handleMapLocationMessageUpdate(obj, shareInfo)
handleMapLocationMessageUpdate(obj, shareInfo, false)
}
}
}
private fun handleMapLocationMessageUpdate(obj: TdApi.Object, shareInfo: TelegramSettings.ShareChatInfo) {
shareInfo.lastMapMessageProcessed = true
private fun handleMapLocationMessageUpdate(obj: TdApi.Object, shareInfo: ShareChatInfo, isBot: Boolean) {
shareInfo.lastMapMessageHandled = true
val messageType = if (isBot) MESSAGE_TYPE_BOT else MESSAGE_TYPE_MAP
when (obj.constructor) {
TdApi.Error.CONSTRUCTOR -> {
log.debug("handleMapLocationMessageUpdate - ERROR $obj")
val error = obj as TdApi.Error
needRefreshActiveLiveLocationMessages = true
shareInfo.pendingMapMessage = false
if (error.code != IGNORED_ERROR_CODE) {
shareInfo.hasSharingError = true
needRefreshActiveLiveLocationMessages = true
shareInfo.pendingMapMessage = false
outgoingMessagesListeners.forEach {
it.onSendLiveLocationError(error.code, error.message)
it.onSendLiveLocationError(error.code, error.message, shareInfo, messageType)
}
}
}
@ -1009,7 +937,7 @@ class TelegramHelper private constructor() {
shareInfo.pendingMapMessage = false
log.debug("handleTextLocationMessageUpdate - MessageSendingStateFailed")
outgoingMessagesListeners.forEach {
it.onSendLiveLocationError(-1, "Map location message ${obj.id} failed to send")
it.onSendLiveLocationError(-1, "Map location message ${obj.id} failed to send", shareInfo, messageType)
}
}
obj.sendingState?.constructor == TdApi.MessageSendingStatePending.CONSTRUCTOR -> {
@ -1033,17 +961,18 @@ class TelegramHelper private constructor() {
}
}
private fun handleTextLocationMessageUpdate(obj: TdApi.Object, shareInfo: TelegramSettings.ShareChatInfo) {
shareInfo.lastTextMessageProcessed = true
private fun handleTextLocationMessageUpdate(obj: TdApi.Object, shareInfo: ShareChatInfo, isBot: Boolean) {
shareInfo.lastTextMessageHandled = true
val messageType = if (isBot) MESSAGE_TYPE_BOT else MESSAGE_TYPE_TEXT
when (obj.constructor) {
TdApi.Error.CONSTRUCTOR -> {
log.debug("handleTextLocationMessageUpdate - ERROR")
val error = obj as TdApi.Error
shareInfo.pendingTextMessage = false
if (error.code != IGNORED_ERROR_CODE) {
shareInfo.hasSharingError = true
shareInfo.pendingTextMessage = false
outgoingMessagesListeners.forEach {
it.onSendLiveLocationError(error.code, error.message)
it.onSendLiveLocationError(error.code, error.message, shareInfo, messageType)
}
}
}
@ -1056,7 +985,7 @@ class TelegramHelper private constructor() {
needRefreshActiveLiveLocationMessages = true
log.debug("handleTextLocationMessageUpdate - MessageSendingStateFailed")
outgoingMessagesListeners.forEach {
it.onSendLiveLocationError(-1, "Text location message ${obj.id} failed to send")
it.onSendLiveLocationError(-1, "Text location message ${obj.id} failed to send", shareInfo, messageType)
}
}
obj.sendingState?.constructor == TdApi.MessageSendingStatePending.CONSTRUCTOR -> {