Fixed infinite LDAP queries loop in case it returns a result that doesn't match the request

This commit is contained in:
Sylvain Berfini 2025-10-20 11:29:48 +02:00
parent cca4b90043
commit a93f2f2d48

View file

@ -100,57 +100,63 @@ class ContactsManager
override fun onSearchResultsReceived(magicSearch: MagicSearch) {
reloadRemoteContactsJob?.cancel()
var queriedSipUri = ""
for ((key, value) in magicSearchMap.entries) {
if (value == magicSearch) {
queriedSipUri = key
}
}
val results = magicSearch.lastSearch
Log.i("$TAG [${results.size}] magic search results available")
Log.i(
"$TAG [${results.size}] magic search results available for query upon SIP URI [$queriedSipUri]"
)
var found = false
if (results.isNotEmpty()) {
val result = results.first {
it.friend != null
}
val result = results.first { it.friend != null }
if (result != null) {
val friend = result.friend!!
Log.i("$TAG Found matching friend in source [${result.sourceFlags}]")
found = true
val address = result.address?.asStringUriOnly().orEmpty()
if (address.isEmpty() || (queriedSipUri.isNotEmpty() && queriedSipUri != address)) {
Log.w("$TAG Received friend [${friend.name}] with SIP URI [$address] doesn't match queried SIP URI [$queriedSipUri]")
} else {
found = true
// Store friend in app's cache to be re-used in call history, conversations, etc...
val temporaryFriendList = getRemoteContactDirectoriesCacheFriendList()
temporaryFriendList.addFriend(friend)
newContactAdded(friend)
Log.i(
"$TAG Stored discovered friend [${friend.name}] in temporary friend list, for later use"
)
// Store friend in app's cache to be re-used in call history, conversations, etc...
val temporaryFriendList = getRemoteContactDirectoriesCacheFriendList()
temporaryFriendList.addFriend(friend)
newContactAdded(friend)
Log.i(
"$TAG Stored discovered friend [${friend.name}] in temporary friend list, for later use"
)
for (listener in listeners) {
listener.onContactFoundInRemoteDirectory(friend)
}
for (listener in listeners) {
listener.onContactFoundInRemoteDirectory(friend)
}
reloadRemoteContactsJob = coroutineScope.launch {
delay(DELAY_BEFORE_RELOADING_CONTACTS_AFTER_MAGIC_SEARCH_RESULT)
coreContext.postOnCoreThread {
Log.i("$TAG At least a new SIP address was discovered, reloading contacts")
conferenceAvatarMap.values.forEach(ContactAvatarModel::destroy)
conferenceAvatarMap.clear()
reloadRemoteContactsJob = coroutineScope.launch {
delay(DELAY_BEFORE_RELOADING_CONTACTS_AFTER_MAGIC_SEARCH_RESULT)
coreContext.postOnCoreThread {
Log.i("$TAG At least a new SIP address was discovered, reloading contacts")
conferenceAvatarMap.values.forEach(ContactAvatarModel::destroy)
conferenceAvatarMap.clear()
notifyContactsListChanged()
notifyContactsListChanged()
}
}
}
}
}
var foundKey = ""
for ((key, value) in magicSearchMap.entries) {
if (value == magicSearch) {
foundKey = key
}
}
if (foundKey.isNotEmpty()) {
magicSearchMap.remove(foundKey)
if (queriedSipUri.isNotEmpty()) {
magicSearchMap.remove(queriedSipUri)
if (!found) {
Log.i(
"$TAG SIP URI [$foundKey] wasn't found in remote directories, adding it to unknown list to prevent further queries"
"$TAG SIP URI [$queriedSipUri] wasn't found in remote directories, adding it to unknown list to prevent further queries"
)
unknownRemoteContactDirectoriesContactsMap.add(foundKey)
unknownRemoteContactDirectoriesContactsMap.add(queriedSipUri)
}
}
magicSearch.removeListener(this)