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:
parent
55d6e1cdf8
commit
505650205d
|
@ -142,14 +142,26 @@ module DiscourseAi
|
|||
|
||||
def self.categories
|
||||
return @categories if defined?(@categories)
|
||||
url = "https://meta.discourse.org/site.json"
|
||||
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"] }]
|
||||
end
|
||||
.to_h
|
||||
|
||||
@categories = {}
|
||||
|
||||
page = 0
|
||||
loop do
|
||||
page += 1
|
||||
url = "https://meta.discourse.org/categories.json?page=#{page}"
|
||||
|
||||
json = JSON.parse(Net::HTTP.get(URI(url)))
|
||||
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
|
||||
end
|
||||
|
||||
@categories
|
||||
end
|
||||
|
||||
def description_args
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -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
|
@ -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: {
|
||||
},
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue