2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-09-02 11:12:13 -04:00
|
|
|
module Notes
|
|
|
|
class UpdateService < BaseService
|
2015-07-30 08:45:54 -04:00
|
|
|
def execute(note)
|
2020-01-30 10:09:15 -05:00
|
|
|
return note unless note.editable? && params.present?
|
2014-09-02 11:12:13 -04:00
|
|
|
|
2019-10-11 14:06:15 -04:00
|
|
|
old_mentioned_users = note.mentioned_users(current_user).to_a
|
2017-03-28 02:25:43 -04:00
|
|
|
|
2021-03-09 10:08:59 -05:00
|
|
|
note.assign_attributes(params)
|
2014-09-02 11:12:13 -04:00
|
|
|
|
2020-10-23 05:08:41 -04:00
|
|
|
track_note_edit_usage_for_issues(note) if note.for_issue?
|
2021-01-11 04:10:46 -05:00
|
|
|
track_note_edit_usage_for_merge_requests(note) if note.for_merge_request?
|
2020-10-23 05:08:41 -04:00
|
|
|
|
2019-08-26 05:20:00 -04:00
|
|
|
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
|
|
|
|
|
2021-03-09 10:08:59 -05:00
|
|
|
if note.note_changed?
|
|
|
|
note.assign_attributes(last_edited_at: Time.current, updated_by: current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
note.with_transaction_returning_status do
|
|
|
|
update_confidentiality(note)
|
|
|
|
note.save
|
|
|
|
end
|
|
|
|
|
2020-08-12 23:10:13 -04:00
|
|
|
unless only_commands || note.for_personal_snippet?
|
2019-08-26 05:20:00 -04:00
|
|
|
note.create_new_cross_references!(current_user)
|
|
|
|
|
|
|
|
update_todos(note, old_mentioned_users)
|
|
|
|
|
|
|
|
update_suggestions(note)
|
2016-02-16 17:21:24 -05:00
|
|
|
end
|
|
|
|
|
2019-08-26 05:20:00 -04:00
|
|
|
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
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
|
2019-08-26 05:20:00 -04:00
|
|
|
if only_commands
|
|
|
|
delete_note(note, message)
|
|
|
|
note = nil
|
|
|
|
else
|
|
|
|
note.save
|
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
|
2014-09-02 11:12:13 -04:00
|
|
|
note
|
|
|
|
end
|
2019-08-26 05:20:00 -04:00
|
|
|
|
|
|
|
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'))
|
2020-03-23 08:09:47 -04:00
|
|
|
# Allow consumers to detect problems applying commands
|
|
|
|
note.errors.add(:commands, _('Commands did not apply')) unless message.present?
|
2019-08-26 05:20:00 -04:00
|
|
|
|
|
|
|
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
|
2020-07-16 08:09:22 -04:00
|
|
|
|
|
|
|
# This method updates confidentiality of all discussion notes at once
|
|
|
|
def update_confidentiality(note)
|
|
|
|
return unless params.key?(:confidential)
|
|
|
|
return unless note.is_a?(DiscussionNote) # we don't need to do bulk update for single notes
|
|
|
|
return unless note.start_of_discussion? # don't update all notes if a response is being updated
|
|
|
|
|
|
|
|
Note.id_in(note.discussion.notes.map(&:id)).update_all(confidential: params[:confidential])
|
|
|
|
end
|
2020-10-23 05:08:41 -04:00
|
|
|
|
|
|
|
def track_note_edit_usage_for_issues(note)
|
|
|
|
Gitlab::UsageDataCounters::IssueActivityUniqueCounter.track_issue_comment_edited_action(author: note.author)
|
|
|
|
end
|
2021-01-11 04:10:46 -05:00
|
|
|
|
|
|
|
def track_note_edit_usage_for_merge_requests(note)
|
2021-01-14 16:10:37 -05:00
|
|
|
Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter.track_edit_comment_action(note: note)
|
2021-01-11 04:10:46 -05:00
|
|
|
end
|
2014-09-02 11:12:13 -04:00
|
|
|
end
|
|
|
|
end
|
2020-03-30 08:07:40 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
Notes::UpdateService.prepend_mod_with('Notes::UpdateService')
|