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)