FEATURE: Add onebox support for chat threads (#23580)

With this commit we now support onboxes of:
- channel
- channel message
- thread
- thread message
This commit is contained in:
Jan Cernik
2023-10-25 09:30:39 -03:00
committed by GitHub
parent a546dcb0cc
commit 3f5a00e20f
9 changed files with 266 additions and 49 deletions

View File

@ -27,26 +27,25 @@ describe Chat::OneboxHandler do
onebox_html = Chat::OneboxHandler.handle(public_chat_url, { channel_id: public_channel.id })
expect(onebox_html).to match_html <<~HTML
<aside class="onebox chat-onebox">
<article class="onebox-body chat-onebox-body">
<h3 class="chat-onebox-title">
<a href="#{public_chat_url}">
<span class="category-chat-badge" style="color: ##{public_channel.chatable.color}">
<svg class="fa d-icon d-icon-d-chat svg-icon svg-string" xmlns="http://www.w3.org/2000/svg"><use href="#d-chat"></use></svg>
</span>
<span class="clear-badge">#{public_channel.name}</span>
</a>
</h3>
<div class="chat-onebox-members-count">1 member</div>
<div class="chat-onebox-members">
<a class="trigger-user-card" data-user-card="#{user.username}" aria-hidden="true" tabindex="-1">
<img loading="lazy" alt="#{user.username}" width="30" height="30" src="#{user.avatar_template_url.gsub("{size}", "60")}" class="avatar">
</a>
</div>
</article>
</aside>
HTML
<aside class="onebox chat-onebox">
<article class="onebox-body chat-onebox-body">
<h3 class="chat-onebox-title">
<a href="/chat/c/-/#{public_channel.id}">
<span class="category-chat-badge" style="color: ##{public_channel.chatable.color}">
<svg class="fa d-icon d-icon-d-chat svg-icon svg-string" xmlns="http://www.w3.org/2000/svg"><use href="#d-chat"></use></svg>
</span>
<span class="clear-badge">#{public_channel.name}</span>
</a>
</h3>
<div class="chat-onebox-members-count">1 member</div>
<div class="chat-onebox-members">
<a class="trigger-user-card" data-user-card="#{user.username}" aria-hidden="true" tabindex="-1">
<img loading="lazy" alt="#{user.username}" width="30" height="30" src="#{user.avatar_template_url.gsub("{size}", "60")}" class="avatar">
</a>
</div>
</article>
</aside>
HTML
end
end
@ -85,27 +84,28 @@ describe Chat::OneboxHandler do
)
expect(onebox_html).to match_html <<~HTML
<div class="chat-transcript" data-message-id="#{public_message.id}" data-username="#{user.username}" data-datetime="#{public_message.created_at.iso8601}" data-channel-name="#{public_channel.name}" data-channel-id="#{public_channel.id}">
<div class="chat-transcript-user">
<div class="chat-transcript-user-avatar">
<a class="trigger-user-card" data-user-card="#{user.username}" aria-hidden="true" tabindex="-1">
<img loading="lazy" alt="#{user.username}" width="20" height="20" src="#{user.avatar_template_url.gsub("{size}", "20")}" class="avatar">
</a>
</div>
<div class="chat-transcript-username">#{user.username}</div>
<div class="chat-transcript-datetime">
<a href="#{public_chat_url}/#{public_message.id}" title="#{public_message.created_at}">#{public_message.created_at}</a>
<div class="chat-transcript" data-message-id="#{public_message.id}" data-username="#{user.username}" data-datetime="#{public_message.created_at.iso8601}" data-channel-name="#{public_channel.name}" data-channel-id="#{public_channel.id}">
<div class="chat-transcript-user">
<div class="chat-transcript-user-avatar">
<a class="trigger-user-card" data-user-card="#{user.username}" aria-hidden="true" tabindex="-1">
<img loading="lazy" alt="#{user.username}" width="20" height="20" src="#{user.avatar_template_url.gsub("{size}", "20")}" class="avatar">
</a>
</div>
<div class="chat-transcript-username">#{user.username}</div>
<div class="chat-transcript-datetime">
<a href="/chat/c/-/#{public_channel.id}" title="#{public_message.created_at}">#{public_message.created_at}</a>
</div>
<a class="chat-transcript-channel" href="/chat/c/-/#{public_channel.id}">
<span class="category-chat-badge" style="color: ##{public_channel.chatable.color}">
<svg class="fa d-icon d-icon-d-chat svg-icon svg-string" xmlns="http://www.w3.org/2000/svg"><use href="#d-chat"></use></svg>
</span>
#{public_channel.name}
</a>
</div>
<a class="chat-transcript-channel" href="/chat/c/-/#{public_channel.id}">
<span class="category-chat-badge" style="color: ##{public_channel.chatable.color}">
<svg class="fa d-icon d-icon-d-chat svg-icon svg-string" xmlns="http://www.w3.org/2000/svg"><use href="#d-chat"></use></svg>
</span>
#{public_channel.name}
</a>
<div class="chat-transcript-messages"><p>Hello world!</p></div>
<div class="chat-transcript-images onebox"></div>
</div>
<div class="chat-transcript-messages"><p>Hello world!</p></div>
</div>
HTML
HTML
end
end
@ -133,4 +133,96 @@ describe Chat::OneboxHandler do
end
end
end
describe "chat thread" do
fab!(:original_public_message) do
Fabricate(:chat_message, user: user, chat_channel: public_channel, message: "Hello world!")
end
fab!(:public_thread) do
Fabricate(:chat_thread, channel: public_channel, original_message: original_public_message)
end
fab!(:private_thread) { Fabricate(:chat_thread, channel: private_channel) }
context "when valid" do
it "renders thread onebox" do
onebox_html =
Chat::OneboxHandler.handle(
"#{public_chat_url}/t/#{public_thread.id}",
{ channel_id: public_channel.id, thread_id: public_thread.id },
)
expect(onebox_html).to match_html <<~HTML
<aside class="onebox chat-onebox">
<article class="onebox-body chat-onebox-body">
<div class="chat-transcript-user">
<h3 class="chat-onebox-title">
<a href="/chat/c/-/#{public_channel.id}/t/#{public_thread.id}">
<span class="category-chat-badge" style="color: ##{public_channel.chatable.color}">
<svg class="fa d-icon d-icon-discourse-threads svg-icon svg-string" xmlns="http://www.w3.org/2000/svg"><use href="#discourse-threads"></use></svg>
</span>
<span class="clear-badge">#{public_thread.title}</span>
</a>
</h3>
<span class="thread-title-connector">in</span>
<a href="/chat/c/-/#{public_channel.id}">
<span class="category-chat-badge" style="color: ##{public_channel.chatable.color}">
<svg class="fa d-icon d-icon-d-chat svg-icon svg-string" xmlns="http://www.w3.org/2000/svg"><use href="#d-chat"></use></svg>
</span>
<span class="clear-badge">#{public_channel.name}</span>
</a>
</div>
<div class="chat-onebox-cooked"><p>Hello world!</p></div>
<div class="chat-onebox-images onebox"></div>
</article>
</aside>
HTML
end
end
context "when channel is private" do
it "does not create a onebox" do
onebox_html =
Chat::OneboxHandler.handle(
"#{private_chat_url}/t/#{private_thread.id}",
{ channel_id: private_channel.id, thread_id: public_thread.id },
)
expect(onebox_html).to be_nil
end
end
context "when thread does not exist" do
it "creates a channel onebox" do
public_channel.add(user)
Chat::Channel.ensure_consistency!
onebox_html =
Chat::OneboxHandler.handle(
"#{public_chat_url}/t/999",
{ channel_id: public_channel.id, thread_id: 999 },
)
expect(onebox_html).to match_html <<~HTML
<aside class="onebox chat-onebox">
<article class="onebox-body chat-onebox-body">
<h3 class="chat-onebox-title">
<a href="/chat/c/-/#{public_channel.id}">
<span class="category-chat-badge" style="color: ##{public_channel.chatable.color}">
<svg class="fa d-icon d-icon-d-chat svg-icon svg-string" xmlns="http://www.w3.org/2000/svg"><use href="#d-chat"></use></svg>
</span>
<span class="clear-badge">#{public_channel.name}</span>
</a>
</h3>
<div class="chat-onebox-members-count">1 member</div>
<div class="chat-onebox-members">
<a class="trigger-user-card" data-user-card="#{user.username}" aria-hidden="true" tabindex="-1">
<img loading="lazy" alt="#{user.username}" width="30" height="30" src="#{user.avatar_template_url.gsub("{size}", "60")}" class="avatar">
</a>
</div>
</article>
</aside>
HTML
end
end
end
end