FIX: mention suppression was not working right (#538)

We were only suppressing non mentions, ones that become spans.

@sam in the test was not resolving to a mention cause the user
did not exist.

depends on: https://github.com/discourse/discourse/pull/26253 for tests to pass.
This commit is contained in:
Sam 2024-03-20 13:00:39 +11:00 committed by GitHub
parent 1a55d7f6e7
commit 41f1530078
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 11 deletions

View File

@ -237,6 +237,10 @@ Follow the provided writing composition instructions carefully and precisely ste
parsed
.css("a")
.each do |a|
if a["class"] == "mention"
a.inner_html = a.inner_html.sub("@", "")
next
end
href = a["href"]
if href.present? && (href.start_with?("#{Discourse.base_url}") || href.start_with?("/"))
begin
@ -255,13 +259,6 @@ Follow the provided writing composition instructions carefully and precisely ste
end
end
parsed
.css("span.mention")
.each do |mention|
no_at_username = mention.text.sub("@", "")
mention.replace("<a href='/u/#{no_at_username}' class='mention'>#{no_at_username}</a>")
end
parsed.to_html
end
end

View File

@ -9,7 +9,8 @@ module DiscourseAi
end
def correctly_configured?
SiteSetting.ai_anthropic_api_key.present?
SiteSetting.ai_anthropic_api_key.present? ||
DiscourseAi::Completions::Endpoints::AwsBedrock.correctly_configured?("claude-2")
end
def configuration_hint

View File

@ -82,8 +82,10 @@ module DiscourseAi
end
it "can suppress notifications by remapping content" do
user = Fabricate(:user)
markdown = <<~MD
@sam is a person
@#{user.username} is a person
[test1](/test) is an internal link
[test2](/test?1=2) is an internal link
[test3](https://example.com) is an external link
@ -117,7 +119,7 @@ module DiscourseAi
# note, magic surprise &amp; is correct HTML 5 representation
expected = <<~HTML
<p><a href="/u/sam" class="mention">sam</a> is a person<br>
<p><a class="mention" href="/u/#{user.username}">#{user.username}</a> is a person<br>
<a href="/test?silent=true">test1</a> is an internal link<br>
<a href="/test?1=2&amp;silent=true">test2</a> is an internal link<br>
<a href="https://example.com" rel="noopener nofollow ugc">test3</a> is an external link<br>
@ -127,7 +129,10 @@ module DiscourseAi
<a href="//%5B%5Btest?silent=true" rel="noopener nofollow ugc">test7</a> is a link with an invalid URL</p>
HTML
expect(report.ordered_posts.first.raw.strip).to eq(expected.strip)
post = report.ordered_posts.first
expect(post.mentions.to_a).to eq([])
expect(post.raw.strip).to eq(expected.strip)
end
it "can exclude tags" do