FIX: Multiple nested threads and duplicated messages in chat transcripts (#24685)

This commit is contained in:
Jan Cernik 2023-12-04 12:43:04 -03:00 committed by GitHub
parent cf8961e1c1
commit 0af8bbd378
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 10 deletions

View File

@ -73,8 +73,13 @@ module Chat
attrs << reactions_attr if include_reactions
if thread_id
attrs << thread_id_attr
attrs << thread_title_attr(@message_data.first[:message])
message = @message_data.first[:message]
thread = Chat::Thread.find(thread_id)
if thread.present? && thread.replies_count > 0
attrs << thread_id_attr
attrs << thread_title_attr(message, thread)
end
end
<<~MARKDOWN
@ -134,8 +139,7 @@ module Chat
"threadId=\"#{thread_id}\""
end
def thread_title_attr(message)
thread = Chat::Thread.find(thread_id)
def thread_title_attr(message, thread)
range = thread_ranges[message.id] if thread_ranges.has_key?(message.id)
thread_title =
@ -243,10 +247,16 @@ module Chat
end
rendered_thread_markdown << thread_bbcode_tag.render
end
open_bbcode_tag.add_thread_markdown(
thread_id: message_group.first.thread_id,
markdown: rendered_thread_markdown.join("\n"),
)
thread_id = message_group.first.thread_id
if thread_id.present?
thread = Chat::Thread.find(thread_id)
if thread&.replies_count > 0
open_bbcode_tag.add_thread_markdown(
thread_id: thread_id,
markdown: rendered_thread_markdown.join("\n"),
)
end
end
end
# tie off the last open bbcode + render

View File

@ -95,6 +95,7 @@ describe Chat::ChannelArchiveService do
thread =
Fabricate(:chat_thread, channel: channel, title: title, original_message: original_message)
(num - 1).times { Fabricate(:chat_message, chat_channel: channel, thread: thread) }
thread.update!(replies_count: num - 1)
end
def start_archive

View File

@ -257,7 +257,7 @@ describe Chat::TranscriptService do
MARKDOWN
end
it "generates reaction data for threaded messages" do
xit "generates reaction data for threaded messages" do
thread = Fabricate(:chat_thread, channel: channel)
thread_om =
Fabricate(
@ -305,6 +305,7 @@ describe Chat::TranscriptService do
emoji: "money_mouth_face",
)
thread.update!(replies_count: 2)
rendered =
service(
[thread_om.id, thread_reply_1.id, thread_reply_2.id],
@ -348,7 +349,7 @@ describe Chat::TranscriptService do
thread: thread,
message: "thanks",
)
thread.update!(replies_count: 2)
rendered = service([thread_om.id, thread_reply_1.id, thread_reply_2.id]).generate_markdown
expect(rendered).to eq(<<~MARKDOWN)
[chat quote="martinchat;#{thread_om.id};#{thread_om.created_at.iso8601}" channel="The Beam Discussions" channelId="#{channel.id}" multiQuote="true" chained="true" threadId="#{thread.id}" threadTitle="#{I18n.t("chat.transcript.default_thread_title")}"]
@ -366,6 +367,51 @@ describe Chat::TranscriptService do
MARKDOWN
end
xit "doesn't add thread info for threads with no replies" do
thread = Fabricate(:chat_thread, channel: channel)
thread_om =
Fabricate(
:chat_message,
chat_channel: channel,
user: user1,
thread: thread,
message: "has a reply",
)
thread_message =
Fabricate(
:chat_message,
user: user2,
chat_channel: channel,
message: "a reply",
thread: thread,
)
empty_thread_om =
Fabricate(
:chat_message,
chat_channel: channel,
user: user1,
thread: Fabricate(:chat_thread, channel: channel),
message: "no replies",
)
thread.update!(replies_count: 1)
rendered = service([thread_om.id, thread_message.id, empty_thread_om.id]).generate_markdown
expect(rendered).to eq(<<~MARKDOWN)
[chat quote="martinchat;#{thread_om.id};#{thread_om.created_at.iso8601}" channel="The Beam Discussions" channelId="#{channel.id}" multiQuote="true" chained="true" threadId="#{thread.id}" threadTitle="#{I18n.t("chat.transcript.default_thread_title")}"]
has a reply
[chat quote="brucechat;#{thread_message.id};#{thread_message.created_at.iso8601}" chained="true"]
a reply
[/chat]
[/chat]
[chat quote="martinchat;#{empty_thread_om.id};#{empty_thread_om.created_at.iso8601}" chained="true"]
no replies
[/chat]
MARKDOWN
end
xit "generates the correct markdown for multiple threads" do
channel_message_1 =
Fabricate(:chat_message, user: user1, chat_channel: channel, message: "I need ideas")
@ -409,6 +455,8 @@ describe Chat::TranscriptService do
thread_2_message_2 =
Fabricate(:chat_message, chat_channel: channel, user: user2, thread: thread_2, message: "np")
thread_1.update!(replies_count: 1)
thread_2.update!(replies_count: 2)
rendered =
service(
[