summaryrefslogtreecommitdiff
path: root/lib/api/subscriptions.rb
blob: dfb54446ddf31c1d38b24f76ef04ff5094b0b29d (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# frozen_string_literal: true

module API
  class Subscriptions < Grape::API
    helpers ::API::Helpers::LabelHelpers

    before { authenticate! }

    subscribables = [
      {
        type: 'merge_requests',
        entity: Entities::MergeRequest,
        source: Project,
        finder: ->(id) { find_merge_request_with_access(id, :update_merge_request) }
      },
      {
        type: 'issues',
        entity: Entities::Issue,
        source: Project,
        finder: ->(id) { find_project_issue(id) }
      },
      {
        type: 'labels',
        entity: Entities::ProjectLabel,
        source: Project,
        finder: ->(id) { find_label(user_project, id) }
      },
      {
        type: 'labels',
        entity: Entities::GroupLabel,
        source: Group,
        finder: ->(id) { find_label(user_group, id) }
      }
    ]

    subscribables.each do |subscribable|
      source_type = subscribable[:source].name.underscore

      params do
        requires :id, type: String, desc: "The #{source_type} ID"
        requires :subscribable_id, type: String, desc: 'The ID of a resource'
      end
      resource source_type.pluralize, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
        desc 'Subscribe to a resource' do
          success subscribable[:entity]
        end
        post ":id/#{subscribable[:type]}/:subscribable_id/subscribe" do
          parent = parent_resource(source_type)
          resource = instance_exec(params[:subscribable_id], &subscribable[:finder])

          if resource.subscribed?(current_user, parent)
            not_modified!
          else
            resource.subscribe(current_user, parent)
            present resource, with: subscribable[:entity], current_user: current_user, project: parent, parent: parent
          end
        end

        desc 'Unsubscribe from a resource' do
          success subscribable[:entity]
        end
        post ":id/#{subscribable[:type]}/:subscribable_id/unsubscribe" do
          parent = parent_resource(source_type)
          resource = instance_exec(params[:subscribable_id], &subscribable[:finder])

          if !resource.subscribed?(current_user, parent)
            not_modified!
          else
            resource.unsubscribe(current_user, parent)
            present resource, with: subscribable[:entity], current_user: current_user, project: parent, parent: parent
          end
        end
      end
    end

    private

    helpers do
      def parent_resource(source_type)
        case source_type
        when 'project'
          user_project
        else
          nil
        end
      end
    end
  end
end