Fix reference revalidation

This commit is contained in:
Ali 2022-03-30 14:46:38 +04:00
parent 30ecf17d89
commit 5517a731ae
3 changed files with 22 additions and 4 deletions

View File

@ -2,6 +2,7 @@ import Foundation
import SwiftSignalKit import SwiftSignalKit
import Postbox import Postbox
import TelegramApi import TelegramApi
import MtProtoKit
public final class NotificationSoundList: Equatable, Codable { public final class NotificationSoundList: Equatable, Codable {
public final class NotificationSound: Equatable, Codable { public final class NotificationSound: Equatable, Codable {
@ -187,11 +188,28 @@ func managedSynchronizeNotificationSoundList(postbox: Postbox, network: Network)
|> restart |> restart
} }
func _internal_saveNotificationSound(account: Account, file: TelegramMediaFile) -> Signal<Never, UploadNotificationSoundError> { func _internal_saveNotificationSound(account: Account, file: FileMediaReference) -> Signal<Never, UploadNotificationSoundError> {
guard let resource = file.resource as? CloudDocumentMediaResource else { guard let resource = file.media.resource as? CloudDocumentMediaResource else {
return .fail(.generic) return .fail(.generic)
} }
return account.network.request(Api.functions.account.saveRingtone(id: .inputDocument(id: resource.fileId, accessHash: resource.accessHash, fileReference: Buffer(data: resource.fileReference)), unsave: .boolFalse)) return account.network.request(Api.functions.account.saveRingtone(id: .inputDocument(id: resource.fileId, accessHash: resource.accessHash, fileReference: Buffer(data: resource.fileReference)), unsave: .boolFalse))
|> `catch` { error -> Signal<Api.Bool, MTRpcError> in
if error.errorDescription == "FILE_REFERENCE_EXPIRED" {
return revalidateMediaResourceReference(postbox: account.postbox, network: account.network, revalidationContext: account.mediaReferenceRevalidationContext, info: TelegramCloudMediaResourceFetchInfo(reference: file.abstract.resourceReference(file.media.resource), preferBackgroundReferenceRevalidation: false, continueInBackground: false), resource: file.media.resource)
|> mapError { _ -> MTRpcError in
return MTRpcError(errorCode: 500, errorDescription: "Internal")
}
|> mapToSignal { result -> Signal<Api.Bool, MTRpcError> in
guard let resource = result.updatedResource as? CloudDocumentMediaResource else {
return .fail(MTRpcError(errorCode: 500, errorDescription: "Internal"))
}
return account.network.request(Api.functions.account.saveRingtone(id: .inputDocument(id: resource.fileId, accessHash: resource.accessHash, fileReference: Buffer(data: resource.fileReference)), unsave: .boolFalse))
}
} else {
return .fail(error)
}
}
|> mapError { _ -> UploadNotificationSoundError in |> mapError { _ -> UploadNotificationSoundError in
return .generic return .generic
} }

View File

@ -696,7 +696,7 @@ public extension TelegramEngine {
} }
} }
public func saveNotificationSound(file: TelegramMediaFile) -> Signal<Never, UploadNotificationSoundError> { public func saveNotificationSound(file: FileMediaReference) -> Signal<Never, UploadNotificationSoundError> {
return _internal_saveNotificationSound(account: self.account, file: file) return _internal_saveNotificationSound(account: self.account, file: file)
} }

View File

@ -875,7 +875,7 @@ func contextMenuForChatPresentationInterfaceState(chatPresentationInterfaceState
actions.append(.action(ContextMenuActionItem(text: "Save for Notifications", icon: { theme in actions.append(.action(ContextMenuActionItem(text: "Save for Notifications", icon: { theme in
return generateTintedImage(image: UIImage(bundleImageName: "Chat/Context Menu/DownloadTone"), color: theme.actionSheet.primaryTextColor) return generateTintedImage(image: UIImage(bundleImageName: "Chat/Context Menu/DownloadTone"), color: theme.actionSheet.primaryTextColor)
}, action: { _, f in }, action: { _, f in
let _ = context.engine.peers.saveNotificationSound(file: file).start(completed: { let _ = context.engine.peers.saveNotificationSound(file: .message(message: MessageReference(message), media: file)).start(completed: {
//TODO:localize //TODO:localize
controllerInteraction.displayUndo(.notificationSoundAdded(title: "Sound added", text: "You can now use this sound as a notification tone in your [custom notification settings]().", action: { controllerInteraction.displayUndo(.notificationSoundAdded(title: "Sound added", text: "You can now use this sound as a notification tone in your [custom notification settings]().", action: {
controllerInteraction.navigationController()?.pushViewController(notificationsAndSoundsController(context: context, exceptionsList: nil)) controllerInteraction.navigationController()?.pushViewController(notificationsAndSoundsController(context: context, exceptionsList: nil))