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

private_features_service.rb « destroy « todos « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 44c3ff231f86fe4a57c883d344bbe874053afac9 (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
# frozen_string_literal: true

module Todos
  module Destroy
    class PrivateFeaturesService < ::Todos::Destroy::BaseService
      attr_reader :project_ids, :user_id

      def initialize(project_ids, user_id = nil)
        @project_ids = project_ids
        @user_id = user_id
      end

      # rubocop: disable CodeReuse/ActiveRecord
      def execute
        ProjectFeature.where(project_id: project_ids).each do |project_features|
          target_types = []
          target_types << Issue.name if private?(project_features.issues_access_level)
          target_types << MergeRequest.name if private?(project_features.merge_requests_access_level)
          target_types << Commit.name if private?(project_features.repository_access_level)

          next if target_types.empty?

          remove_todos(project_features.project_id, target_types)
        end
      end
      # rubocop: enable CodeReuse/ActiveRecord

      private

      def private?(feature_level)
        feature_level == ProjectFeature::PRIVATE
      end

      # rubocop: disable CodeReuse/ActiveRecord
      def remove_todos(project_id, target_types)
        items = Todo.where(project_id: project_id)
        items = items.where(user_id: user_id) if user_id

        items.where.not(user_id: authorized_users)
          .where(target_type: target_types)
          .delete_all
      end
      # rubocop: enable CodeReuse/ActiveRecord
    end
  end
end