Skip to content

Commit

Permalink
FIX: Fetch categories data using specific endpoint (#543)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nbianca committed Apr 8, 2024
1 parent 55d6e1c commit 5056502
Show file tree
Hide file tree
Showing 5 changed files with 2,042 additions and 12 deletions.
28 changes: 20 additions & 8 deletions lib/ai_bot/tools/discourse_meta_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,26 @@ def invoke(bot_user, llm)

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
Expand Down
Loading

0 comments on commit 5056502

Please sign in to comment.