FEATURE: Cache embed contents in the database (#25133)

* FEATURE: Cache embed contents in the database

This will be useful for features that rely on the semantic content of topics, like the many AI features



Co-authored-by: Roman Rizzi <rizziromanalejandro@gmail.com>
This commit is contained in:
Rafael dos Santos Silva
2024-01-05 10:09:31 -03:00
committed by GitHub
parent ac4d90b3a6
commit 13735f35fb
3 changed files with 64 additions and 10 deletions

View File

@ -73,6 +73,9 @@ RSpec.describe TopicEmbed do
expect(post.cooked).to have_tag("a", with: { href: "http://eviltrout.com/hello" })
expect(post.cooked).to have_tag("img", with: { src: "http://eviltrout.com/images/wat.jpg" })
# It caches the embed content
expect(post.topic.topic_embed.embed_content_cache).to eq(contents)
# It converts relative URLs to absolute when expanded
stub_request(:get, url).to_return(status: 200, body: contents)
expect(TopicEmbed.expanded_for(post)).to have_tag(
@ -107,6 +110,13 @@ RSpec.describe TopicEmbed do
expect(topic_embed.post.topic.user).to eq(new_user)
end
it "Supports updating the embed content cache" do
expect do TopicEmbed.import(user, url, title, "new contents") end.to change {
topic_embed.reload.embed_content_cache
}
expect(topic_embed.embed_content_cache).to eq("new contents")
end
it "Should leave uppercase Feed Entry URL untouched in content" do
cased_url = "http://eviltrout.com/ABCD"
post = TopicEmbed.import(user, cased_url, title, "some random content")
@ -559,4 +569,30 @@ RSpec.describe TopicEmbed do
expect(html).to eq(expected_html)
end
end
describe ".expanded_for" do
fab!(:user)
let(:title) { "How to turn a fish from good to evil in 30 seconds" }
let(:url) { "http://eviltrout.com/123" }
let(:contents) { "<p>hello world new post :D</p>" }
fab!(:embeddable_host)
fab!(:category)
fab!(:tag)
it "returns embed content" do
stub_request(:get, url).to_return(status: 200, body: contents)
post = TopicEmbed.import(user, url, title, contents)
expect(TopicEmbed.expanded_for(post)).to include(contents)
end
it "updates the embed content cache" do
stub_request(:get, url)
.to_return(status: 200, body: contents)
.then
.to_return(status: 200, body: "contents changed")
post = TopicEmbed.import(user, url, title, contents)
TopicEmbed.expanded_for(post)
expect(post.topic.topic_embed.reload.embed_content_cache).to include("contents changed")
end
end
end