Merge pull request #5003 from vector-im/doug/5002_stop_requesting_url_previews

Stop requesting URL previews if the homeserver has disabled the feature.
This commit is contained in:
Doug 2021-10-14 17:44:18 +01:00 committed by GitHub
commit fca940ed54
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 2 deletions

View file

@ -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.
@ -82,9 +86,11 @@ class URLPreviewService: NSObject {
store.removeExpiredItems()
}
/// Deletes all cached preview data and closed previews from the store.
/// Deletes all cached preview data and closed previews from the store,
/// re-enabling URL previews if they have been disabled by `checkForDisabledAPI`.
func clearStore() {
store.deleteAll()
MXKAppSettings.standard().enableBubbleComponentLinkDetection = true
}
/// Store the `eventId` and `roomId` of a closed preview.
@ -156,4 +162,20 @@ 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 we're looking for is a generic 404 and not a matrix error.
guard
!MXError.isMXError(error),
let response = MXHTTPOperation.urlResponse(fromError: error)
else { return }
if response.statusCode == 404 {
MXLog.debug("[URLPreviewService] Disabling link detection as homeserver does not support URL previews.")
MXKAppSettings.standard().enableBubbleComponentLinkDetection = false
}
}
}

View file

@ -1117,6 +1117,8 @@ NSString *const URLPreviewDidUpdateNotification = @"URLPreviewDidUpdateNotificat
});
} failure:^(NSError * _Nullable error) {
MXStrongifyAndReturnIfNil(self);
MXLogDebug(@"[RoomBubbleCellData] Failed to get url preview")
// Remove the loading URLPreviewView, indicate that the layout needs refreshing and send a notification for refresh

1
changelog.d/5002.change Normal file
View file

@ -0,0 +1 @@
URL Previews: Stop requesting URL previews if the feature has been disabled on the homeserver.