diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 14:18:50 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-06-18 14:18:50 +0300 |
commit | 8c7f4e9d5f36cff46365a7f8c4b9c21578c1e781 (patch) | |
tree | a77e7fe7a93de11213032ed4ab1f33a3db51b738 /app/graphql/mutations/discussions/toggle_resolve.rb | |
parent | 00b35af3db1abfe813a778f643dad221aad51fca (diff) |
Add latest changes from gitlab-org/gitlab@13-1-stable-ee
Diffstat (limited to 'app/graphql/mutations/discussions/toggle_resolve.rb')
-rw-r--r-- | app/graphql/mutations/discussions/toggle_resolve.rb | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/app/graphql/mutations/discussions/toggle_resolve.rb b/app/graphql/mutations/discussions/toggle_resolve.rb new file mode 100644 index 00000000000..41fd22c6b55 --- /dev/null +++ b/app/graphql/mutations/discussions/toggle_resolve.rb @@ -0,0 +1,73 @@ +# frozen_string_literal: true + +module Mutations + module Discussions + class ToggleResolve < BaseMutation + graphql_name 'DiscussionToggleResolve' + + description 'Toggles the resolved state of a discussion' + + argument :id, + GraphQL::ID_TYPE, + required: true, + description: 'The global id of the discussion' + + argument :resolve, + GraphQL::BOOLEAN_TYPE, + required: true, + description: 'Will resolve the discussion when true, and unresolve the discussion when false' + + field :discussion, + Types::Notes::DiscussionType, + null: true, + description: 'The discussion after mutation' + + def resolve(id:, resolve:) + discussion = authorized_find_discussion!(id: id) + errors = [] + + begin + if resolve + resolve!(discussion) + else + unresolve!(discussion) + end + rescue ActiveRecord::RecordNotSaved + errors << "Discussion failed to be #{'un' unless resolve}resolved" + end + + { + discussion: discussion, + errors: errors + } + end + + private + + # `Discussion` permissions are checked through `Discussion#can_resolve?`, + # so we use this method of checking permissions rather than by defining + # an `authorize` permission and calling `authorized_find!`. + def authorized_find_discussion!(id:) + find_object(id: id).tap do |discussion| + raise_resource_not_available_error! unless discussion&.can_resolve?(current_user) + end + end + + def find_object(id:) + GitlabSchema.object_from_id(id, expected_type: ::Discussion) + end + + def resolve!(discussion) + ::Discussions::ResolveService.new( + discussion.project, + current_user, + one_or_more_discussions: discussion + ).execute + end + + def unresolve!(discussion) + discussion.unresolve! + end + end + end +end |