Stop requesting URL previews if the feature has been disabled on the homeserver.

This commit is contained in:
Doug
2021-10-13 12:51:15 +01:00
parent dbddc675a5
commit ef572d42fe
3 changed files with 25 additions and 1 deletions
@@ -15,6 +15,7 @@
//
import Foundation
import AFNetworking
enum URLPreviewServiceError: Error {
case missingResponse
@@ -74,7 +75,10 @@ class URLPreviewService: NSObject {
success(previewData)
}
}, failure: failure)
}, failure: { error in
self.checkForDisabledAPI(in: error)
failure(error)
})
}
/// Removes any cached preview data that has expired.
@@ -156,4 +160,21 @@ class URLPreviewService: NSObject {
return components?.url ?? url
}
/// Checks an error returned from `MXRestClient` to see whether the previews API
/// has been disabled on the homeserver. If this is true, link detection will be disabled
/// to prevent further requests being made and stop any previews loaders being presented.
private func checkForDisabledAPI(in error: Error?) {
// The error will contain a 404 and no matrix error code.
guard
let error = error as NSError?,
error.userInfo[kMXErrorCodeKey] == nil,
let response = error.userInfo[AFNetworkingOperationFailingURLResponseErrorKey] as? HTTPURLResponse
else { return }
if response.statusCode == 404 {
MXLog.debug("[URLPreviewService] Disabling link detection as homeserver does not support URL previews.")
MXKAppSettings.standard().enableBubbleComponentLinkDetection = false
}
}
}