diff options
Diffstat (limited to 'app/services/notes')
-rw-r--r-- | app/services/notes/create_service.rb | 29 | ||||
-rw-r--r-- | app/services/notes/quick_actions_service.rb | 16 | ||||
-rw-r--r-- | app/services/notes/update_service.rb | 15 |
3 files changed, 44 insertions, 16 deletions
diff --git a/app/services/notes/create_service.rb b/app/services/notes/create_service.rb index 7dd6cd9a87c..fdab2a07990 100644 --- a/app/services/notes/create_service.rb +++ b/app/services/notes/create_service.rb @@ -21,6 +21,8 @@ module Notes # only, there is no need be create a note! execute_quick_actions(note) do |only_commands| + note.check_for_spam(action: :create, user: current_user) unless only_commands + note.run_after_commit do # Finish the harder work in the background NewNoteWorker.perform_async(note.id) @@ -105,16 +107,10 @@ module Notes def do_commands(note, update_params, message, command_names, only_commands) return if quick_actions_service.commands_executed_count.to_i == 0 - if update_params.present? - invalid_message = validate_commands(note, update_params) - - if invalid_message - note.errors.add(:validation, invalid_message) - message = invalid_message - else - quick_actions_service.apply_updates(update_params, note) - note.commands_changes = update_params - end + update_error = quick_actions_update_errors(note, update_params) + if update_error + note.errors.add(:validation, update_error) + message = update_error end # We must add the error after we call #save because errors are reset @@ -127,6 +123,19 @@ module Notes end end + def quick_actions_update_errors(note, params) + return unless params.present? + + invalid_message = validate_commands(note, params) + return invalid_message if invalid_message + + service_response = quick_actions_service.apply_updates(params, note) + note.commands_changes = params + return if service_response.success? + + service_response.message.join(', ') + end + def quick_action_options { merge_request_diff_head_sha: params[:merge_request_diff_head_sha], diff --git a/app/services/notes/quick_actions_service.rb b/app/services/notes/quick_actions_service.rb index 38f7a23ce29..cba7398ebc0 100644 --- a/app/services/notes/quick_actions_service.rb +++ b/app/services/notes/quick_actions_service.rb @@ -50,7 +50,21 @@ module Notes update_params[:spend_time][:note_id] = note.id end - noteable_update_service(note, update_params).execute(note.noteable) + execute_update_service(note, update_params) + end + + private + + def execute_update_service(note, params) + service_response = noteable_update_service(note, params).execute(note.noteable) + + service_errors = if service_response.respond_to?(:errors) + service_response.errors.full_messages + elsif service_response.respond_to?(:[]) && service_response[:status] == :error + service_response[:message] + end + + service_errors.blank? ? ServiceResponse.success : ServiceResponse.error(message: service_errors) end def noteable_update_service(note, update_params) diff --git a/app/services/notes/update_service.rb b/app/services/notes/update_service.rb index e04891da7f8..52940281018 100644 --- a/app/services/notes/update_service.rb +++ b/app/services/notes/update_service.rb @@ -9,6 +9,8 @@ module Notes note.assign_attributes(params) + return note unless note.valid? + track_note_edit_usage_for_issues(note) if note.for_issue? track_note_edit_usage_for_merge_requests(note) if note.for_merge_request? @@ -23,10 +25,7 @@ module Notes note.note = content end - if note.note_changed? - note.assign_attributes(last_edited_at: Time.current, updated_by: current_user) - end - + update_note(note, only_commands) note.save unless only_commands || note.for_personal_snippet? @@ -45,7 +44,6 @@ module Notes if only_commands delete_note(note, message) - note = nil else note.save end @@ -56,6 +54,13 @@ module Notes private + def update_note(note, only_commands) + return unless note.note_changed? + + note.assign_attributes(last_edited_at: Time.current, updated_by: current_user) + note.check_for_spam(action: :update, user: current_user) unless only_commands + end + def delete_note(note, message) # We must add the error after we call #save because errors are reset # when #save is called |