mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-24 07:05:35 +00:00
Fix error processing
This commit is contained in:
@@ -30,17 +30,7 @@ func _internal_translate(network: Network, text: String, fromLang: String?, toLa
|
||||
}
|
||||
|
||||
public enum EngineAudioTranscriptionResult {
|
||||
public struct Success {
|
||||
public var id: Int64
|
||||
public var text: String
|
||||
|
||||
public init(id: Int64, text: String) {
|
||||
self.id = id
|
||||
self.text = text
|
||||
}
|
||||
}
|
||||
|
||||
case success(Success)
|
||||
case success
|
||||
case error
|
||||
}
|
||||
|
||||
@@ -73,31 +63,38 @@ func _internal_transcribeAudio(postbox: Postbox, network: Network, audioTranscri
|
||||
return .single(nil)
|
||||
}
|
||||
|> mapToSignal { result -> Signal<EngineAudioTranscriptionResult, NoError> in
|
||||
guard let result = result else {
|
||||
return .single(.error)
|
||||
}
|
||||
|
||||
return postbox.transaction { transaction -> EngineAudioTranscriptionResult in
|
||||
switch result {
|
||||
case let .transcribedAudio(flags, transcriptionId, text):
|
||||
let isPending = (flags & (1 << 0)) != 0
|
||||
|
||||
if isPending {
|
||||
audioTranscriptionManager.with { audioTranscriptionManager in
|
||||
audioTranscriptionManager.addPendingMapping(transcriptionId: transcriptionId, messageId: messageId)
|
||||
let updatedAttribute: AudioTranscriptionMessageAttribute
|
||||
if let result = result {
|
||||
switch result {
|
||||
case let .transcribedAudio(flags, transcriptionId, text):
|
||||
let isPending = (flags & (1 << 0)) != 0
|
||||
|
||||
if isPending {
|
||||
audioTranscriptionManager.with { audioTranscriptionManager in
|
||||
audioTranscriptionManager.addPendingMapping(transcriptionId: transcriptionId, messageId: messageId)
|
||||
}
|
||||
}
|
||||
|
||||
updatedAttribute = AudioTranscriptionMessageAttribute(id: transcriptionId, text: text, isPending: isPending, didRate: false)
|
||||
}
|
||||
} else {
|
||||
updatedAttribute = AudioTranscriptionMessageAttribute(id: 0, text: "", isPending: false, didRate: false)
|
||||
}
|
||||
|
||||
transaction.updateMessage(messageId, update: { currentMessage in
|
||||
let storeForwardInfo = currentMessage.forwardInfo.flatMap(StoreMessageForwardInfo.init)
|
||||
var attributes = currentMessage.attributes.filter { !($0 is AudioTranscriptionMessageAttribute) }
|
||||
|
||||
attributes.append(AudioTranscriptionMessageAttribute(id: transcriptionId, text: text, isPending: isPending, didRate: false))
|
||||
|
||||
return .update(StoreMessage(id: currentMessage.id, globallyUniqueId: currentMessage.globallyUniqueId, groupingKey: currentMessage.groupingKey, threadId: currentMessage.threadId, timestamp: currentMessage.timestamp, flags: StoreMessageFlags(currentMessage.flags), tags: currentMessage.tags, globalTags: currentMessage.globalTags, localTags: currentMessage.localTags, forwardInfo: storeForwardInfo, authorId: currentMessage.author?.id, text: currentMessage.text, attributes: attributes, media: currentMessage.media))
|
||||
})
|
||||
transaction.updateMessage(messageId, update: { currentMessage in
|
||||
let storeForwardInfo = currentMessage.forwardInfo.flatMap(StoreMessageForwardInfo.init)
|
||||
var attributes = currentMessage.attributes.filter { !($0 is AudioTranscriptionMessageAttribute) }
|
||||
|
||||
return .success(EngineAudioTranscriptionResult.Success(id: transcriptionId, text: text))
|
||||
attributes.append(updatedAttribute)
|
||||
|
||||
return .update(StoreMessage(id: currentMessage.id, globallyUniqueId: currentMessage.globallyUniqueId, groupingKey: currentMessage.groupingKey, threadId: currentMessage.threadId, timestamp: currentMessage.timestamp, flags: StoreMessageFlags(currentMessage.flags), tags: currentMessage.tags, globalTags: currentMessage.globalTags, localTags: currentMessage.localTags, forwardInfo: storeForwardInfo, authorId: currentMessage.author?.id, text: currentMessage.text, attributes: attributes, media: currentMessage.media))
|
||||
})
|
||||
|
||||
if let _ = result {
|
||||
return .success
|
||||
} else {
|
||||
return .error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,11 +31,18 @@ private struct FetchControls {
|
||||
let cancel: () -> Void
|
||||
}
|
||||
|
||||
private func transcribedText(message: Message) -> EngineAudioTranscriptionResult? {
|
||||
private enum TranscribedText {
|
||||
case success(String)
|
||||
case error
|
||||
}
|
||||
|
||||
private func transcribedText(message: Message) -> TranscribedText? {
|
||||
for attribute in message.attributes {
|
||||
if let attribute = attribute as? AudioTranscriptionMessageAttribute {
|
||||
if !attribute.text.isEmpty || !attribute.isPending {
|
||||
return .success(EngineAudioTranscriptionResult.Success(id: attribute.id, text: attribute.text))
|
||||
if !attribute.text.isEmpty {
|
||||
return .success(attribute.text)
|
||||
} else {
|
||||
return .error
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -499,7 +506,7 @@ final class ChatMessageInteractiveFileNode: ASDisplayNode {
|
||||
var isVoice = false
|
||||
var audioDuration: Int32 = 0
|
||||
|
||||
let canTranscribe = arguments.associatedData.isPremium || arguments.context.sharedContext.immediateExperimentalUISettings.localTranscription
|
||||
let canTranscribe = arguments.associatedData.isPremium
|
||||
|
||||
let messageTheme = arguments.incoming ? arguments.presentationData.theme.theme.chat.message.incoming : arguments.presentationData.theme.theme.chat.message.outgoing
|
||||
|
||||
@@ -608,8 +615,8 @@ final class ChatMessageInteractiveFileNode: ASDisplayNode {
|
||||
|
||||
if let transcribedText = transcribedText, case .expanded = effectiveAudioTranscriptionState {
|
||||
switch transcribedText {
|
||||
case let .success(success):
|
||||
textString = NSAttributedString(string: success.text, font: textFont, textColor: messageTheme.primaryTextColor)
|
||||
case let .success(text):
|
||||
textString = NSAttributedString(string: text, font: textFont, textColor: messageTheme.primaryTextColor)
|
||||
case .error:
|
||||
let errorTextFont = Font.regular(floor(arguments.presentationData.fontSize.baseDisplaySize * 15.0 / 17.0))
|
||||
//TODO:localize
|
||||
|
||||
Reference in New Issue
Block a user