Skip to content

Commit

Permalink
Merge pull request #1785 from tactilenews/1784_fix_faulty_file_type_d…
Browse files Browse the repository at this point in the history
…etermination_logic

Fix faulty file type determination logic
  • Loading branch information
mattwr18 authored Feb 12, 2024
2 parents 38cbcd1 + 7055bc7 commit 7dc5331
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 33 deletions.
40 changes: 21 additions & 19 deletions app/components/chat_message/chat_message.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -59,34 +59,36 @@
<% end %>
<% end %>

<% if image? %>
<% if message.text.blank? %>
<% unless files.empty? %>
<% if image? %>
<% if message.text.blank? %>
<header class="ChatMessage-header">
<% if message.sender %>
<%= c 'avatar', record: message.sender, style: :small, class: "ChatMessage-avatar" %>
<% else %>
<%= c 'avatar', style: :small, class: "ChatMessage-avatar" %>
<% end %>
<%= c 'chat_message_photos', photos: image_files %>
</header>
<% else %>
<%= c 'chat_message_photos', photos: image_files %>
<% end %>
<% end %>

<% if audio? %>
<header class="ChatMessage-header">
<% if message.sender %>
<%= c 'avatar', record: message.sender, style: :small, class: "ChatMessage-avatar" %>
<% else %>
<%= c 'avatar', style: :small, class: "ChatMessage-avatar" %>
<% end %>
<%= c 'chat_message_photos', photos: files %>
<%= c 'chat_message_audio', audios: audio_files %>
</header>
<% else %>
<%= c 'chat_message_photos', photos: files %>
<% end %>
<% end %>

<% if audio? %>
<header class="ChatMessage-header">
<% if message.sender %>
<%= c 'avatar', record: message.sender, style: :small, class: "ChatMessage-avatar" %>
<% else %>
<%= c 'avatar', style: :small, class: "ChatMessage-avatar" %>
<% end %>
<%= c 'chat_message_audio', audio: audio %>
</header>
<% end %>

<% if video? %>
<%= c 'chat_message_video', videos: files %>
<% if video? %>
<%= c 'chat_message_video', videos: video_files %>
<% end %>
<% end %>

<footer class="ChatMessage-footer">
Expand Down
18 changes: 13 additions & 5 deletions app/components/chat_message/chat_message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,35 @@ def id
end

def audio?
files.first.attachment.blob.audio? unless files.empty?
files.any? { |file| file.attachment.blob.content_type.match? /audio/ }
end

def image?
files.first.attachment.blob.image? unless files.empty?
files.any? { |file| file.attachment.blob.content_type.match? /image/ }
end

def video?
files.first.attachment.blob.video? unless files.empty?
files.any? { |file| file.attachment.blob.content_type.match? /video/ }
end

def photos
message.photos
end

def image_files
files.select { |file| file.attachment.blob.content_type.match? /image/ }
end

def video_files
files.select { |file| file.attachment.blob.content_type.match? /video/ }
end

def files
message.files
end

def audio
files.first unless files.empty?
def audio_files
files.select { |file| file.attachment.blob.content_type.match? /audio/ }
end

def creator_name
Expand Down
10 changes: 6 additions & 4 deletions app/components/chat_message_audio/chat_message_audio.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<div class="ChatMessageAudio">
<audio controls>
<source src="<%= url_for(audio.attachment) %>" type="audio/ogg">
<%= t 'components.chat_message_audio.no_audio_support' %>
</audio>
<% audios.each do |audio| %>
<audio controls>
<source src="<%= url_for(audio.attachment) %>" type="audio/ogg">
<%= t 'components.chat_message_audio.no_audio_support' %>
</audio>
<% end %>
</div>
6 changes: 3 additions & 3 deletions app/components/chat_message_audio/chat_message_audio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

module ChatMessageAudio
class ChatMessageAudio < ApplicationComponent
def initialize(audio:, **)
def initialize(audios:, **)
super

@audio = audio
@audios = audios
end

private

attr_reader :audio
attr_reader :audios
end
end
4 changes: 2 additions & 2 deletions spec/components/chat_message_voice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
RSpec.describe ChatMessageAudio::ChatMessageAudio, type: :component do
subject { render_inline(described_class.new(**params)) }

let(:audio) { create(:file) }
let(:params) { { audio: audio } }
let(:audios) { [create(:file)] }
let(:params) { { audios: audios } }
it { should have_css('.ChatMessageAudio') }
end

0 comments on commit 7dc5331

Please sign in to comment.