Welcome to mirror list, hosted at ThFree Co, Russian Federation.

toggle_subscription_service.rb « labels « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c643c82c3f2c849046f4c2401afbc2a99b217740 (plain)
1
2
3
4
5
6
7
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
42
43
44
45
46
47
48
49
50
module Labels
  class ToggleSubscriptionService < Labels::BaseService
    def execute(label)
      Label.transaction do
        label.toggle_subscription(user)

        return if label.project_label?

        toggle_subscription_to_global_label(label.label_type, label.title) if label.global_label?
        toggle_subscription_to_group_label(label.label_type, label.title) if label.group_label?
      end
    end

    private

    def toggle_subscription_to_global_label(label_type, title)
      if subject.nil?
        toggle_subscription(Group.all, label_type, title)
        toggle_subscription(Project.all, label_type, title)
      end

      if subject.is_a?(Group)
        toggle_subscription(nil, label_type, title)
        toggle_subscription(Group.where.not(id: subject), label_type, title)
        toggle_subscription(Project.all, label_type, title)
      end

      if subject.is_a?(Project)
        toggle_subscription(nil, label_type, title)
        toggle_subscription(Group.all, label_type, title)
        toggle_subscription(Project.where.not(id: subject), label_type, title)
      end
    end

    def toggle_subscription_to_group_label(label_type, title)
      if subject.is_a?(Group)
        toggle_subscription(subject.projects, label_type, title)
      end

      if subject.is_a?(Project)
        toggle_subscription(subject.group, label_type, title)
        toggle_subscription(subject.group.projects.where.not(id: subject), label_type, title)
      end
    end

    def toggle_subscription(subject, label_type, title)
      find_labels(subject, label_type, title).each { |label| label.toggle_subscription(user) }
    end
  end
end