2012-07-31 05:32:49 +00:00
|
|
|
module Notes
|
2014-01-16 17:03:42 +00:00
|
|
|
class CreateService < BaseService
|
2012-07-31 05:32:49 +00:00
|
|
|
def execute
|
2014-06-17 19:09:01 +00:00
|
|
|
note = project.notes.new(params)
|
2012-07-31 05:32:49 +00:00
|
|
|
note.author = current_user
|
2014-02-20 12:07:42 +00:00
|
|
|
note.system = false
|
2014-06-17 19:09:01 +00:00
|
|
|
|
2016-04-16 19:09:08 +00:00
|
|
|
if note.award_emoji?
|
2016-05-17 17:28:17 +00:00
|
|
|
noteable = note.noteable
|
2016-09-27 14:24:50 +00:00
|
|
|
if noteable.user_can_award?(current_user, note.award_emoji_name)
|
|
|
|
todo_service.new_award_emoji(noteable, current_user)
|
|
|
|
return noteable.create_award_emoji(note.award_emoji_name, current_user)
|
|
|
|
end
|
2016-04-16 19:09:08 +00:00
|
|
|
end
|
|
|
|
|
2016-06-30 15:34:19 +00:00
|
|
|
# We execute commands (extracted from `params[:note]`) on the noteable
|
|
|
|
# **before** we save the note because if the note consists of commands
|
|
|
|
# only, there is no need be create a note!
|
2016-08-12 09:19:29 +00:00
|
|
|
slash_commands_service = SlashCommandsService.new(project, current_user)
|
2016-08-13 01:17:18 +00:00
|
|
|
|
2016-08-13 16:58:51 +00:00
|
|
|
if slash_commands_service.supported?(note)
|
|
|
|
content, command_params = slash_commands_service.extract_commands(note)
|
2016-06-30 15:34:19 +00:00
|
|
|
|
2016-08-13 16:58:51 +00:00
|
|
|
only_commands = content.empty?
|
|
|
|
|
|
|
|
note.note = content
|
|
|
|
end
|
|
|
|
|
2016-10-13 15:26:44 +00:00
|
|
|
note.run_after_commit do
|
2016-01-27 15:59:16 +00:00
|
|
|
# Finish the harder work in the background
|
2016-10-13 15:26:44 +00:00
|
|
|
NewNoteWorker.perform_async(note.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
if !only_commands && note.save
|
2016-06-30 15:34:19 +00:00
|
|
|
todo_service.new_note(note, current_user)
|
|
|
|
end
|
|
|
|
|
2016-11-19 02:19:04 +00:00
|
|
|
if command_params.present?
|
2016-08-13 16:58:51 +00:00
|
|
|
slash_commands_service.execute(command_params, note)
|
|
|
|
|
|
|
|
# We must add the error after we call #save because errors are reset
|
|
|
|
# when #save is called
|
|
|
|
if only_commands
|
|
|
|
note.errors.add(:commands_only, 'Your commands have been executed!')
|
|
|
|
end
|
2016-11-24 06:32:32 +00:00
|
|
|
|
|
|
|
note.commands_changes = command_params.keys
|
2014-06-17 19:09:01 +00:00
|
|
|
end
|
|
|
|
|
2012-07-31 05:32:49 +00:00
|
|
|
note
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|