From 026e23cd4761f9e873eae2f56f81f5d4c63cebe8 Mon Sep 17 00:00:00 2001 From: Caio Almeida <117518+caiosba@users.noreply.github.com> Date: Wed, 14 Aug 2024 12:12:14 -0300 Subject: [PATCH] Moving `accessible_teams` from `UserType` to `MeType`. (#1991) This information is only needed for the current user, so, it should be under `MeType`, not `UserType`. References: CV2-4938 and CV2-4704. --- app/graph/types/me_type.rb | 14 ++ app/graph/types/user_type.rb | 5 - app/models/concerns/project_media_creators.rb | 4 +- lib/relay.idl | 43 +++--- public/relay.json | 136 ++++++++++-------- .../controllers/graphql_controller_11_test.rb | 16 ++- .../controllers/graphql_controller_12_test.rb | 8 +- 7 files changed, 128 insertions(+), 98 deletions(-) diff --git a/app/graph/types/me_type.rb b/app/graph/types/me_type.rb index 73553552fe..0033ca3d31 100644 --- a/app/graph/types/me_type.rb +++ b/app/graph/types/me_type.rb @@ -133,6 +133,20 @@ def team_users_count(status: nil) team_users(status: status).count end + field :accessible_teams, TeamType.connection_type, null: true + + def accessible_teams + return Team.none unless object == User.current + teams = User.current.is_admin? ? Team.all : User.current.teams.where('team_users.status' => 'member') + teams.order('name ASC') + end + + field :accessible_teams_count, GraphQL::Types::Int, null: true + + def accessible_teams_count + accessible_teams.count + end + field :annotations, AnnotationType.connection_type, null: true do argument :type, GraphQL::Types::String, required: false end diff --git a/app/graph/types/user_type.rb b/app/graph/types/user_type.rb index 86f8c186bf..d14b767670 100644 --- a/app/graph/types/user_type.rb +++ b/app/graph/types/user_type.rb @@ -26,11 +26,6 @@ def profile_image super_admin? ? "#{CheckConfig.get('checkdesk_base_url')}/images/user.png" : object.profile_image end - field :accessible_teams, PublicTeamType.connection_type, null: true - def accessible_teams - User.current.is_admin? ? Team.all : User.current.teams - end - private def super_admin? diff --git a/app/models/concerns/project_media_creators.rb b/app/models/concerns/project_media_creators.rb index 19ccde6d9d..6b4e405b7a 100644 --- a/app/models/concerns/project_media_creators.rb +++ b/app/models/concerns/project_media_creators.rb @@ -255,7 +255,7 @@ def create_claim_description_and_fact_check claim_description: cd, report_status: (fact_check['publish_report'] ? 'published' : 'unpublished'), rating: self.set_status, - tags: self.set_tags, + tags: self.set_tags.to_a.map(&:strip), skip_check_ability: true }) end @@ -263,6 +263,6 @@ def create_claim_description_and_fact_check end def create_tags - self.set_tags.each { |tag| Tag.create!(annotated: self, tag: tag, skip_check_ability: true) } if self.set_tags.is_a?(Array) + self.set_tags.each { |tag| Tag.create!(annotated: self, tag: tag.strip, skip_check_ability: true) } if self.set_tags.is_a?(Array) end end diff --git a/lib/relay.idl b/lib/relay.idl index dfb9aaec5e..4c7445b1ae 100644 --- a/lib/relay.idl +++ b/lib/relay.idl @@ -8899,6 +8899,28 @@ Me type """ type Me implements Node { accepted_terms: Boolean + accessible_teams( + """ + Returns the elements in the list that come after the specified cursor. + """ + after: String + + """ + Returns the elements in the list that come before the specified cursor. + """ + before: String + + """ + Returns the first _n_ elements from the list. + """ + first: Int + + """ + Returns the last _n_ elements from the list. + """ + last: Int + ): TeamConnection + accessible_teams_count: Int annotations( """ Returns the elements in the list that come after the specified cursor. @@ -16258,27 +16280,6 @@ type UpdateUserPayload { User type """ type User implements Node { - accessible_teams( - """ - Returns the elements in the list that come after the specified cursor. - """ - after: String - - """ - Returns the elements in the list that come before the specified cursor. - """ - before: String - - """ - Returns the first _n_ elements from the list. - """ - first: Int - - """ - Returns the last _n_ elements from the list. - """ - last: Int - ): PublicTeamConnection created_at: String dbid: Int email: String diff --git a/public/relay.json b/public/relay.json index ee7d71faff..10d08ecdd4 100644 --- a/public/relay.json +++ b/public/relay.json @@ -48439,6 +48439,81 @@ "isDeprecated": false, "deprecationReason": null }, + { + "name": "accessible_teams", + "description": null, + "args": [ + { + "name": "after", + "description": "Returns the elements in the list that come after the specified cursor.", + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "defaultValue": null, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "before", + "description": "Returns the elements in the list that come before the specified cursor.", + "type": { + "kind": "SCALAR", + "name": "String", + "ofType": null + }, + "defaultValue": null, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "first", + "description": "Returns the first _n_ elements from the list.", + "type": { + "kind": "SCALAR", + "name": "Int", + "ofType": null + }, + "defaultValue": null, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "last", + "description": "Returns the last _n_ elements from the list.", + "type": { + "kind": "SCALAR", + "name": "Int", + "ofType": null + }, + "defaultValue": null, + "isDeprecated": false, + "deprecationReason": null + } + ], + "type": { + "kind": "OBJECT", + "name": "TeamConnection", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, + { + "name": "accessible_teams_count", + "description": null, + "args": [ + + ], + "type": { + "kind": "SCALAR", + "name": "Int", + "ofType": null + }, + "isDeprecated": false, + "deprecationReason": null + }, { "name": "annotations", "description": null, @@ -89789,67 +89864,6 @@ "name": "User", "description": "User type", "fields": [ - { - "name": "accessible_teams", - "description": null, - "args": [ - { - "name": "after", - "description": "Returns the elements in the list that come after the specified cursor.", - "type": { - "kind": "SCALAR", - "name": "String", - "ofType": null - }, - "defaultValue": null, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "before", - "description": "Returns the elements in the list that come before the specified cursor.", - "type": { - "kind": "SCALAR", - "name": "String", - "ofType": null - }, - "defaultValue": null, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "first", - "description": "Returns the first _n_ elements from the list.", - "type": { - "kind": "SCALAR", - "name": "Int", - "ofType": null - }, - "defaultValue": null, - "isDeprecated": false, - "deprecationReason": null - }, - { - "name": "last", - "description": "Returns the last _n_ elements from the list.", - "type": { - "kind": "SCALAR", - "name": "Int", - "ofType": null - }, - "defaultValue": null, - "isDeprecated": false, - "deprecationReason": null - } - ], - "type": { - "kind": "OBJECT", - "name": "PublicTeamConnection", - "ofType": null - }, - "isDeprecated": false, - "deprecationReason": null - }, { "name": "created_at", "description": null, diff --git a/test/controllers/graphql_controller_11_test.rb b/test/controllers/graphql_controller_11_test.rb index 38f77e011e..af6670ab50 100644 --- a/test/controllers/graphql_controller_11_test.rb +++ b/test/controllers/graphql_controller_11_test.rb @@ -116,7 +116,7 @@ def teardown assert_not_nil data['project_media']['id'] end - test "admin users should be able to see all workspaces" do + test "admin users should be able to see all workspaces as accessible teams" do Team.destroy_all user = create_user @@ -128,16 +128,18 @@ def teardown create_team_user user: admin, team: team2 authenticate_with_user(admin) - query = "query { user(id: #{admin.id}) { accessible_teams { edges { node { dbid } } } } }" + query = "query { me { accessible_teams_count, accessible_teams { edges { node { dbid } } } } }" post :create, params: { query: query } assert_response :success - data = JSON.parse(response.body)['data']['user']['accessible_teams']['edges'] + response = JSON.parse(@response.body)['data']['me'] + data = response['accessible_teams']['edges'] assert_equal 2, data.size assert_equal team1.id, data[0]['node']['dbid'] assert_equal team2.id, data[1]['node']['dbid'] + assert_equal 2, response['accessible_teams_count'] end - test "non-admin users should only be able to see workspaces they belong to" do + test "non-admin users should only be able to see workspaces they belong to as accessible teams" do Team.destroy_all user = create_user team1 = create_team @@ -148,11 +150,13 @@ def teardown create_team_user user: user2, team: team2 authenticate_with_user(user) - query = "query { user(id: #{user.id}) { accessible_teams { edges { node { dbid } } } } }" + query = "query { me { accessible_teams_count, accessible_teams { edges { node { dbid } } } } }" post :create, params: { query: query } assert_response :success - data = JSON.parse(response.body)['data']['user']['accessible_teams']['edges'] + response = JSON.parse(@response.body)['data']['me'] + data = response['accessible_teams']['edges'] assert_equal 1, data.size assert_equal team1.id, data[0]['node']['dbid'] + assert_equal 1, response['accessible_teams_count'] end end diff --git a/test/controllers/graphql_controller_12_test.rb b/test/controllers/graphql_controller_12_test.rb index f998aedfd6..a4e5a9e84d 100644 --- a/test/controllers/graphql_controller_12_test.rb +++ b/test/controllers/graphql_controller_12_test.rb @@ -611,6 +611,7 @@ def teardown end test "should treat ' tag' and 'tag' as the same tag, and not try to create a new tag" do + Sidekiq::Testing.inline! t = create_team a = ApiKey.create! b = create_bot_user api_key_id: a.id @@ -622,7 +623,7 @@ def teardown createProjectMedia(input: { project_id: ' + p.id.to_s + ', media_type: "Blank", - channel: {main: 1}, + channel: { main: 1 }, set_tags: ["science"], set_status: "verified", set_claim_description: "Claim #1.", @@ -647,13 +648,14 @@ def teardown post :create, params: { query: query1, team: t.slug } assert_response :success assert_equal 'science', JSON.parse(@response.body)['data']['createProjectMedia']['project_media']['tags']['edges'][0]['node']['tag_text'] + sleep 1 query2 = ' mutation create { createProjectMedia(input: { project_id: ' + p.id.to_s + ', media_type: "Blank", - channel: {main: 1}, - set_tags: [" science"], + channel: { main: 1 }, + set_tags: ["science "], set_status: "verified", set_claim_description: "Claim #2.", set_fact_check: {