docbrown/app/queries/articles/api_search_query.rb
Ridhwana e67454fa53
Preparation of endpoint and files required for ChatGPT Plugin (#19394)
* feat: add the ./wellknown/ai-plugin.json file

* feat: add a logo

* feat: firts attempt at open_api file with endpoint we thought we'd use

* feat: add a search action to teh articles controller that build up teh appropriate json

* feat: add a query param to the article index

* feat: add some chatgpt endpoints and file paths to CORS

* feat: update the openapi yml file to reflect the article search endpoint

* fix: use the correct concept

* feat: replace the logo

* feat: update the ai-json file

* feat: update the openapi file

* feat: update the api and it's params

* feat: update the search query

* fix: ordering

* feat: cors debug

* fix: logic with markdown

* chore: fix test

* spec: article

* spec: article

* spec: article

* Update public/.well-known/ai-plugin.json

* Update public/openapi.yml

---------

Co-authored-by: Ben Halpern <bendhalpern@gmail.com>
2023-04-27 16:51:24 -04:00

50 lines
1.1 KiB
Ruby

module Articles
class ApiSearchQuery
DEFAULT_PER_PAGE = 30
def self.call(...)
new(...).call
end
def initialize(params)
@q = params[:q]
@top = params[:top]
@page = params[:page].to_i
@per_page = [(params[:per_page] || DEFAULT_PER_PAGE).to_i, per_page_max].min
end
def call
@articles = published_articles_with_users_and_organizations
if q.present?
@articles = query_articles
end
if top.present?
@articles = top_articles.order(public_reactions_count: :desc)
end
@articles.page(page).per(per_page || DEFAULT_PER_PAGE)
end
private
attr_reader :q, :top, :page, :per_page
def per_page_max
(ApplicationConfig["API_PER_PAGE_MAX"] || 1000).to_i
end
def query_articles
@articles.search_articles(q)
end
def top_articles
@articles.where("published_at > ?", top.to_i.days.ago)
end
def published_articles_with_users_and_organizations
Article.published.includes([{ user: :profile }, :organization]).order(hotness_score: :desc)
end
end
end