debian-mirror-gitlab/app/services/notes/update_service.rb

82 lines
2.1 KiB
Ruby
Raw Normal View History

2018-11-18 11:00:15 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
module Notes
class UpdateService < BaseService
2015-09-11 14:41:01 +05:30
def execute(note)
return note unless note.editable?
2015-04-26 12:48:37 +05:30
2019-12-21 20:55:43 +05:30
old_mentioned_users = note.mentioned_users(current_user).to_a
2017-08-17 22:00:37 +05:30
2020-01-01 13:55:28 +05:30
note.assign_attributes(params.merge(updated_by: current_user))
note.with_transaction_returning_status do
note.save && note.store_mentions!
end
2015-04-26 12:48:37 +05:30
2019-12-04 20:38:33 +05:30
only_commands = false
quick_actions_service = QuickActionsService.new(project, current_user)
if quick_actions_service.supported?(note)
content, update_params, message = quick_actions_service.execute(note, {})
only_commands = content.empty?
note.note = content
end
unless only_commands
note.create_new_cross_references!(current_user)
update_todos(note, old_mentioned_users)
update_suggestions(note)
2016-04-02 18:10:28 +05:30
end
2019-12-04 20:38:33 +05:30
if quick_actions_service.commands_executed_count.to_i > 0
if update_params.present?
quick_actions_service.apply_updates(update_params, note)
note.commands_changes = update_params
2019-02-15 15:39:39 +05:30
end
2019-12-04 20:38:33 +05:30
if only_commands
delete_note(note, message)
note = nil
else
note.save
end
2019-02-15 15:39:39 +05:30
end
2015-04-26 12:48:37 +05:30
note
end
2019-12-04 20:38:33 +05:30
private
def delete_note(note, message)
# We must add the error after we call #save because errors are reset
# when #save is called
note.errors.add(:commands_only, message.presence || _('Commands did not apply'))
Notes::DestroyService.new(project, current_user).execute(note)
end
def update_suggestions(note)
return unless note.supports_suggestion?
Suggestion.transaction do
note.suggestions.delete_all
Suggestions::CreateService.new(note).execute
end
# We need to refresh the previous suggestions call cache
# in order to get the new records.
note.reset
end
def update_todos(note, old_mentioned_users)
return unless note.previous_changes.include?('note')
TodoService.new.update_note(note, current_user, old_mentioned_users)
end
2015-04-26 12:48:37 +05:30
end
end