From eeb659a0dfbbf231fb8805c60ccdd6a1b719c3d3 Mon Sep 17 00:00:00 2001 From: mmrbnjd Date: Wed, 13 Aug 2025 18:37:26 +0330 Subject: [PATCH] ... --- Presentation/HushianWebApp/Pages/Chat.razor | 64 +++++++++++++------ .../Pages/FromUserSide/UserCP.razor | 17 +---- .../HushianWebApp/Service/ChatService.cs | 1 + 3 files changed, 47 insertions(+), 35 deletions(-) diff --git a/Presentation/HushianWebApp/Pages/Chat.razor b/Presentation/HushianWebApp/Pages/Chat.razor index 8471218..3d35580 100644 --- a/Presentation/HushianWebApp/Pages/Chat.razor +++ b/Presentation/HushianWebApp/Pages/Chat.razor @@ -236,9 +236,7 @@ مرورگر شما از پخش صدا پشتیبانی نمی‌کند. -
- @GetAudioDuration(msg.FileContent) -
+ } @if (!string.IsNullOrWhiteSpace(msg.text)) @@ -488,16 +486,55 @@ } async Task OnClickSendMsg() { - if (!string.IsNullOrEmpty(MsgInput) && ChatCurrent != null) + if ((!string.IsNullOrEmpty(MsgInput) || SelectedImageFile != null || RecordedAudioBytes != null) + && ChatCurrent != null) { Common.Enums.ConversationType type = CurrentUser.Role == "Company" ? Common.Enums.ConversationType.CU : Common.Enums.ConversationType.EU; - var geter= await chatService.ADDChatResponse(ChatCurrent.ID, MsgInput, type); - if(geter!=null) - {ChatCurrent?.Responses.Add(geter); + ChatItemResponseDto? model=null; + if (SelectedImageFile != null) + { + var bytes = SelectedImageBytes ?? Array.Empty(); + model = await chatService.ADDChatResponse( + ChatCurrent.ID, + MsgInput, + type, + SelectedImageFile.Name, + SelectedImageFile.ContentType, + bytes); + } + else if (RecordedAudioBytes != null) + { + // Send audio message + var fileName = $"audio_{DateTimeOffset.Now.ToUnixTimeSeconds()}.wav"; + model = await chatService.ADDChatResponse( + ChatCurrent.ID, + MsgInput, + type, + fileName, + "audio/wav", + RecordedAudioBytes); + } + else + { + model = await chatService.ADDChatResponse(ChatCurrent.ID, MsgInput, type); + } + if(model!=null) + { + ChatCurrent?.Responses.Add(model); ChatCurrent.LastText = MsgInput; await Task.Yield(); await JS.InvokeVoidAsync("scrollToBottom", "B1"); - MsgInput = string.Empty;} + MsgInput = string.Empty; + SelectedImageFile = null; + SelectedImageBytes = null; + SelectedImagePreview = null; + + // Clear recorded audio after sending + RecordedAudioBytes = null; + RecordedAudioUrl = null; + RecordedAudioDuration = "00:00"; + + } } } async Task onClickSelectedChat(int InboxID, ChatItemDto chatItem) @@ -605,18 +642,7 @@ => (string.IsNullOrWhiteSpace(fileType) || content == null || content.Length == 0) ? string.Empty : $"data:{fileType};base64,{Convert.ToBase64String(content)}"; - private string GetAudioDuration(byte[]? content) - { - // Simple duration calculation based on file size (approximate) - if (content == null || content.Length == 0) return "00:00"; - // Assuming 16-bit PCM at 44.1kHz, mono - var bytesPerSecond = 44100 * 2; // 44.1kHz * 2 bytes per sample - var durationSeconds = content.Length / bytesPerSecond; - var minutes = durationSeconds / 60; - var seconds = durationSeconds % 60; - return $"{minutes:D2}:{seconds:D2}"; - } // Audio recording methods private async Task ToggleAudioRecording() diff --git a/Presentation/HushianWebApp/Pages/FromUserSide/UserCP.razor b/Presentation/HushianWebApp/Pages/FromUserSide/UserCP.razor index ed9bd99..8ea6dfa 100644 --- a/Presentation/HushianWebApp/Pages/FromUserSide/UserCP.razor +++ b/Presentation/HushianWebApp/Pages/FromUserSide/UserCP.razor @@ -93,9 +93,6 @@ مرورگر شما از پخش صدا پشتیبانی نمی‌کند. -
- @GetAudioDuration(msg.FileContent) -
} @if (!string.IsNullOrWhiteSpace(msg.text)) @@ -302,7 +299,7 @@ value += "/" + CompanyGroups.FirstOrDefault(f => f.ID == GroupID.GetValueOrDefault()).Name; } - if (LastOpenChat != null) + if (LastOpenChat != null && LastOpenChat.Responses!=null) { var model = LastOpenChat.Responses.OrderBy(o => o.ID).LastOrDefault(l => l.Type != Common.Enums.ConversationType.UE); @@ -407,18 +404,6 @@ ? string.Empty : $"data:{fileType};base64,{Convert.ToBase64String(content)}"; - private string GetAudioDuration(byte[]? content) - { - // Simple duration calculation based on file size (approximate) - if (content == null || content.Length == 0) return "00:00"; - - // Assuming 16-bit PCM at 44.1kHz, mono - var bytesPerSecond = 44100 * 2; // 44.1kHz * 2 bytes per sample - var durationSeconds = content.Length / bytesPerSecond; - var minutes = durationSeconds / 60; - var seconds = durationSeconds % 60; - return $"{minutes:D2}:{seconds:D2}"; - } } @functions { async Task OnClickSendMsg() diff --git a/Presentation/HushianWebApp/Service/ChatService.cs b/Presentation/HushianWebApp/Service/ChatService.cs index 36a44e1..14f67d7 100644 --- a/Presentation/HushianWebApp/Service/ChatService.cs +++ b/Presentation/HushianWebApp/Service/ChatService.cs @@ -67,6 +67,7 @@ namespace HushianWebApp.Service public async Task ADDChatResponse(int conversationID, string text, ConversationType type, string? fileName, string? fileType, byte[]? fileContent) { + if (text == null) text = ""; var response = await _baseController.Post($"{BaseRoute}ADDChatResponse", new ADD_ConversationResponseDto() { ConversationID = conversationID,