debian-mirror-gitlab/app/graphql/mutations/discussions/toggle_resolve.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

74 lines
2 KiB
Ruby
Raw Normal View History

2020-06-23 00:09:42 +05:30
# frozen_string_literal: true
module Mutations
module Discussions
class ToggleResolve < BaseMutation
graphql_name 'DiscussionToggleResolve'
description 'Toggles the resolved state of a discussion'
argument :id,
2021-01-03 14:25:43 +05:30
Types::GlobalIDType[Discussion],
2020-06-23 00:09:42 +05:30
required: true,
2021-10-27 15:23:28 +05:30
description: 'Global ID of the discussion.'
2020-06-23 00:09:42 +05:30
argument :resolve,
2021-10-27 15:23:28 +05:30
GraphQL::Types::Boolean,
2020-06-23 00:09:42 +05:30
required: true,
2021-03-08 18:12:59 +05:30
description: 'Will resolve the discussion when true, and unresolve the discussion when false.'
2020-06-23 00:09:42 +05:30
field :discussion,
Types::Notes::DiscussionType,
null: true,
2021-10-27 15:23:28 +05:30
description: 'Discussion after mutation.'
2020-06-23 00:09:42 +05:30
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:)
2021-01-03 14:25:43 +05:30
GitlabSchema.find_by_gid(id)
2020-06-23 00:09:42 +05:30
end
def resolve!(discussion)
::Discussions::ResolveService.new(
discussion.project,
current_user,
one_or_more_discussions: discussion
).execute
end
def unresolve!(discussion)
2021-03-11 19:13:27 +05:30
::Discussions::UnresolveService.new(discussion, current_user).execute
2020-06-23 00:09:42 +05:30
end
end
end
end