Fix error processing

This commit is contained in:
Ali
2022-05-31 01:24:41 +04:00
parent 7b2ead9b23
commit 4c19ffb361
2 changed files with 41 additions and 37 deletions

View File

@@ -30,17 +30,7 @@ func _internal_translate(network: Network, text: String, fromLang: String?, toLa
} }
public enum EngineAudioTranscriptionResult { public enum EngineAudioTranscriptionResult {
public struct Success { case 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 error case error
} }
@@ -73,31 +63,38 @@ func _internal_transcribeAudio(postbox: Postbox, network: Network, audioTranscri
return .single(nil) return .single(nil)
} }
|> mapToSignal { result -> Signal<EngineAudioTranscriptionResult, NoError> in |> mapToSignal { result -> Signal<EngineAudioTranscriptionResult, NoError> in
guard let result = result else {
return .single(.error)
}
return postbox.transaction { transaction -> EngineAudioTranscriptionResult in return postbox.transaction { transaction -> EngineAudioTranscriptionResult in
switch result { let updatedAttribute: AudioTranscriptionMessageAttribute
case let .transcribedAudio(flags, transcriptionId, text): if let result = result {
let isPending = (flags & (1 << 0)) != 0 switch result {
case let .transcribedAudio(flags, transcriptionId, text):
if isPending { let isPending = (flags & (1 << 0)) != 0
audioTranscriptionManager.with { audioTranscriptionManager in
audioTranscriptionManager.addPendingMapping(transcriptionId: transcriptionId, messageId: messageId) 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 transaction.updateMessage(messageId, update: { currentMessage in
let storeForwardInfo = currentMessage.forwardInfo.flatMap(StoreMessageForwardInfo.init) let storeForwardInfo = currentMessage.forwardInfo.flatMap(StoreMessageForwardInfo.init)
var attributes = currentMessage.attributes.filter { !($0 is AudioTranscriptionMessageAttribute) } 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))
})
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
} }
} }
} }

View File

@@ -31,11 +31,18 @@ private struct FetchControls {
let cancel: () -> Void 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 { for attribute in message.attributes {
if let attribute = attribute as? AudioTranscriptionMessageAttribute { if let attribute = attribute as? AudioTranscriptionMessageAttribute {
if !attribute.text.isEmpty || !attribute.isPending { if !attribute.text.isEmpty {
return .success(EngineAudioTranscriptionResult.Success(id: attribute.id, text: attribute.text)) return .success(attribute.text)
} else {
return .error
} }
} }
} }
@@ -499,7 +506,7 @@ final class ChatMessageInteractiveFileNode: ASDisplayNode {
var isVoice = false var isVoice = false
var audioDuration: Int32 = 0 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 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 { if let transcribedText = transcribedText, case .expanded = effectiveAudioTranscriptionState {
switch transcribedText { switch transcribedText {
case let .success(success): case let .success(text):
textString = NSAttributedString(string: success.text, font: textFont, textColor: messageTheme.primaryTextColor) textString = NSAttributedString(string: text, font: textFont, textColor: messageTheme.primaryTextColor)
case .error: case .error:
let errorTextFont = Font.regular(floor(arguments.presentationData.fontSize.baseDisplaySize * 15.0 / 17.0)) let errorTextFont = Font.regular(floor(arguments.presentationData.fontSize.baseDisplaySize * 15.0 / 17.0))
//TODO:localize //TODO:localize