Fix pending reset for text messages. Fix sending error detection.

This commit is contained in:
max-klaus 2020-01-25 22:10:01 +03:00
parent 4f9cfeed7b
commit 1f7797da7e
2 changed files with 28 additions and 10 deletions

View file

@ -108,7 +108,7 @@ private const val PROXY_ENABLED = "proxy_enabled"
private const val PROXY_PREFERENCES_KEY = "proxy_preferences" private const val PROXY_PREFERENCES_KEY = "proxy_preferences"
private const val SHARING_INITIALIZATION_TIME = 60 * 2L // 2 minutes private const val SHARING_INITIALIZATION_TIME = 60 * 2L // 2 minutes
private const val WAITING_TDLIB_TIME = 30 // 2 seconds private const val WAITING_TDLIB_TIME = 3 // 3 seconds
private const val GPS_UPDATE_EXPIRED_TIME = 60 * 3L // 3 minutes private const val GPS_UPDATE_EXPIRED_TIME = 60 * 3L // 3 minutes
@ -503,13 +503,15 @@ class TelegramSettings(private val app: TelegramApplication) {
private fun getNewSharingStatusHistoryItem(): SharingStatus { private fun getNewSharingStatusHistoryItem(): SharingStatus {
return SharingStatus().apply { return SharingStatus().apply {
statusChangeTime = System.currentTimeMillis() val currentTimeMillis = System.currentTimeMillis()
val currentTime = currentTimeMillis / 1000
statusChangeTime = currentTimeMillis
val lm = app.getSystemService(Context.LOCATION_SERVICE) as LocationManager val lm = app.getSystemService(Context.LOCATION_SERVICE) as LocationManager
val gpsEnabled = try { val gpsEnabled = try {
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) { if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
val loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER) val loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER)
val gpsActive = loc != null && ((statusChangeTime - loc.time) / 1000) < GPS_UPDATE_EXPIRED_TIME val gpsActive = loc != null && ((currentTimeMillis - loc.time) / 1000) < GPS_UPDATE_EXPIRED_TIME
val lastSentLocationExpired = ((statusChangeTime - app.shareLocationHelper.lastLocationUpdateTime) / 1000) > GPS_UPDATE_EXPIRED_TIME val lastSentLocationExpired = ((currentTimeMillis - app.shareLocationHelper.lastLocationUpdateTime) / 1000) > GPS_UPDATE_EXPIRED_TIME
(gpsActive || !lastSentLocationExpired) (gpsActive || !lastSentLocationExpired)
} else { } else {
false false
@ -522,12 +524,15 @@ class TelegramSettings(private val app: TelegramApplication) {
var sendChatsErrors = false var sendChatsErrors = false
shareChatsInfo.forEach { (_, shareInfo) -> shareChatsInfo.forEach { (_, shareInfo) ->
if (shareInfo.lastTextSuccessfulSendTime == -1L && shareInfo.lastMapSuccessfulSendTime == -1L val initTime = (currentTime - shareInfo.start) < SHARING_INITIALIZATION_TIME
&& ((statusChangeTime / 1000 - shareInfo.start) < SHARING_INITIALIZATION_TIME)) { var initSending = false
initSending = initSending || (shareInfo.lastTextSuccessfulSendTime == -1L && shareTypeValue != SHARE_TYPE_MAP)
initSending = initSending || (shareInfo.lastMapSuccessfulSendTime == -1L && shareTypeValue != SHARE_TYPE_TEXT)
if (initTime && initSending) {
initializing = true initializing = true
} else { } else {
val textSharingError = shareInfo.lastSendTextMessageTime - shareInfo.lastTextSuccessfulSendTime > WAITING_TDLIB_TIME val textSharingError = !shareInfo.lastTextMessageProcessed && currentTime - shareInfo.lastSendTextMessageTime > WAITING_TDLIB_TIME
val mapSharingError = shareInfo.lastSendMapMessageTime - shareInfo.lastMapSuccessfulSendTime > WAITING_TDLIB_TIME val mapSharingError = !shareInfo.lastMapMessageProcessed && currentTime - shareInfo.lastSendMapMessageTime > WAITING_TDLIB_TIME
if (shareInfo.hasSharingError if (shareInfo.hasSharingError
|| (shareTypeValue == SHARE_TYPE_MAP_AND_TEXT && (textSharingError || mapSharingError)) || (shareTypeValue == SHARE_TYPE_MAP_AND_TEXT && (textSharingError || mapSharingError))
|| textSharingError && (shareTypeValue == SHARE_TYPE_TEXT) || textSharingError && (shareTypeValue == SHARE_TYPE_TEXT)
@ -1424,7 +1429,15 @@ class TelegramSettings(private val app: TelegramApplication) {
var lastTextSuccessfulSendTime = -1L var lastTextSuccessfulSendTime = -1L
var lastMapSuccessfulSendTime = -1L var lastMapSuccessfulSendTime = -1L
var lastSendTextMessageTime = -1 var lastSendTextMessageTime = -1
set(value) {
field = value
lastTextMessageProcessed = false
}
var lastSendMapMessageTime = -1 var lastSendMapMessageTime = -1
set(value) {
field = value
lastMapMessageProcessed = false
}
var sentMessages = 0 var sentMessages = 0
var pendingTdLibText = 0 var pendingTdLibText = 0
var pendingTdLibMap = 0 var pendingTdLibMap = 0
@ -1434,6 +1447,8 @@ class TelegramSettings(private val app: TelegramApplication) {
var shouldSendViaBotMapMessage = false var shouldSendViaBotMapMessage = false
var hasSharingError = false var hasSharingError = false
var additionalActiveTime = ADDITIONAL_ACTIVE_TIME_VALUES_SEC[0] var additionalActiveTime = ADDITIONAL_ACTIVE_TIME_VALUES_SEC[0]
var lastMapMessageProcessed = false
var lastTextMessageProcessed = false
fun getNextAdditionalActiveTime(): Long { fun getNextAdditionalActiveTime(): Long {
var index = ADDITIONAL_ACTIVE_TIME_VALUES_SEC.indexOf(additionalActiveTime) var index = ADDITIONAL_ACTIVE_TIME_VALUES_SEC.indexOf(additionalActiveTime)

View file

@ -528,6 +528,7 @@ class TelegramHelper private constructor() {
} }
} }
resultArticles.forEach { resultArticles.forEach {
shareInfo.lastTextMessageProcessed = false
client?.send(TdApi.SendInlineQueryResultMessage(shareInfo.chatId, 0, true, client?.send(TdApi.SendInlineQueryResultMessage(shareInfo.chatId, 0, true,
true, inlineQueryResults.inlineQueryId, it.id, false)) { obj -> true, inlineQueryResults.inlineQueryId, it.id, false)) { obj ->
handleTextLocationMessageUpdate(obj, shareInfo) handleTextLocationMessageUpdate(obj, shareInfo)
@ -940,7 +941,7 @@ class TelegramHelper private constructor() {
} }
fun editTextLocation(shareInfo: TelegramSettings.ShareChatInfo, content: TdApi.InputMessageText) { fun editTextLocation(shareInfo: TelegramSettings.ShareChatInfo, content: TdApi.InputMessageText) {
if (shareInfo.currentTextMessageId!=-1L) { if (shareInfo.currentTextMessageId != -1L) {
shareInfo.pendingTdLibText++ shareInfo.pendingTdLibText++
shareInfo.lastSendTextMessageTime = (System.currentTimeMillis() / 1000).toInt() shareInfo.lastSendTextMessageTime = (System.currentTimeMillis() / 1000).toInt()
log.info("editTextLocation ${shareInfo.currentTextMessageId} pendingTdLibText: ${shareInfo.pendingTdLibText}") log.info("editTextLocation ${shareInfo.currentTextMessageId} pendingTdLibText: ${shareInfo.pendingTdLibText}")
@ -985,6 +986,7 @@ class TelegramHelper private constructor() {
} }
private fun handleMapLocationMessageUpdate(obj: TdApi.Object, shareInfo: TelegramSettings.ShareChatInfo) { private fun handleMapLocationMessageUpdate(obj: TdApi.Object, shareInfo: TelegramSettings.ShareChatInfo) {
shareInfo.lastMapMessageProcessed = true
when (obj.constructor) { when (obj.constructor) {
TdApi.Error.CONSTRUCTOR -> { TdApi.Error.CONSTRUCTOR -> {
log.debug("handleMapLocationMessageUpdate - ERROR $obj") log.debug("handleMapLocationMessageUpdate - ERROR $obj")
@ -1032,11 +1034,12 @@ class TelegramHelper private constructor() {
} }
private fun handleTextLocationMessageUpdate(obj: TdApi.Object, shareInfo: TelegramSettings.ShareChatInfo) { private fun handleTextLocationMessageUpdate(obj: TdApi.Object, shareInfo: TelegramSettings.ShareChatInfo) {
shareInfo.lastTextMessageProcessed = true
when (obj.constructor) { when (obj.constructor) {
TdApi.Error.CONSTRUCTOR -> { TdApi.Error.CONSTRUCTOR -> {
log.debug("handleTextLocationMessageUpdate - ERROR") log.debug("handleTextLocationMessageUpdate - ERROR")
val error = obj as TdApi.Error val error = obj as TdApi.Error
shareInfo.pendingMapMessage = false shareInfo.pendingTextMessage = false
if (error.code != IGNORED_ERROR_CODE) { if (error.code != IGNORED_ERROR_CODE) {
shareInfo.hasSharingError = true shareInfo.hasSharingError = true
outgoingMessagesListeners.forEach { outgoingMessagesListeners.forEach {