2020-05-31 20:08:25 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Mutations
|
|
|
|
module Discussions
|
|
|
|
class ToggleResolve < BaseMutation
|
|
|
|
graphql_name 'DiscussionToggleResolve'
|
|
|
|
|
|
|
|
description 'Toggles the resolved state of a discussion'
|
|
|
|
|
|
|
|
argument :id,
|
2020-10-20 05:08:43 -04:00
|
|
|
Types::GlobalIDType[Discussion],
|
2020-05-31 20:08:25 -04:00
|
|
|
required: true,
|
2021-01-07 07:10:24 -05:00
|
|
|
description: 'The global ID of the discussion.'
|
2020-05-31 20:08:25 -04:00
|
|
|
|
|
|
|
argument :resolve,
|
|
|
|
GraphQL::BOOLEAN_TYPE,
|
|
|
|
required: true,
|
2021-01-07 07:10:24 -05:00
|
|
|
description: 'Will resolve the discussion when true, and unresolve the discussion when false.'
|
2020-05-31 20:08:25 -04:00
|
|
|
|
|
|
|
field :discussion,
|
|
|
|
Types::Notes::DiscussionType,
|
|
|
|
null: true,
|
2021-01-07 07:10:24 -05:00
|
|
|
description: 'The discussion after mutation.'
|
2020-05-31 20:08:25 -04:00
|
|
|
|
|
|
|
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:)
|
2020-10-20 05:08:43 -04:00
|
|
|
# TODO: remove explicit coercion once compatibility layer has been removed
|
|
|
|
# See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
|
|
|
|
id = Types::GlobalIDType[Discussion].coerce_isolated_input(id)
|
|
|
|
GitlabSchema.find_by_gid(id)
|
2020-05-31 20:08:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def resolve!(discussion)
|
|
|
|
::Discussions::ResolveService.new(
|
|
|
|
discussion.project,
|
|
|
|
current_user,
|
|
|
|
one_or_more_discussions: discussion
|
|
|
|
).execute
|
|
|
|
end
|
|
|
|
|
|
|
|
def unresolve!(discussion)
|
2021-01-26 16:09:04 -05:00
|
|
|
::Discussions::UnresolveService.new(discussion, current_user).execute
|
2020-05-31 20:08:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|