2012-07-31 01:32:49 -04:00
|
|
|
module Notes
|
2014-01-16 12:03:42 -05:00
|
|
|
class CreateService < BaseService
|
2012-07-31 01:32:49 -04:00
|
|
|
def execute
|
2014-06-17 15:09:01 -04:00
|
|
|
note = project.notes.new(params)
|
2012-07-31 01:32:49 -04:00
|
|
|
note.author = current_user
|
2014-02-20 07:07:42 -05:00
|
|
|
note.system = false
|
2014-06-17 15:09:01 -04:00
|
|
|
|
2016-04-16 15:09:08 -04:00
|
|
|
if note.award_emoji?
|
2016-05-17 13:28:17 -04:00
|
|
|
noteable = note.noteable
|
2016-09-27 10:24:50 -04: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 15:09:08 -04:00
|
|
|
end
|
|
|
|
|
2016-06-30 11:34:19 -04: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 05:19:29 -04:00
|
|
|
slash_commands_service = SlashCommandsService.new(project, current_user)
|
2016-08-12 21:17:18 -04:00
|
|
|
|
2016-08-13 12:58:51 -04:00
|
|
|
if slash_commands_service.supported?(note)
|
|
|
|
content, command_params = slash_commands_service.extract_commands(note)
|
2016-06-30 11:34:19 -04:00
|
|
|
|
2016-08-13 12:58:51 -04:00
|
|
|
only_commands = content.empty?
|
|
|
|
|
|
|
|
note.note = content
|
|
|
|
end
|
|
|
|
|
|
|
|
if !only_commands && note.save
|
2016-01-27 10:59:16 -05:00
|
|
|
# Finish the harder work in the background
|
|
|
|
NewNoteWorker.perform_in(2.seconds, note.id, params)
|
2016-06-30 11:34:19 -04:00
|
|
|
todo_service.new_note(note, current_user)
|
|
|
|
end
|
|
|
|
|
2016-08-13 12:58:51 -04:00
|
|
|
if command_params && command_params.any?
|
|
|
|
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
|
2014-06-17 15:09:01 -04:00
|
|
|
end
|
|
|
|
|
2012-07-31 01:32:49 -04:00
|
|
|
note
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|