Skip to content

Commit

Permalink
feat(community): add locale field
Browse files Browse the repository at this point in the history
  • Loading branch information
mydearxym committed May 3, 2024
1 parent 9f0df37 commit cdf67ff
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 11 deletions.
3 changes: 2 additions & 1 deletion lib/groupher_server/cms/models/community.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions lib/groupher_server_web/schema/cms/cms_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
3 changes: 3 additions & 0 deletions lib/groupher_server_web/schema/cms/mutations/community.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand All @@ -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)

Expand Down
3 changes: 2 additions & 1 deletion lib/support/factory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion test/groupher_server/accounts/oauth_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
4 changes: 3 additions & 1 deletion test/groupher_server/cms/community/community_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down
18 changes: 12 additions & 6 deletions test/groupher_server_web/mutation/cms/crud_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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"])
Expand All @@ -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 """
Expand Down
2 changes: 1 addition & 1 deletion test/groupher_server_web/query/cms/third_part_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit cdf67ff

Please sign in to comment.