From 9f0df37e54a5eb3250a5b63909bd94ff603df1f3 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Thu, 2 May 2024 18:59:48 +0800 Subject: [PATCH 1/3] chore: wip --- config/mock.exs | 3 +++ lib/groupher_server_web/resolvers/cms_resolver.ex | 4 ++++ lib/groupher_server_web/router.ex | 4 ++-- lib/groupher_server_web/schema/cms/cms_queries.ex | 7 +++++++ lib/groupher_server_web/schema/cms/cms_types.ex | 4 ++++ test/groupher_server/accounts/oauth_test.exs | 2 -- .../mutation/accounts/oauth_test.exs | 9 +++------ test/groupher_server_web/query/cms/cms_test.exs | 15 +++++++++++++++ 8 files changed, 38 insertions(+), 10 deletions(-) diff --git a/config/mock.exs b/config/mock.exs index a4d479a..2388deb 100644 --- a/config/mock.exs +++ b/config/mock.exs @@ -34,6 +34,9 @@ config :groupher_server, GroupherServer.Repo, # config email services config :groupher_server, GroupherServer.Mailer, adapter: Bamboo.LocalAdapter +config :groupher_server, :oauth, + oauth_trust_code: "fWrFuWs1j+TGcrok7XHkwDLiOVTGOnUR3JWF3cbcu2Tcnbj7TvSS1mMVeekvjgNQ" + config :ex_aliyun_openapi, :sts, access_key_id: System.get_env("ALI_OSS_STS_AK"), access_key_secret: System.get_env("_ALIOSS_STS_AS") diff --git a/lib/groupher_server_web/resolvers/cms_resolver.ex b/lib/groupher_server_web/resolvers/cms_resolver.ex index cf5d12e..bf4d469 100644 --- a/lib/groupher_server_web/resolvers/cms_resolver.ex +++ b/lib/groupher_server_web/resolvers/cms_resolver.ex @@ -519,4 +519,8 @@ defmodule GroupherServerWeb.Resolvers.CMS do def upload_tokens(_root, _, _) do CMS.upload_tokens() end + + def client_i18n(_root, _, _) do + {:ok, %{locale: "__ignore_this__"}} + end end diff --git a/lib/groupher_server_web/router.ex b/lib/groupher_server_web/router.ex index 2264e0f..724a1c6 100644 --- a/lib/groupher_server_web/router.ex +++ b/lib/groupher_server_web/router.ex @@ -37,9 +37,9 @@ defmodule GroupherServerWeb.Router do forward( "/", Absinthe.Plug.GraphiQL, - schema: GroupherServerWeb.Schema + schema: GroupherServerWeb.Schema, # json_codec: Jason, - # interface: :playground, + interface: :playground # context: %{pubsub: GroupherServerWeb.Endpoint} ) end diff --git a/lib/groupher_server_web/schema/cms/cms_queries.ex b/lib/groupher_server_web/schema/cms/cms_queries.ex index 1218458..3fd7b23 100644 --- a/lib/groupher_server_web/schema/cms/cms_queries.ex +++ b/lib/groupher_server_web/schema/cms/cms_queries.ex @@ -194,6 +194,13 @@ defmodule GroupherServerWeb.Schema.CMS.Queries do resolve(&R.CMS.paged_kanban_posts/3) end + # this query is only use to pass the graphql type spec, real logic is in fronten + field :client_i18n, :client_locale do + arg(:locale, :string) + + resolve(&R.CMS.client_i18n/3) + end + article_search_queries() article_reacted_users_query(:upvot, &R.CMS.upvoted_users/3) diff --git a/lib/groupher_server_web/schema/cms/cms_types.ex b/lib/groupher_server_web/schema/cms/cms_types.ex index 40452f0..dade240 100644 --- a/lib/groupher_server_web/schema/cms/cms_types.ex +++ b/lib/groupher_server_web/schema/cms/cms_types.ex @@ -419,4 +419,8 @@ defmodule GroupherServerWeb.Schema.CMS.Types do field(:favicon, :string) field(:site_name, :string) end + + object :client_locale do + field(:locale, :string) + end end diff --git a/test/groupher_server/accounts/oauth_test.exs b/test/groupher_server/accounts/oauth_test.exs index b955d67..104daeb 100644 --- a/test/groupher_server/accounts/oauth_test.exs +++ b/test/groupher_server/accounts/oauth_test.exs @@ -91,7 +91,6 @@ defmodule GroupherServer.Test.Accounts.Oauth do assert last.provider == "twitter" end - @tag :wip test "can unlink oauth provider" do user_login = @valid_twitter_profile["login"] github_provider = @valid_github_profile |> Map.put("login", user_login) @@ -110,7 +109,6 @@ defmodule GroupherServer.Test.Accounts.Oauth do assert after_delete.provider == "github" end - @tag :wip test "can not unlink oauth provider if there is only one" do user_login = @valid_twitter_profile["login"] github_provider = @valid_github_profile |> Map.put("login", user_login) diff --git a/test/groupher_server_web/mutation/accounts/oauth_test.exs b/test/groupher_server_web/mutation/accounts/oauth_test.exs index 5aafc65..9152940 100644 --- a/test/groupher_server_web/mutation/accounts/oauth_test.exs +++ b/test/groupher_server_web/mutation/accounts/oauth_test.exs @@ -32,7 +32,7 @@ defmodule GroupherServer.Test.Mutation.Account.Oauth do } } """ - @tag :wip + test "can signin oauth with github", ~m(guest_conn)a do variables = %{ provider: @valid_github_profile, @@ -63,7 +63,7 @@ defmodule GroupherServer.Test.Mutation.Account.Oauth do } } """ - @tag :wip + test "can link oauth with twitter", ~m(user_conn user)a do github_provider = @valid_github_profile |> Map.put("login", user.login) @@ -77,7 +77,6 @@ defmodule GroupherServer.Test.Mutation.Account.Oauth do assert ret["user"]["login"] == user.login end - @tag :wip test "can not link oauth with twitter with unlogged", ~m(guest_conn user)a do github_provider = @valid_github_profile |> Map.put("login", user.login) @@ -89,7 +88,6 @@ defmodule GroupherServer.Test.Mutation.Account.Oauth do assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login)) end - @tag :wip test "can not link oauth with un-trust code", ~m(user_conn user)a do github_provider = @valid_github_profile |> Map.put("login", user.login) @@ -110,7 +108,7 @@ defmodule GroupherServer.Test.Mutation.Account.Oauth do } } """ - @tag :wip + test "can unlink oauth with provider", ~m(user_conn user)a do github_provider = @valid_github_profile |> Map.put("login", user.login) twitter_provider = @valid_twitter_profile |> Map.put("login", user.login) @@ -128,7 +126,6 @@ defmodule GroupherServer.Test.Mutation.Account.Oauth do assert ret["login"] == user.login end - @tag :wip test "can not unlink oauth with provider when unlogged in", ~m(guest_conn user)a do github_provider = @valid_github_profile |> Map.put("login", user.login) twitter_provider = @valid_twitter_profile |> Map.put("login", user.login) diff --git a/test/groupher_server_web/query/cms/cms_test.exs b/test/groupher_server_web/query/cms/cms_test.exs index 00132d1..d70813d 100644 --- a/test/groupher_server_web/query/cms/cms_test.exs +++ b/test/groupher_server_web/query/cms/cms_test.exs @@ -593,4 +593,19 @@ defmodule GroupherServer.Test.Query.CMS.Basic do assert not is_nil(results["siteName"]) end end + + @query """ + query($locale: String!) { + clientI18n(locale: $locale) { + locale + } + } + """ + @tag :wip + test "fake client i18n test", ~m(guest_conn)a do + variables = %{locale: "en"} + results = guest_conn |> query_result(@query, variables, "clientI18n") + + assert results["locale"] == "__ignore_this__" + end end From cdf67ff2db62427040f8fdc7202410fc854d341e Mon Sep 17 00:00:00 2001 From: mydearxym Date: Fri, 3 May 2024 10:58:33 +0800 Subject: [PATCH 2/3] feat(community): add locale field --- lib/groupher_server/cms/models/community.ex | 3 ++- .../schema/cms/cms_types.ex | 1 + .../schema/cms/mutations/community.ex | 3 +++ lib/support/factory.ex | 3 ++- .../20240502110245_add_locale_to_community.exs | 9 +++++++++ test/groupher_server/accounts/oauth_test.exs | 2 +- .../cms/community/community_test.exs | 4 +++- .../mutation/cms/crud_test.exs | 18 ++++++++++++------ .../query/cms/third_part_test.exs | 2 +- 9 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 priv/repo/migrations/20240502110245_add_locale_to_community.exs diff --git a/lib/groupher_server/cms/models/community.ex b/lib/groupher_server/cms/models/community.ex index 9b05539..630e383 100644 --- a/lib/groupher_server/cms/models/community.ex +++ b/lib/groupher_server/cms/models/community.ex @@ -27,7 +27,7 @@ defmodule GroupherServer.CMS.Model.Community do @required_fields ~w(title desc user_id logo slug)a # @required_fields ~w(title desc user_id)a - @optional_fields ~w(favicon label geo_info index aka contributes_digest pending)a + @optional_fields ~w(favicon label geo_info index aka contributes_digest pending locale)a def max_pinned_article_count_per_thread, do: @max_pinned_article_count_per_thread @@ -43,6 +43,7 @@ defmodule GroupherServer.CMS.Model.Community do field(:index, :integer) field(:geo_info, :map) field(:views, :integer) + field(:locale, :string) embeds_one(:meta, Embeds.CommunityMeta, on_replace: :delete) belongs_to(:author, User, foreign_key: :user_id) diff --git a/lib/groupher_server_web/schema/cms/cms_types.ex b/lib/groupher_server_web/schema/cms/cms_types.ex index dade240..c14aca1 100644 --- a/lib/groupher_server_web/schema/cms/cms_types.ex +++ b/lib/groupher_server_web/schema/cms/cms_types.ex @@ -185,6 +185,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do field(:index, :integer) field(:logo, :string) field(:author, :user, resolve: dataloader(CMS, :author)) + field(:locale, :string) field(:threads, list_of(:thread_item), resolve: dataloader(CMS, :threads)) field(:categories, list_of(:category), resolve: dataloader(CMS, :categories)) field(:dashboard, :dashboard, resolve: dataloader(CMS, :dashboard)) diff --git a/lib/groupher_server_web/schema/cms/mutations/community.ex b/lib/groupher_server_web/schema/cms/mutations/community.ex index 82af080..fcb7837 100644 --- a/lib/groupher_server_web/schema/cms/mutations/community.ex +++ b/lib/groupher_server_web/schema/cms/mutations/community.ex @@ -11,6 +11,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Community do arg(:desc, non_null(:string)) arg(:slug, non_null(:string)) arg(:logo, non_null(:string)) + arg(:locale, :string) middleware(M.Authorize, :login) middleware(M.Passport, claim: "cms->community.create") @@ -26,6 +27,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Community do arg(:desc, :string) arg(:slug, :string) arg(:logo, :string) + arg(:locale, :string) middleware(M.Authorize, :login) middleware(M.Passport, claim: "cms->community.update") @@ -48,6 +50,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Community do arg(:desc, non_null(:string)) arg(:slug, non_null(:string)) arg(:logo, non_null(:string)) + arg(:locale, :string) arg(:apply_msg, :string) arg(:apply_category, :string) diff --git a/lib/support/factory.ex b/lib/support/factory.ex index c9e8083..73bd6b4 100644 --- a/lib/support/factory.ex +++ b/lib/support/factory.ex @@ -216,7 +216,8 @@ defmodule GroupherServer.Support.Factory do desc: "community desc", slug: title, logo: "https://coderplanets.oss-cn-beijing.aliyuncs.com/icons/pl/elixir.svg", - author: mock(:user) + author: mock(:user), + locale: "en" } end diff --git a/priv/repo/migrations/20240502110245_add_locale_to_community.exs b/priv/repo/migrations/20240502110245_add_locale_to_community.exs new file mode 100644 index 0000000..062c7a5 --- /dev/null +++ b/priv/repo/migrations/20240502110245_add_locale_to_community.exs @@ -0,0 +1,9 @@ +defmodule GroupherServer.Repo.Migrations.AddLocaleToCommunity do + use Ecto.Migration + + def change do + alter table(:communities, prefix: "cms") do + add(:locale, :string) + end + end +end diff --git a/test/groupher_server/accounts/oauth_test.exs b/test/groupher_server/accounts/oauth_test.exs index 104daeb..c7efcb5 100644 --- a/test/groupher_server/accounts/oauth_test.exs +++ b/test/groupher_server/accounts/oauth_test.exs @@ -3,7 +3,7 @@ defmodule GroupherServer.Test.Accounts.Oauth do # TODO import Service.Utils move both helper and github import Helper.Utils - alias Helper.{Guardian, ORM} + alias Helper.ORM alias GroupherServer.Accounts alias Accounts.Model.{User, OauthProvider} diff --git a/test/groupher_server/cms/community/community_test.exs b/test/groupher_server/cms/community/community_test.exs index 01ea7bd..4f4770b 100644 --- a/test/groupher_server/cms/community/community_test.exs +++ b/test/groupher_server/cms/community/community_test.exs @@ -26,7 +26,8 @@ defmodule GroupherServer.Test.CMS.Community do end describe "[cms community curd]" do - test "new created community should have default threads", ~m(user)a do + @tag :wip + test "new created community should have default threads & locale", ~m(user)a do community_attrs = mock_attrs(:community, %{slug: "elixir", user_id: user.id}) {:ok, community} = CMS.create_community(community_attrs) @@ -35,6 +36,7 @@ defmodule GroupherServer.Test.CMS.Community do {:ok, community} = ORM.find(Community, community.id, preload: :threads) + assert community.locale == "en" assert community.threads |> length == 5 end end diff --git a/test/groupher_server_web/mutation/cms/crud_test.exs b/test/groupher_server_web/mutation/cms/crud_test.exs index 4c138c1..882a87c 100644 --- a/test/groupher_server_web/mutation/cms/crud_test.exs +++ b/test/groupher_server_web/mutation/cms/crud_test.exs @@ -196,11 +196,12 @@ defmodule GroupherServer.Test.Mutation.CMS.CRUD do describe "[mutation cms community]" do @create_community_query """ - mutation($title: String!, $desc: String!, $logo: String!, $slug: String!) { - createCommunity(title: $title, desc: $desc, logo: $logo, slug: $slug) { + mutation($title: String!, $desc: String!, $logo: String!, $slug: String!, $locale: String) { + createCommunity(title: $title, desc: $desc, logo: $logo, slug: $slug, locale: $locale) { id title desc + locale author { id } @@ -212,15 +213,17 @@ defmodule GroupherServer.Test.Mutation.CMS.CRUD do } } """ + @tag :wip test "create community with valid attrs" do rule_conn = simu_conn(:user, cms: %{"community.create" => true}) - variables = mock_attrs(:community) + variables = mock_attrs(:community, %{locale: "zh"}) created = rule_conn |> mutation_result(@create_community_query, variables, "createCommunity") {:ok, found} = Community |> ORM.find(created["id"]) assert created["id"] == to_string(found.id) + assert created["locale"] == "zh" end test "created community should have default threads" do @@ -734,9 +737,10 @@ defmodule GroupherServer.Test.Mutation.CMS.CRUD do describe "mutation cms community apply" do @apply_community_query """ - mutation($title: String!, $desc: String!, $logo: String!, $slug: String!, $applyMsg: String, $applyCategory: String) { - applyCommunity(title: $title, desc: $desc, logo: $logo, slug: $slug, applyMsg: $applyMsg, applyCategory: $applyCategory) { + mutation($title: String!, $desc: String!, $logo: String!, $slug: String!, $applyMsg: String, $applyCategory: String, $locale: String) { + applyCommunity(title: $title, desc: $desc, logo: $logo, slug: $slug, applyMsg: $applyMsg, applyCategory: $applyCategory, locale: $locale) { id + locale moderators { role user { @@ -754,8 +758,9 @@ defmodule GroupherServer.Test.Mutation.CMS.CRUD do } } """ + @tag :wip test "apply a community should have default root user", ~m(user_conn)a do - variables = mock_attrs(:community) + variables = mock_attrs(:community, %{locale: "it"}) created = user_conn |> mutation_result(@apply_community_query, variables, "applyCommunity") {:ok, found} = Community |> ORM.find(created["id"]) @@ -764,6 +769,7 @@ defmodule GroupherServer.Test.Mutation.CMS.CRUD do moderator = created["moderators"] |> Enum.at(0) assert moderator["role"] == "root" + assert created["locale"] == "it" end @approve_community_query """ diff --git a/test/groupher_server_web/query/cms/third_part_test.exs b/test/groupher_server_web/query/cms/third_part_test.exs index b27e780..17c6bd3 100644 --- a/test/groupher_server_web/query/cms/third_part_test.exs +++ b/test/groupher_server_web/query/cms/third_part_test.exs @@ -3,7 +3,7 @@ defmodule GroupherServer.Test.Query.CMS.ThirdPart do alias GroupherServer.Accounts.Model.User # alias GroupherServer.CMS - alias Helper.ORM + # alias Helper.ORM setup do guest_conn = simu_conn(:guest) From 328213c1d86b3fa1cb655fd7b740b5a7aac58478 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Mon, 6 May 2024 09:29:54 +0800 Subject: [PATCH 3/3] feat(community): add locale --- lib/groupher_server/cms/delegates/community_crud.ex | 2 +- lib/groupher_server/cms/models/community.ex | 3 ++- lib/groupher_server/cms/models/metrics/dashboard.ex | 1 + lib/groupher_server_web/schema/cms/cms_types.ex | 1 + .../20240503045538_add_homepage_to_community.exs | 9 +++++++++ .../mutation/cms/dashboard_test.exs | 10 ++++++++-- 6 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 priv/repo/migrations/20240503045538_add_homepage_to_community.exs diff --git a/lib/groupher_server/cms/delegates/community_crud.ex b/lib/groupher_server/cms/delegates/community_crud.ex index 1300bb9..d396293 100644 --- a/lib/groupher_server/cms/delegates/community_crud.ex +++ b/lib/groupher_server/cms/delegates/community_crud.ex @@ -134,7 +134,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityCRUD do """ def update_dashboard(community_slug, :base_info, args) do main_fields = - Map.take(args, [:title, :desc, :logo, :favicon, :slug]) + Map.take(args, [:title, :locale, :desc, :logo, :favicon, :slug, :homepage]) |> OSS.persist_file(:logo) |> OSS.persist_file(:favicon) diff --git a/lib/groupher_server/cms/models/community.ex b/lib/groupher_server/cms/models/community.ex index 630e383..9fcf651 100644 --- a/lib/groupher_server/cms/models/community.ex +++ b/lib/groupher_server/cms/models/community.ex @@ -27,7 +27,7 @@ defmodule GroupherServer.CMS.Model.Community do @required_fields ~w(title desc user_id logo slug)a # @required_fields ~w(title desc user_id)a - @optional_fields ~w(favicon label geo_info index aka contributes_digest pending locale)a + @optional_fields ~w(favicon label geo_info index aka contributes_digest pending locale homepage)a def max_pinned_article_count_per_thread, do: @max_pinned_article_count_per_thread @@ -44,6 +44,7 @@ defmodule GroupherServer.CMS.Model.Community do field(:geo_info, :map) field(:views, :integer) field(:locale, :string) + field(:homepage, :string) embeds_one(:meta, Embeds.CommunityMeta, on_replace: :delete) belongs_to(:author, User, foreign_key: :user_id) diff --git a/lib/groupher_server/cms/models/metrics/dashboard.ex b/lib/groupher_server/cms/models/metrics/dashboard.ex index ef37d51..7e33db3 100644 --- a/lib/groupher_server/cms/models/metrics/dashboard.ex +++ b/lib/groupher_server/cms/models/metrics/dashboard.ex @@ -30,6 +30,7 @@ defmodule GroupherServer.CMS.Model.Metrics.Dashboard do [ [:favicon, :string, ""], [:title, :string, ""], + [:locale, :string, ""], [:logo, :string, ""], [:slug, :string, ""], [:desc, :string, ""], diff --git a/lib/groupher_server_web/schema/cms/cms_types.ex b/lib/groupher_server_web/schema/cms/cms_types.ex index c14aca1..00784f9 100644 --- a/lib/groupher_server_web/schema/cms/cms_types.ex +++ b/lib/groupher_server_web/schema/cms/cms_types.ex @@ -182,6 +182,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do field(:desc, :string) field(:slug, :string) field(:favicon, :string) + field(:homepage, :string) field(:index, :integer) field(:logo, :string) field(:author, :user, resolve: dataloader(CMS, :author)) diff --git a/priv/repo/migrations/20240503045538_add_homepage_to_community.exs b/priv/repo/migrations/20240503045538_add_homepage_to_community.exs new file mode 100644 index 0000000..72af294 --- /dev/null +++ b/priv/repo/migrations/20240503045538_add_homepage_to_community.exs @@ -0,0 +1,9 @@ +defmodule GroupherServer.Repo.Migrations.AddHomepageToCommunity do + use Ecto.Migration + + def change do + alter table(:communities, prefix: "cms") do + add(:homepage, :string) + end + end +end diff --git a/test/groupher_server_web/mutation/cms/dashboard_test.exs b/test/groupher_server_web/mutation/cms/dashboard_test.exs index 4adfdf6..c430cb6 100644 --- a/test/groupher_server_web/mutation/cms/dashboard_test.exs +++ b/test/groupher_server_web/mutation/cms/dashboard_test.exs @@ -22,20 +22,23 @@ defmodule GroupherServer.Test.Mutation.CMS.Dashboard do describe "[mutation cms community]" do @update_info_query """ - mutation($community: String!, $homepage: String, $title: String, $slug: String, $desc: String, $introduction: String, $logo: String, $favicon: String, $city: String, $techstack: String) { - updateDashboardBaseInfo(community: $community, homepage: $homepage, title: $title, slug: $slug, desc: $desc, introduction: $introduction, logo: $logo, favicon: $favicon, city: $city, techstack: $techstack) { + mutation($community: String!, $homepage: String, $locale: String, $title: String, $slug: String, $desc: String, $introduction: String, $logo: String, $favicon: String, $city: String, $techstack: String) { + updateDashboardBaseInfo(community: $community, homepage: $homepage, locale: $locale, title: $title, slug: $slug, desc: $desc, introduction: $introduction, logo: $logo, favicon: $favicon, city: $city, techstack: $techstack) { id title + locale dashboard { baseInfo { title + locale introduction } } } } """ + @tag :wip test "update community dashboard base info", ~m(community)a do rule_conn = simu_conn(:user, cms: %{"community.update" => true}) @@ -45,6 +48,7 @@ defmodule GroupherServer.Test.Mutation.CMS.Dashboard do slug: "groupher", homepage: "https://groupher.com", desc: "thie community is awesome", + locale: "lt", introduction: """ I feel very happy writing this post. After reading this post you might feel the same as me. @@ -74,8 +78,10 @@ defmodule GroupherServer.Test.Mutation.CMS.Dashboard do {:ok, found} = Community |> ORM.find(updated["id"], preload: :dashboard) + assert found.locale == "lt" assert found.dashboard.base_info.introduction |> String.length() == 828 assert found.dashboard.base_info.title == "groupher" + assert found.dashboard.base_info.locale == "lt" assert found.dashboard.base_info.desc == "thie community is awesome" assert found.dashboard.base_info.slug == "groupher"