Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(community): add locale #28

Merged
merged 3 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/mock.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion lib/groupher_server/cms/delegates/community_crud.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 3 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 homepage)a

def max_pinned_article_count_per_thread, do: @max_pinned_article_count_per_thread

Expand All @@ -43,6 +43,8 @@ defmodule GroupherServer.CMS.Model.Community do
field(:index, :integer)
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)
Expand Down
1 change: 1 addition & 0 deletions lib/groupher_server/cms/models/metrics/dashboard.ex
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ defmodule GroupherServer.CMS.Model.Metrics.Dashboard do
[
[:favicon, :string, ""],
[:title, :string, ""],
[:locale, :string, ""],
[:logo, :string, ""],
[:slug, :string, ""],
[:desc, :string, ""],
Expand Down
4 changes: 4 additions & 0 deletions lib/groupher_server_web/resolvers/cms_resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/groupher_server_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions lib/groupher_server_web/schema/cms/cms_queries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions lib/groupher_server_web/schema/cms/cms_types.ex
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,11 @@ 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))
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 Expand Up @@ -419,4 +421,8 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
field(:favicon, :string)
field(:site_name, :string)
end

object :client_locale do
field(:locale, :string)
end
end
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
Original file line number Diff line number Diff line change
@@ -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
4 changes: 1 addition & 3 deletions test/groupher_server/accounts/oauth_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# 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 Expand Up @@ -91,17 +91,16 @@
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)
{:ok, _} = Accounts.signin_oauth(github_provider)
{:ok, res} = Accounts.link_oauth(user_login, @valid_twitter_profile)

Check warning on line 98 in test/groupher_server/accounts/oauth_test.exs

View workflow job for this annotation

GitHub Actions / Build and test

variable "res" is unused (if the variable is not meant to be used, prefix it with an underscore)

{:ok, providers} = ORM.find_all(OauthProvider, %{page: 1, size: 10})
assert providers.total_count == 2

{:ok, res} = Accounts.unlink_oauth(user_login, @valid_twitter_profile)

Check warning on line 103 in test/groupher_server/accounts/oauth_test.exs

View workflow job for this annotation

GitHub Actions / Build and test

variable "res" is unused (there is a variable with the same name in the context, use the pin operator (^) to match on it or prefix this variable with underscore if it is not meant to be used)

{:ok, providers} = ORM.find_all(OauthProvider, %{page: 1, size: 10})
assert providers.total_count == 1
Expand All @@ -110,7 +109,6 @@
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)
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
9 changes: 3 additions & 6 deletions test/groupher_server_web/mutation/accounts/oauth_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)
Expand All @@ -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)
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
10 changes: 8 additions & 2 deletions test/groupher_server_web/mutation/cms/dashboard_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand All @@ -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.
Expand Down Expand Up @@ -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"

Expand Down
15 changes: 15 additions & 0 deletions test/groupher_server_web/query/cms/cms_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
Loading