2016-08-09 13:26:45 -04:00
|
|
|
module Notes
|
|
|
|
class SlashCommandsService < BaseService
|
|
|
|
UPDATE_SERVICES = {
|
|
|
|
'Issue' => Issues::UpdateService,
|
|
|
|
'MergeRequest' => MergeRequests::UpdateService
|
|
|
|
}
|
|
|
|
|
2016-09-15 10:57:06 -04:00
|
|
|
def self.noteable_update_service(note)
|
|
|
|
UPDATE_SERVICES[note.noteable_type]
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.supported?(note, current_user)
|
2016-08-13 12:58:51 -04:00
|
|
|
noteable_update_service(note) &&
|
2016-09-20 04:23:13 -04:00
|
|
|
current_user &&
|
2016-09-15 10:57:06 -04:00
|
|
|
current_user.can?(:"update_#{note.noteable_type.underscore}", note.noteable)
|
|
|
|
end
|
|
|
|
|
|
|
|
def supported?(note)
|
|
|
|
self.class.supported?(note, current_user)
|
2016-08-13 12:58:51 -04:00
|
|
|
end
|
|
|
|
|
2016-08-12 05:19:29 -04:00
|
|
|
def extract_commands(note)
|
2016-08-13 12:58:51 -04:00
|
|
|
return [note.note, {}] unless supported?(note)
|
2016-08-09 13:26:45 -04:00
|
|
|
|
2016-08-12 05:19:29 -04:00
|
|
|
SlashCommands::InterpretService.new(project, current_user).
|
2016-08-09 13:26:45 -04:00
|
|
|
execute(note.note, note.noteable)
|
2016-08-12 05:19:29 -04:00
|
|
|
end
|
2016-08-09 13:26:45 -04:00
|
|
|
|
2016-08-12 21:17:18 -04:00
|
|
|
def execute(command_params, note)
|
|
|
|
return if command_params.empty?
|
2016-08-13 12:58:51 -04:00
|
|
|
return unless supported?(note)
|
|
|
|
|
2016-09-15 10:57:06 -04:00
|
|
|
self.class.noteable_update_service(note).new(project, current_user, command_params).execute(note.noteable)
|
2016-08-09 13:26:45 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|