FIX: Fetch categories data using specific endpoint (#543)

It used to fetch it from /site.json, but /categories.json is the more
appropriate one. This one also implements pagination, so we have to do
one request per page.
This commit is contained in:
Bianca Nenciu 2024-04-08 11:33:20 +03:00 committed by GitHub
parent 55d6e1cdf8
commit 505650205d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 2042 additions and 12 deletions

View File

@ -142,14 +142,26 @@ module DiscourseAi
def self.categories
return @categories if defined?(@categories)
url = "https://meta.discourse.org/site.json"
@categories = {}
page = 0
loop do
page += 1
url = "https://meta.discourse.org/categories.json?page=#{page}"
json = JSON.parse(Net::HTTP.get(URI(url)))
@categories =
json["categories"]
.map do |c|
[c["id"], { "name" => c["name"], "parent_category_id" => c["parent_category_id"] }]
break if json["category_list"]["categories"].blank?
json["category_list"]["categories"].each do |c|
@categories[c["id"]] = {
"name" => c["name"],
"parent_category_id" => c["parent_category_id"],
}
end
.to_h
end
@categories
end
def description_args

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,7 @@
{
"category_list": {
"can_create_category": false,
"can_create_topic": false,
"categories": []
}
}

File diff suppressed because one or more lines are too long

View File

@ -11,12 +11,27 @@ RSpec.describe DiscourseAi::AiBot::Tools::DiscourseMetaSearch do
let(:mock_search_json) { plugin_file_from_fixtures("search.json", "search_meta").read }
let(:mock_site_json) { plugin_file_from_fixtures("site.json", "search_meta").read }
let(:mock_categories_page_1) do
plugin_file_from_fixtures("categories_page_1.json", "search_meta").read
end
let(:mock_categories_page_2) do
plugin_file_from_fixtures("categories_page_2.json", "search_meta").read
end
before do
stub_request(:get, "https://meta.discourse.org/site.json").to_return(
stub_request(:get, "https://meta.discourse.org/categories.json?page=1").to_return(
status: 200,
body: mock_site_json,
body: mock_categories_page_1,
headers: {
},
)
end
before do
stub_request(:get, "https://meta.discourse.org/categories.json?page=2").to_return(
status: 200,
body: mock_categories_page_2,
headers: {
},
)