souls 0.22.6 → 0.22.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/exe/souls +21 -5
  4. data/hoy/.env.sample +7 -0
  5. data/hoy/.gitignore +32 -0
  6. data/hoy/.irbrc +1 -0
  7. data/hoy/.rspec +3 -0
  8. data/hoy/.rubocop.yml +132 -0
  9. data/hoy/.ruby-version +1 -0
  10. data/hoy/CODE_OF_CONDUCT.md +74 -0
  11. data/hoy/Dockerfile +16 -0
  12. data/hoy/Dockerfile.dev +17 -0
  13. data/hoy/Gemfile +50 -0
  14. data/hoy/Gemfile.lock +407 -0
  15. data/hoy/LICENSE.txt +67 -0
  16. data/hoy/Procfile +2 -0
  17. data/hoy/Procfile.dev +2 -0
  18. data/hoy/README.md +61 -0
  19. data/hoy/Rakefile +5 -0
  20. data/hoy/app.rb +116 -0
  21. data/hoy/app/graphql/mutations/.keep +0 -0
  22. data/hoy/app/graphql/mutations/base/article/create_article.rb +30 -0
  23. data/hoy/app/graphql/mutations/base/article/delete_article.rb +17 -0
  24. data/hoy/app/graphql/mutations/base/article/destroy_delete_article.rb +17 -0
  25. data/hoy/app/graphql/mutations/base/article/update_article.rb +30 -0
  26. data/hoy/app/graphql/mutations/base/article_category/create_article_category.rb +21 -0
  27. data/hoy/app/graphql/mutations/base/article_category/delete_article_category.rb +17 -0
  28. data/hoy/app/graphql/mutations/base/article_category/destroy_delete_article_category.rb +17 -0
  29. data/hoy/app/graphql/mutations/base/article_category/update_article_category.rb +21 -0
  30. data/hoy/app/graphql/mutations/base/user/create_user.rb +31 -0
  31. data/hoy/app/graphql/mutations/base/user/delete_user.rb +17 -0
  32. data/hoy/app/graphql/mutations/base/user/destroy_delete_user.rb +17 -0
  33. data/hoy/app/graphql/mutations/base/user/update_user.rb +31 -0
  34. data/hoy/app/graphql/mutations/base_mutation.rb +33 -0
  35. data/hoy/app/graphql/mutations/user_manager/add_user_role.rb +22 -0
  36. data/hoy/app/graphql/mutations/user_manager/remove_user_role.rb +22 -0
  37. data/hoy/app/graphql/mutations/user_manager/sign_in_user.rb +45 -0
  38. data/hoy/app/graphql/queries/article.rb +13 -0
  39. data/hoy/app/graphql/queries/article_categories.rb +11 -0
  40. data/hoy/app/graphql/queries/article_category.rb +13 -0
  41. data/hoy/app/graphql/queries/articles.rb +11 -0
  42. data/hoy/app/graphql/queries/base_query.rb +9 -0
  43. data/hoy/app/graphql/queries/me.rb +11 -0
  44. data/hoy/app/graphql/queries/user.rb +13 -0
  45. data/hoy/app/graphql/queries/users.rb +11 -0
  46. data/hoy/app/graphql/resolvers/article_category_search.rb +41 -0
  47. data/hoy/app/graphql/resolvers/article_search.rb +57 -0
  48. data/hoy/app/graphql/resolvers/base.rb +17 -0
  49. data/hoy/app/graphql/resolvers/user_search.rb +63 -0
  50. data/hoy/app/graphql/souls_api_schema.rb +43 -0
  51. data/hoy/app/graphql/types/.keep +0 -0
  52. data/hoy/app/graphql/types/article_category_type.rb +12 -0
  53. data/hoy/app/graphql/types/article_type.rb +30 -0
  54. data/hoy/app/graphql/types/base/base_argument.rb +4 -0
  55. data/hoy/app/graphql/types/base/base_enum.rb +4 -0
  56. data/hoy/app/graphql/types/base/base_field.rb +5 -0
  57. data/hoy/app/graphql/types/base/base_input_object.rb +5 -0
  58. data/hoy/app/graphql/types/base/base_interface.rb +7 -0
  59. data/hoy/app/graphql/types/base/base_object.rb +6 -0
  60. data/hoy/app/graphql/types/base/base_scalar.rb +4 -0
  61. data/hoy/app/graphql/types/base/base_union.rb +4 -0
  62. data/hoy/app/graphql/types/base/mutation_type.rb +16 -0
  63. data/hoy/app/graphql/types/base/query_type.rb +18 -0
  64. data/hoy/app/graphql/types/connections/article_category_connection.rb +3 -0
  65. data/hoy/app/graphql/types/connections/article_connection.rb +3 -0
  66. data/hoy/app/graphql/types/connections/base_connection.rb +14 -0
  67. data/hoy/app/graphql/types/connections/user_connection.rb +3 -0
  68. data/hoy/app/graphql/types/edges/article_category_edge.rb +5 -0
  69. data/hoy/app/graphql/types/edges/article_edge.rb +5 -0
  70. data/hoy/app/graphql/types/edges/base_edge.rb +4 -0
  71. data/hoy/app/graphql/types/edges/user_edge.rb +5 -0
  72. data/hoy/app/graphql/types/user_type.rb +22 -0
  73. data/hoy/app/models/article.rb +4 -0
  74. data/hoy/app/models/article_category.rb +3 -0
  75. data/hoy/app/models/user.rb +19 -0
  76. data/hoy/app/policies/application_policy.rb +40 -0
  77. data/hoy/app/policies/article_category_policy.rb +31 -0
  78. data/hoy/app/policies/article_policy.rb +31 -0
  79. data/hoy/app/policies/user_policy.rb +35 -0
  80. data/hoy/app/utils/association_loader.rb +50 -0
  81. data/hoy/app/utils/firebase_id_token.rb +4 -0
  82. data/hoy/app/utils/json_web_token.rb +13 -0
  83. data/hoy/app/utils/record_loader.rb +10 -0
  84. data/hoy/app/utils/souls_helper.rb +96 -0
  85. data/hoy/cloudbuild.yml +32 -0
  86. data/hoy/config.ru +17 -0
  87. data/hoy/config/database.yml +33 -0
  88. data/hoy/config/souls.rb +4 -0
  89. data/hoy/constants/areas.rb +71 -0
  90. data/hoy/constants/column_name_ja.rb +27 -0
  91. data/hoy/db/migrate/20200006095538_create_users.rb +30 -0
  92. data/hoy/db/migrate/20200712180236_create_article_categories.rb +12 -0
  93. data/hoy/db/migrate/20200714215521_create_articles.rb +22 -0
  94. data/hoy/db/schema.rb +78 -0
  95. data/hoy/db/seeds.rb +44 -0
  96. data/hoy/github/workflows/delivery.yml +81 -0
  97. data/hoy/log/.keep +0 -0
  98. data/hoy/spec/factories/article_categories.rb +9 -0
  99. data/hoy/spec/factories/articles.rb +17 -0
  100. data/hoy/spec/factories/users.rb +23 -0
  101. data/hoy/spec/models/article_category_spec.rb +7 -0
  102. data/hoy/spec/models/article_spec.rb +7 -0
  103. data/hoy/spec/models/user_spec.rb +7 -0
  104. data/hoy/spec/mutations/base/article_category_spec.rb +46 -0
  105. data/hoy/spec/mutations/base/article_spec.rb +70 -0
  106. data/hoy/spec/mutations/base/user_spec.rb +76 -0
  107. data/hoy/spec/policies/article_category_policy_spec.rb +25 -0
  108. data/hoy/spec/policies/article_policy_spec.rb +25 -0
  109. data/hoy/spec/policies/user_policy_spec.rb +5 -0
  110. data/hoy/spec/queries/article_category_spec.rb +39 -0
  111. data/hoy/spec/queries/article_spec.rb +53 -0
  112. data/hoy/spec/queries/user_spec.rb +59 -0
  113. data/hoy/spec/resolvers/article_category_search_spec.rb +54 -0
  114. data/hoy/spec/resolvers/article_search_spec.rb +68 -0
  115. data/hoy/spec/resolvers/user_search_spec.rb +74 -0
  116. data/hoy/spec/spec_helper.rb +110 -0
  117. data/hoy/tmp/.keep +0 -0
  118. data/lib/souls/init.rb +16 -9
  119. data/lib/souls/version.rb +1 -1
  120. metadata +115 -1
@@ -0,0 +1,31 @@
1
+ module Mutations
2
+ module Base::User
3
+ class CreateUser < BaseMutation
4
+ field :error, String, null: true
5
+ field :user_edge, Types::UserType.edge_type, null: false
6
+
7
+ argument :birthday, String, required: false
8
+ argument :email, String, required: false
9
+ argument :first_name, String, required: false
10
+ argument :first_name_kana, String, required: false
11
+ argument :first_name_kanji, String, required: false
12
+ argument :icon_url, String, required: false
13
+ argument :last_name, String, required: false
14
+ argument :last_name_kana, String, required: false
15
+ argument :last_name_kanji, String, required: false
16
+ argument :screen_name, String, required: false
17
+ argument :tel, String, required: false
18
+ argument :uid, String, required: false
19
+ argument :username, String, required: false
20
+
21
+ def resolve(**args)
22
+ data = ::User.new(args)
23
+ raise(StandardError, data.errors.full_messages) unless data.save
24
+
25
+ { user_edge: { node: data } }
26
+ rescue StandardError => e
27
+ GraphQL::ExecutionError.new(e)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,17 @@
1
+ module Mutations
2
+ module Base::User
3
+ class DeleteUser < BaseMutation
4
+ field :user, Types::UserType, null: false
5
+ argument :id, String, required: true
6
+
7
+ def resolve(**args)
8
+ _, data_id = SoulsApiSchema.from_global_id(args[:id])
9
+ user = ::User.find(data_id)
10
+ user.update(is_deleted: true)
11
+ { user: ::User.find(data_id) }
12
+ rescue StandardError => e
13
+ GraphQL::ExecutionError.new(e)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module Mutations
2
+ module Base::User
3
+ class DestroyDeleteUser < BaseMutation
4
+ field :user, Types::UserType, null: false
5
+ argument :id, String, required: true
6
+
7
+ def resolve(**args)
8
+ _, data_id = SoulsApiSchema.from_global_id(args[:id])
9
+ user = ::User.find(data_id)
10
+ user.destroy
11
+ { user: user }
12
+ rescue StandardError => e
13
+ GraphQL::ExecutionError.new(e)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,31 @@
1
+ module Mutations
2
+ module Base::User
3
+ class UpdateUser < BaseMutation
4
+ field :user_edge, Types::UserType.edge_type, null: false
5
+
6
+ argument :birthday, String, required: false
7
+ argument :email, String, required: false
8
+ argument :first_name, String, required: false
9
+ argument :first_name_kana, String, required: false
10
+ argument :first_name_kanji, String, required: false
11
+ argument :icon_url, String, required: false
12
+ argument :id, String, required: true
13
+ argument :last_name, String, required: false
14
+ argument :last_name_kana, String, required: false
15
+ argument :last_name_kanji, String, required: false
16
+ argument :screen_name, String, required: false
17
+ argument :tel, String, required: false
18
+ argument :uid, String, required: false
19
+ argument :username, String, required: false
20
+
21
+ def resolve(**args)
22
+ _, args[:id] = SoulsApiSchema.from_global_id(args[:id])
23
+ user = ::User.find(args[:id])
24
+ user.update(args)
25
+ { user_edge: { node: ::User.find(args[:id]) } }
26
+ rescue StandardError => e
27
+ GraphQL::ExecutionError.new(e)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,33 @@
1
+ module Mutations
2
+ class BaseMutation < GraphQL::Schema::RelayClassicMutation
3
+ argument_class Types::BaseArgument
4
+ field_class Types::BaseField
5
+ input_object_class Types::BaseInputObject
6
+ object_class Types::BaseObject
7
+
8
+ def fb_auth(token:)
9
+ FirebaseIdToken::Certificates.request!
10
+ sleep(3) if ENV["RACK_ENV"] == "development"
11
+ @payload = FirebaseIdToken::Signature.verify(token)
12
+ raise(ArgumentError, "Invalid or Missing Token") if @payload.blank?
13
+
14
+ @payload
15
+ end
16
+
17
+ def auth_check(context)
18
+ raise(GraphQL::ExecutionError, "You need to sign in!!") if context[:user].nil?
19
+ end
20
+
21
+ def get_token(token)
22
+ JsonWebToken.decode(token)
23
+ end
24
+
25
+ def production?
26
+ ENV["RACK_ENV"] == "production"
27
+ end
28
+
29
+ def get_instance_id
30
+ `curl http://metadata.google.internal/computeMetadata/v1/instance/id -H Metadata-Flavor:Google`
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,22 @@
1
+ module Mutations
2
+ module UserManager
3
+ class AddUserRole < BaseMutation
4
+ argument :target_user_id, String, required: true
5
+ argument :user_roles, [String], required: true
6
+
7
+ field :user, Types::UserType, null: true
8
+
9
+ def resolve(**args)
10
+ check_user_permissions(context[:user], context[:user], :update_user_role?)
11
+ _, user_id = SoulsApiSchema.from_global_id(args[:target_user_id])
12
+ target_user = ::User.find(user_id)
13
+ target_user.roles << args[:user_roles].map(&:to_sym)
14
+ return { user: target_user } if target_user.save
15
+
16
+ raise
17
+ rescue StandardError => e
18
+ GraphQL::ExecutionError.new(e.to_s)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,22 @@
1
+ module Mutations
2
+ module UserManager
3
+ class RemoveUserRole < BaseMutation
4
+ argument :target_user_id, String, required: true
5
+ argument :user_roles, [String], required: true
6
+
7
+ field :user, Types::UserType, null: true
8
+
9
+ def resolve(**args)
10
+ check_user_permissions(context[:user], context[:user], :update_user_role?)
11
+ _, user_id = SoulsApiSchema.from_global_id(args[:target_user_id])
12
+ target_user = ::User.find(user_id)
13
+ args[:user_roles].each { |role| target_user.roles.delete(role) }
14
+ return { user: target_user } if target_user.save
15
+
16
+ raise
17
+ rescue StandardError => e
18
+ GraphQL::ExecutionError.new(e.to_s)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,45 @@
1
+ module Mutations
2
+ module UserManager
3
+ class SignInUser < BaseMutation
4
+ field :status, String, null: false
5
+ field :token, String, null: true
6
+ field :user_role, String, null: true
7
+ field :username, String, null: true
8
+ argument :token, String, required: false
9
+
10
+ def resolve(token:)
11
+ fb_auth(token: token)
12
+ begin
13
+ user = ::User.find_by_uid(@payload["sub"])
14
+ user.update(icon_url: @payload["picture"], username: @payload["name"])
15
+ token_base = JsonWebToken.encode(user_id: user.id)
16
+ {
17
+ status: "ログイン成功!",
18
+ username: user.username,
19
+ token: token_base
20
+ }
21
+ rescue StandardError
22
+ user =
23
+ ::User.new(
24
+ uid: @payload["sub"],
25
+ email: @payload["email"],
26
+ icon_url: @payload["picture"],
27
+ username: @payload["name"],
28
+ user_role: 4
29
+ )
30
+ if user.save
31
+ token = JsonWebToken.encode(user_id: user.id)
32
+ {
33
+ status: "ユーザー新規登録完了!",
34
+ username: user.username,
35
+ token: token,
36
+ user_role: user.user_role
37
+ }
38
+ else
39
+ { status: user.errors.full_messages }
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,13 @@
1
+ module Queries
2
+ class Article < Queries::BaseQuery
3
+ type Types::ArticleType, null: false
4
+ argument :id, String, required: true
5
+
6
+ def resolve(**args)
7
+ _, data_id = SoulsApiSchema.from_global_id(args[:id])
8
+ ::Article.find(data_id)
9
+ rescue StandardError => e
10
+ GraphQL::ExecutionError.new(e)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module Queries
2
+ class ArticleCategories < Queries::BaseQuery
3
+ type [Types::ArticleCategoryType], null: false
4
+
5
+ def resolve
6
+ ::ArticleCategory.all
7
+ rescue StandardError => e
8
+ GraphQL::ExecutionError.new(e)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module Queries
2
+ class ArticleCategory < Queries::BaseQuery
3
+ type Types::ArticleCategoryType, null: false
4
+ argument :id, String, required: true
5
+
6
+ def resolve(**args)
7
+ _, data_id = SoulsApiSchema.from_global_id(args[:id])
8
+ ::ArticleCategory.find(data_id)
9
+ rescue StandardError => e
10
+ GraphQL::ExecutionError.new(e)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module Queries
2
+ class Articles < Queries::BaseQuery
3
+ type [Types::ArticleType], null: false
4
+
5
+ def resolve
6
+ ::Article.all
7
+ rescue StandardError => e
8
+ GraphQL::ExecutionError.new(e)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module Queries
2
+ class BaseQuery < GraphQL::Schema::Resolver
3
+ def blog_host
4
+ return "localhost:50051" if Sinatra.env.development? || Sinatra.env.test?
5
+
6
+ ENV["GRPC_SERVER_URL1"]
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,11 @@
1
+ module Queries
2
+ class Me < Queries::BaseQuery
3
+ type Types::UserType, null: false
4
+
5
+ def resolve
6
+ ::User.find(context[:user].id)
7
+ rescue StandardError => e
8
+ GraphQL::ExecutionError.new(e)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module Queries
2
+ class User < Queries::BaseQuery
3
+ type Types::UserType, null: false
4
+ argument :id, String, required: true
5
+
6
+ def resolve(**args)
7
+ _, data_id = SoulsApiSchema.from_global_id(args[:id])
8
+ ::User.find(data_id)
9
+ rescue StandardError => e
10
+ GraphQL::ExecutionError.new(e)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ module Queries
2
+ class Users < Queries::BaseQuery
3
+ type [Types::UserType], null: false
4
+
5
+ def resolve
6
+ ::User.all
7
+ rescue StandardError => e
8
+ GraphQL::ExecutionError.new(e)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,41 @@
1
+ module Resolvers
2
+ class ArticleCategorySearch < Base
3
+ include SearchObject.module(:graphql)
4
+ scope { ::ArticleCategory.all }
5
+ type Types::ArticleCategoryType.connection_type, null: false
6
+ description "Search ArticleCategory"
7
+
8
+ class ArticleCategoryFilter < ::Types::BaseInputObject
9
+ argument :OR, [self], required: false
10
+ argument :end_date, String, required: false
11
+ argument :is_deleted, Boolean, required: false
12
+ argument :name, String, required: false
13
+ argument :start_date, String, required: false
14
+ argument :tags, [String], required: false
15
+ end
16
+
17
+ option :filter, type: ArticleCategoryFilter, with: :apply_filter
18
+ option :first, type: types.Int, with: :apply_first
19
+ option :skip, type: types.Int, with: :apply_skip
20
+
21
+ def apply_filter(scope, value)
22
+ branches = normalize_filters(value).inject { |acc, elem| acc.or(elem) }
23
+ scope.merge(branches)
24
+ end
25
+
26
+ def normalize_filters(value, branches = [])
27
+ scope = ::ArticleCategory.all
28
+ scope = scope.where(name: value[:name]) if value[:name]
29
+ scope = scope.where("tags @> ARRAY[?]::text[]", value[:tags]) if value[:tags]
30
+ scope = scope.where(is_deleted: value[:is_deleted]) unless value[:is_deleted].nil?
31
+ scope = scope.where("created_at >= ?", value[:start_date]) if value[:start_date]
32
+ scope = scope.where("created_at <= ?", value[:end_date]) if value[:end_date]
33
+
34
+ branches << scope
35
+
36
+ value[:OR].inject(branches) { |acc, elem| normalize_filters(elem, acc) } if value[:OR].present?
37
+
38
+ branches
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,57 @@
1
+ module Resolvers
2
+ class ArticleSearch < Base
3
+ include SearchObject.module(:graphql)
4
+ scope { ::Article.all }
5
+ type Types::ArticleType.connection_type, null: false
6
+ description "Search Article"
7
+
8
+ class ArticleFilter < ::Types::BaseInputObject
9
+ argument :OR, [self], required: false
10
+ argument :article_category_id, String, required: false
11
+ argument :body, String, required: false
12
+ argument :end_date, String, required: false
13
+ argument :is_deleted, Boolean, required: false
14
+ argument :is_public, Boolean, required: false
15
+ argument :just_created, Boolean, required: false
16
+ argument :public_date, GraphQL::Types::ISO8601DateTime, required: false
17
+ argument :slag, String, required: false
18
+ argument :start_date, String, required: false
19
+ argument :tags, [String], required: false
20
+ argument :thumnail_url, String, required: false
21
+ argument :title, String, required: false
22
+ end
23
+
24
+ option :filter, type: ArticleFilter, with: :apply_filter
25
+ option :first, type: types.Int, with: :apply_first
26
+ option :skip, type: types.Int, with: :apply_skip
27
+
28
+ def apply_filter(scope, value)
29
+ branches = normalize_filters(value).inject { |acc, elem| acc.or(elem) }
30
+ scope.merge(branches)
31
+ end
32
+
33
+ def normalize_filters(value, branches = [])
34
+ scope = ::Article.all
35
+ scope = scope.where(title: value[:title]) if value[:title]
36
+ scope = scope.where(body: value[:body]) if value[:body]
37
+ scope = scope.where(thumnail_url: value[:thumnail_url]) if value[:thumnail_url]
38
+ scope = scope.where(public_date: value[:public_date]) if value[:public_date]
39
+ if value[:article_category_id]
40
+ scope = scope.where(article_category_id: decode_global_key(value[:article_category_id]))
41
+ end
42
+ scope = scope.where(is_public: value[:is_public]) unless value[:is_public].nil?
43
+ scope = scope.where(just_created: value[:just_created]) unless value[:just_created].nil?
44
+ scope = scope.where(slag: value[:slag]) if value[:slag]
45
+ scope = scope.where("tags @> ARRAY[?]::text[]", value[:tags]) if value[:tags]
46
+ scope = scope.where(is_deleted: value[:is_deleted]) unless value[:is_deleted].nil?
47
+ scope = scope.where("created_at >= ?", value[:start_date]) if value[:start_date]
48
+ scope = scope.where("created_at <= ?", value[:end_date]) if value[:end_date]
49
+
50
+ branches << scope
51
+
52
+ value[:OR].inject(branches) { |acc, elem| normalize_filters(elem, acc) } if value[:OR].present?
53
+
54
+ branches
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,17 @@
1
+ module Resolvers
2
+ class Base < GraphQL::Schema::Resolver
3
+ # argument_class Arguments::Base
4
+ def decode_global_key(id)
5
+ _, data_id = SoulsApiSchema.from_global_id(id)
6
+ data_id
7
+ end
8
+
9
+ def apply_first(scope, value)
10
+ scope.limit(value)
11
+ end
12
+
13
+ def apply_skip(scope, value)
14
+ scope.offset(value)
15
+ end
16
+ end
17
+ end