2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-29 01:49:17 -04:00
|
|
|
module API
|
|
|
|
class Events < Grape::API
|
|
|
|
include PaginationParams
|
2018-07-15 09:00:48 -04:00
|
|
|
include APIGuard
|
2017-05-29 01:49:17 -04:00
|
|
|
|
|
|
|
helpers do
|
|
|
|
params :event_filter_params do
|
|
|
|
optional :action, type: String, values: Event.actions, desc: 'Event action to filter on'
|
|
|
|
optional :target_type, type: String, values: Event.target_types, desc: 'Event target type to filter on'
|
|
|
|
optional :before, type: Date, desc: 'Include only events created before this date'
|
|
|
|
optional :after, type: Date, desc: 'Include only events created after this date'
|
|
|
|
end
|
|
|
|
|
|
|
|
params :sort_params do
|
|
|
|
optional :sort, type: String, values: %w[asc desc], default: 'desc',
|
|
|
|
desc: 'Return events sorted in ascending and descending order'
|
|
|
|
end
|
|
|
|
|
2018-09-21 04:44:34 -04:00
|
|
|
RedactedEvent = OpenStruct.new(target_title: 'Confidential event').freeze
|
|
|
|
|
|
|
|
def redact_events(events)
|
|
|
|
events.map do |event|
|
|
|
|
if event.visible_to_user?(current_user)
|
|
|
|
event
|
|
|
|
else
|
|
|
|
RedactedEvent
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-09-21 04:44:34 -04:00
|
|
|
def present_events(events, redact: true)
|
2017-05-29 01:49:17 -04:00
|
|
|
events = events.reorder(created_at: params[:sort])
|
2018-06-03 07:36:22 -04:00
|
|
|
.with_associations
|
2017-05-29 01:49:17 -04:00
|
|
|
|
2018-09-21 04:44:34 -04:00
|
|
|
events = paginate(events)
|
|
|
|
events = redact_events(events) if redact
|
|
|
|
|
|
|
|
present events, with: Entities::Event
|
2017-05-29 01:49:17 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
resource :events do
|
2018-07-15 09:00:48 -04:00
|
|
|
allow_access_with_scope :read_user, if: -> (request) { request.get? }
|
|
|
|
|
2017-05-29 01:49:17 -04:00
|
|
|
desc "List currently authenticated user's events" do
|
|
|
|
detail 'This feature was introduced in GitLab 9.3.'
|
|
|
|
success Entities::Event
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
use :event_filter_params
|
|
|
|
use :sort_params
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
get do
|
|
|
|
authenticate!
|
|
|
|
|
|
|
|
events = EventsFinder.new(params.merge(source: current_user, current_user: current_user)).execute.preload(:author, :target)
|
|
|
|
|
2018-09-21 04:44:34 -04:00
|
|
|
# Since we're viewing our own events, redaction is unnecessary
|
|
|
|
present_events(events, redact: false)
|
2017-05-29 01:49:17 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
params do
|
2017-06-01 23:41:47 -04:00
|
|
|
requires :id, type: String, desc: 'The ID or Username of the user'
|
2017-05-29 01:49:17 -04:00
|
|
|
end
|
|
|
|
resource :users do
|
2018-07-15 09:00:48 -04:00
|
|
|
allow_access_with_scope :read_user, if: -> (request) { request.get? }
|
|
|
|
|
2017-05-29 01:49:17 -04:00
|
|
|
desc 'Get the contribution events of a specified user' do
|
|
|
|
detail 'This feature was introduced in GitLab 8.13.'
|
|
|
|
success Entities::Event
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
use :event_filter_params
|
|
|
|
use :sort_params
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
get ':id/events' do
|
2017-06-01 23:41:47 -04:00
|
|
|
user = find_user(params[:id])
|
2017-05-29 01:49:17 -04:00
|
|
|
not_found!('User') unless user
|
|
|
|
|
|
|
|
events = EventsFinder.new(params.merge(source: user, current_user: current_user)).execute.preload(:author, :target)
|
|
|
|
|
|
|
|
present_events(events)
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
2017-08-31 07:44:49 -04:00
|
|
|
resource :projects, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
|
2017-05-29 01:49:17 -04:00
|
|
|
desc "List a Project's visible events" do
|
|
|
|
success Entities::Event
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
use :event_filter_params
|
|
|
|
use :sort_params
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
get ":id/events" do
|
|
|
|
events = EventsFinder.new(params.merge(source: user_project, current_user: current_user)).execute.preload(:author, :target)
|
|
|
|
|
|
|
|
present_events(events)
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-05-29 01:49:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|