Class: Collavre::TopicBranchService

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

Defined Under Namespace

Classes: BranchError

Constant Summary collapse

MAX_BRANCH_COMMENTS =
100

Instance Method Summary collapse

Constructor Details

#initialize(creative:, user:, source_topic:, name: nil) ⇒ TopicBranchService

Returns a new instance of TopicBranchService.



7
8
9
10
11
12
# File 'app/services/collavre/topic_branch_service.rb', line 7

def initialize(creative:, user:, source_topic:, name: nil)
  @creative = creative
  @user = user
  @source_topic = source_topic
  @name = name
end

Instance Method Details

#call(comment_ids:, enforce_limit: true, auto_select: true) ⇒ Object

Creates a new topic with copies of the selected comments. Returns the new Topic. enforce_limit: false bypasses MAX_BRANCH_COMMENTS for system-initiated full-history copies (e.g. Drop Trigger) where the UI’s selection cap does not apply. auto_select: false omits user_id from the topic-created broadcast so background/system branches do not hijack the owner’s current selection.

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/services/collavre/topic_branch_service.rb', line 21

def call(comment_ids:, enforce_limit: true, auto_select: true)
  comment_ids = Array(comment_ids).map(&:presence).compact.map(&:to_i)
  comment_ids = comment_ids.first(MAX_BRANCH_COMMENTS) if enforce_limit
  raise BranchError, I18n.t("collavre.comments.branch.no_selection") if comment_ids.empty?

  validate_permissions!
  originals = fetch_comments(comment_ids)

  Topic.transaction do
    @new_topic = create_branch_topic
    copy_comments(originals)
  end

  broadcast_topic_created(auto_select: auto_select)

  @new_topic
end