2024-07-02 11:51:59 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Summarization
|
|
|
|
class SummaryController < ::ApplicationController
|
|
|
|
requires_plugin ::DiscourseAi::PLUGIN_NAME
|
|
|
|
|
|
|
|
def show
|
|
|
|
topic = Topic.find(params[:topic_id])
|
|
|
|
guardian.ensure_can_see!(topic)
|
|
|
|
|
2024-10-15 12:53:26 -04:00
|
|
|
if !guardian.can_see_summary?(topic, AiSummary.summary_types[:complete])
|
|
|
|
raise Discourse::NotFound
|
|
|
|
end
|
2024-07-02 11:51:59 -04:00
|
|
|
|
|
|
|
RateLimiter.new(current_user, "summary", 6, 5.minutes).performed! if current_user
|
|
|
|
|
|
|
|
opts = params.permit(:skip_age_check)
|
2024-08-13 07:47:47 -04:00
|
|
|
skip_age_check = opts[:skip_age_check] == "true"
|
2024-07-02 11:51:59 -04:00
|
|
|
|
2024-10-15 12:53:26 -04:00
|
|
|
summarization_service = DiscourseAi::TopicSummarization.for(topic, current_user)
|
|
|
|
|
2024-07-02 11:51:59 -04:00
|
|
|
if params[:stream] && current_user
|
2024-10-15 12:53:26 -04:00
|
|
|
cached_summary = summarization_service.cached_summary
|
2024-08-13 07:47:47 -04:00
|
|
|
|
|
|
|
if cached_summary && !skip_age_check
|
|
|
|
render_serialized(cached_summary, AiTopicSummarySerializer)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2024-07-02 11:51:59 -04:00
|
|
|
Jobs.enqueue(
|
|
|
|
:stream_topic_ai_summary,
|
|
|
|
topic_id: topic.id,
|
|
|
|
user_id: current_user.id,
|
2024-08-13 07:47:47 -04:00
|
|
|
skip_age_check: skip_age_check,
|
2024-07-02 11:51:59 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
render json: success_json
|
|
|
|
else
|
|
|
|
hijack do
|
2024-10-15 12:53:26 -04:00
|
|
|
summary = summarization_service.summarize(skip_age_check: skip_age_check)
|
2024-07-02 11:51:59 -04:00
|
|
|
render_serialized(summary, AiTopicSummarySerializer)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|