2023-04-04 10:24:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module Summarization
|
|
|
|
class EntryPoint
|
|
|
|
def inject_into(plugin)
|
2024-07-02 11:51:59 -04:00
|
|
|
plugin.add_to_serializer(:current_user, :can_summarize) do
|
2024-07-03 20:48:18 -04:00
|
|
|
return false if !SiteSetting.ai_summarization_enabled
|
2024-07-02 11:51:59 -04:00
|
|
|
scope.user.in_any_groups?(SiteSetting.ai_custom_summarization_allowed_groups_map)
|
2024-01-29 14:04:25 -05:00
|
|
|
end
|
|
|
|
|
2024-07-02 11:51:59 -04:00
|
|
|
plugin.add_to_serializer(:topic_view, :summarizable) do
|
2024-10-21 14:15:25 -04:00
|
|
|
scope.can_see_summary?(object.topic)
|
2024-01-29 14:04:25 -05:00
|
|
|
end
|
|
|
|
|
2024-07-02 11:51:59 -04:00
|
|
|
plugin.add_to_serializer(:web_hook_topic_view, :summarizable) do
|
2024-10-21 14:15:25 -04:00
|
|
|
scope.can_see_summary?(object.topic)
|
2023-06-27 11:26:33 -04:00
|
|
|
end
|
2024-10-18 17:01:39 -04:00
|
|
|
|
2024-11-22 10:07:27 -05:00
|
|
|
plugin.register_modifier(:topic_query_create_list_topics) do |topics, options|
|
|
|
|
if Discourse.filters.include?(options[:filter]) && SiteSetting.ai_summarization_enabled &&
|
|
|
|
SiteSetting.ai_summarize_max_hot_topics_gists_per_batch > 0
|
2024-11-25 10:24:02 -05:00
|
|
|
topics.includes(:ai_gist_summary)
|
2024-11-22 10:07:27 -05:00
|
|
|
else
|
|
|
|
topics
|
|
|
|
end
|
|
|
|
end
|
2024-10-18 17:01:39 -04:00
|
|
|
|
|
|
|
plugin.add_to_serializer(
|
|
|
|
:topic_list_item,
|
|
|
|
:ai_topic_gist,
|
2024-10-21 14:15:25 -04:00
|
|
|
include_condition: -> { scope.can_see_gists? },
|
2024-10-18 17:01:39 -04:00
|
|
|
) do
|
2024-11-19 13:37:19 -05:00
|
|
|
return if !Discourse.filters.include?(options[:filter])
|
2024-11-25 10:24:02 -05:00
|
|
|
object.ai_gist_summary&.summarized_text
|
2024-10-18 17:01:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# To make sure hot topic gists are inmediately up to date, we rely on this event
|
|
|
|
# instead of using a scheduled job.
|
|
|
|
plugin.on(:topic_hot_scores_updated) { Jobs.enqueue(:hot_topics_gist_batch) }
|
2024-10-25 11:38:49 -04:00
|
|
|
|
|
|
|
# As this event can be triggered quite often, let's be overly cautious enqueueing
|
|
|
|
# jobs if the feature is disabled.
|
|
|
|
plugin.on(:post_created) do |post|
|
|
|
|
if SiteSetting.discourse_ai_enabled && SiteSetting.ai_summarization_enabled &&
|
|
|
|
SiteSetting.ai_summarize_max_hot_topics_gists_per_batch > 0 && post.topic
|
|
|
|
hot_score = TopicHotScore.find_by(topic: post.topic)
|
|
|
|
|
2024-10-25 12:04:42 -04:00
|
|
|
if hot_score.present? && hot_score.updated_at > 1.day.ago
|
2024-10-25 11:38:49 -04:00
|
|
|
Jobs.enqueue(:update_hot_topic_gist, topic_id: post&.topic_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-04-04 10:24:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|