Class: TopicSummarization

Inherits:
Object
  • Object
show all
Defined in:
app/services/topic_summarization.rb

Instance Method Summary collapse

Constructor Details

#initialize(strategy) ⇒ TopicSummarization

Returns a new instance of TopicSummarization.



4
5
6
# File 'app/services/topic_summarization.rb', line 4

def initialize(strategy)
  @strategy = strategy
end

Instance Method Details

#summarize(topic, user, opts = {}, &on_partial_blk) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/services/topic_summarization.rb', line 8

def summarize(topic, user, opts = {}, &on_partial_blk)
  existing_summary = SummarySection.find_by(target: topic, meta_section_id: nil)

  # Existing summary shouldn't be nil in this scenario because the controller checks its existence.
  return if !user && !existing_summary

  targets_data = summary_targets(topic).pluck(:post_number, :raw, :username)

  current_topic_sha = build_sha(targets_data.map(&:first))
  can_summarize = Summarization::Base.can_request_summary_for?(user)

  if use_cached?(existing_summary, can_summarize, current_topic_sha, !!opts[:skip_age_check])
    # It's important that we signal a cached summary is outdated
    existing_summary.mark_as_outdated if new_targets?(existing_summary, current_topic_sha)

    return existing_summary
  end

  delete_cached_summaries_of(topic) if existing_summary

  content = {
    resource_path: "#{Discourse.base_path}/t/-/#{topic.id}",
    content_title: topic.title,
    contents: [],
  }

  targets_data.map do |(pn, raw, username)|
    content[:contents] << { poster: username, id: pn, text: raw }
  end

  summarization_result = strategy.summarize(content, &on_partial_blk)

  cache_summary(summarization_result, targets_data.map(&:first), topic)
end

#summary_targets(topic) ⇒ Object



43
44
45
# File 'app/services/topic_summarization.rb', line 43

def summary_targets(topic)
  topic.has_summary? ? best_replies(topic) : pick_selection(topic)
end