diff --git a/lib/groupher_server/cms/cms.ex b/lib/groupher_server/cms/cms.ex index 601dc531c..30f6327c4 100644 --- a/lib/groupher_server/cms/cms.ex +++ b/lib/groupher_server/cms/cms.ex @@ -37,6 +37,12 @@ defmodule GroupherServer.CMS do defdelegate read_community(args, user), to: CommunityCURD defdelegate create_community(args), to: CommunityCURD defdelegate update_community(id, args), to: CommunityCURD + defdelegate apply_community(args), to: CommunityCURD + defdelegate approve_community_apply(id), to: CommunityCURD + defdelegate deny_community_apply(id), to: CommunityCURD + defdelegate is_community_exist?(raw), to: CommunityCURD + defdelegate has_pending_community_apply?(user), to: CommunityCURD + # >> editor .. defdelegate update_editor(user, community, title), to: CommunityCURD # >> geo info .. diff --git a/lib/groupher_server/cms/constant.ex b/lib/groupher_server/cms/constant.ex index dab0ca349..c8b2b1cb3 100644 --- a/lib/groupher_server/cms/constant.ex +++ b/lib/groupher_server/cms/constant.ex @@ -8,7 +8,23 @@ defmodule GroupherServer.CMS.Constant do @artiment_illegal 1 @artiment_audit_failed 2 + @community_normal 0 + @community_applying 1 + + @apply_public "PUBLIC" + @apply_city "CITY" + @apply_works "WORKS" + @apply_team "TEAM" + def pending(:legal), do: @artiment_legal def pending(:illegal), do: @artiment_illegal def pending(:audit_failed), do: @artiment_audit_failed + + def pending(:normal), do: @community_normal + def pending(:applying), do: @community_applying + + def apply_category(:public), do: @apply_public + def apply_category(:city), do: @apply_city + def apply_category(:works), do: @apply_works + def apply_category(:team), do: @apply_team end diff --git a/lib/groupher_server/cms/delegates/article_community.ex b/lib/groupher_server/cms/delegates/article_community.ex index 13f0bcdbb..8c0a54538 100644 --- a/lib/groupher_server/cms/delegates/article_community.ex +++ b/lib/groupher_server/cms/delegates/article_community.ex @@ -187,7 +187,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do @doc "update isEdited meta label if needed" # TODO: diff history def update_edit_status(%{meta: %Embeds.ArticleMeta{is_edited: _} = meta} = content) do - meta = meta |> strip_struct |> Map.merge(%{is_edited: true}) + meta = meta |> Map.merge(%{is_edited: true}) ORM.update_meta(content, meta) end diff --git a/lib/groupher_server/cms/delegates/community_curd.ex b/lib/groupher_server/cms/delegates/community_curd.ex index 28d41bf0b..e5836d824 100644 --- a/lib/groupher_server/cms/delegates/community_curd.ex +++ b/lib/groupher_server/cms/delegates/community_curd.ex @@ -24,13 +24,18 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do Thread } + alias CMS.Constant + @default_meta Embeds.CommunityMeta.default_meta() @article_threads get_config(:article, :threads) - def read_community(clauses, user), do: read_community(clauses) |> viewer_has_states(user) - def read_community(%{id: id}), do: ORM.read(Community, id, inc: :views) - def read_community(%{raw: raw} = clauses), do: do_read_community(clauses, raw) - def read_community(%{title: title} = clauses), do: do_read_community(clauses, title) + @community_normal Constant.pending(:normal) + @community_applying Constant.pending(:applying) + + @default_apply_category Constant.apply_category(:public) + + def read_community(raw, user), do: read_community(raw) |> viewer_has_states(user) + def read_community(raw), do: do_read_community(raw) @doc """ create a community @@ -54,6 +59,59 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do end end + @doc """ + check if community exist + """ + def is_community_exist?(raw) do + case ORM.find_by(Community, raw: raw) do + {:ok, _} -> {:ok, %{exist: true}} + {:error, _} -> {:ok, %{exist: false}} + end + end + + def has_pending_community_apply?(%User{} = user) do + with {:ok, paged_applies} <- paged_community_applies(user, %{page: 1, size: 1}) do + case paged_applies.total_count > 0 do + true -> {:ok, %{exist: true}} + false -> {:ok, %{exist: false}} + end + end + end + + def paged_community_applies(%User{} = user, %{page: page, size: size} = _filter) do + Community + |> where([c], c.pending == ^@community_applying) + |> where([c], c.user_id == ^user.id) + |> ORM.paginator(~m(page size)a) + |> done + end + + def apply_community(args) do + with {:ok, community} <- create_community(Map.merge(args, %{pending: @community_applying})) do + apply_msg = Map.get(args, :apply_msg, "") + apply_category = Map.get(args, :apply_category, @default_apply_category) + + meta = community.meta |> Map.merge(~m(apply_msg apply_category)a) + ORM.update_meta(community, meta) + end + end + + def approve_community_apply(id) do + # TODO: create community with thread, category and tags + with {:ok, community} <- ORM.find(Community, id) do + ORM.update(community, %{pending: @community_normal}) + end + end + + def deny_community_apply(id) do + with {:ok, community} <- ORM.find(Community, id) do + case community.pending == @community_applying do + true -> ORM.delete(community) + false -> {:ok, community} + end + end + end + @doc """ update editors_count of a community """ @@ -235,13 +293,20 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do end end - defp do_read_community(clauses, aka) do - case ORM.read_by(Community, clauses, inc: :views) do - {:ok, community} -> {:ok, community} - {:error, _} -> ORM.find_by(Community, aka: aka) + defp do_read_community(raw) do + with {:ok, community} <- find_community(raw) do + community |> ORM.read(inc: :views) end end + defp find_community(raw) do + Community + |> where([c], c.pending == ^@community_normal) + |> where([c], c.raw == ^raw or c.aka == ^raw) + |> Repo.one() + |> done + end + defp viewer_has_states({:ok, community}, %User{id: user_id}) do viewer_has_states = %{ viewer_has_subscribed: user_id in community.meta.subscribed_user_ids, diff --git a/lib/groupher_server/cms/delegates/document.ex b/lib/groupher_server/cms/delegates/document.ex index 6435905f5..7275ab000 100644 --- a/lib/groupher_server/cms/delegates/document.ex +++ b/lib/groupher_server/cms/delegates/document.ex @@ -3,10 +3,9 @@ defmodule GroupherServer.CMS.Delegate.Document do CURD operation on post/job ... """ import Ecto.Query, warn: false - import Helper.Utils, only: [done: 1, thread_of: 2, get_config: 2] + import Helper.Utils, only: [thread_of: 2] import Helper.ErrorCode - import ShortMaps alias Helper.{ORM, Converter} alias GroupherServer.{CMS, Repo} diff --git a/lib/groupher_server/cms/delegates/search.ex b/lib/groupher_server/cms/delegates/search.ex index f66232f2b..477e096ca 100644 --- a/lib/groupher_server/cms/delegates/search.ex +++ b/lib/groupher_server/cms/delegates/search.ex @@ -30,7 +30,10 @@ defmodule GroupherServer.CMS.Delegate.Search do defp do_search_communities(queryable, title) do queryable - |> where([c], ilike(c.title, ^"%#{title}%") or ilike(c.raw, ^"%#{title}%")) + |> where( + [c], + ilike(c.title, ^"%#{title}%") or ilike(c.raw, ^"%#{title}%") or ilike(c.aka, ^"%#{title}%") + ) |> ORM.paginator(page: 1, size: @search_items_count) |> done() end diff --git a/lib/groupher_server/cms/models/article_document.ex b/lib/groupher_server/cms/models/article_document.ex index 82e651210..29b2276b6 100644 --- a/lib/groupher_server/cms/models/article_document.ex +++ b/lib/groupher_server/cms/models/article_document.ex @@ -8,7 +8,6 @@ defmodule GroupherServer.CMS.Model.ArticleDocument do use Accessible import Ecto.Changeset - import GroupherServer.CMS.Helper.Macros import Helper.Utils, only: [get_config: 2] @timestamps_opts [type: :utc_datetime_usec] diff --git a/lib/groupher_server/cms/models/community.ex b/lib/groupher_server/cms/models/community.ex index 7fde142d0..3bcd369a1 100644 --- a/lib/groupher_server/cms/models/community.ex +++ b/lib/groupher_server/cms/models/community.ex @@ -3,6 +3,8 @@ defmodule GroupherServer.CMS.Model.Community do alias __MODULE__ use Ecto.Schema + use Accessible + import Ecto.Changeset alias GroupherServer.{Accounts, CMS} @@ -22,7 +24,7 @@ defmodule GroupherServer.CMS.Model.Community do @required_fields ~w(title desc user_id logo raw)a # @required_fields ~w(title desc user_id)a - @optional_fields ~w(label geo_info index aka contributes_digest)a + @optional_fields ~w(label geo_info index aka contributes_digest pending)a def max_pinned_article_count_per_thread, do: @max_pinned_article_count_per_thread @@ -51,6 +53,8 @@ defmodule GroupherServer.CMS.Model.Community do field(:article_tags_count, :integer, default: 0) field(:threads_count, :integer, default: 0) + field(:pending, :integer, default: 0) + field(:viewer_has_subscribed, :boolean, default: false, virtual: true) field(:viewer_is_editor, :boolean, default: false, virtual: true) field(:contributes_digest, {:array, :integer}, default: []) @@ -81,8 +85,9 @@ defmodule GroupherServer.CMS.Model.Community do |> validate_required(@required_fields) |> cast_embed(:meta, with: &Embeds.CommunityMeta.changeset/2) |> validate_length(:title, min: 1, max: 30) + |> validate_length(:raw, min: 1, max: 30) |> foreign_key_constraint(:user_id) - |> unique_constraint(:title, name: :communities_title_index) + |> unique_constraint(:raw, name: :communities_raw_index) |> unique_constraint(:aka, name: :communities_aka_index) # |> foreign_key_constraint(:communities_author_fkey) diff --git a/lib/groupher_server/cms/models/embeds/community_meta.ex b/lib/groupher_server/cms/models/embeds/community_meta.ex index fdd62ecdc..20f19593d 100644 --- a/lib/groupher_server/cms/models/embeds/community_meta.ex +++ b/lib/groupher_server/cms/models/embeds/community_meta.ex @@ -31,7 +31,9 @@ defmodule GroupherServer.CMS.Model.Embeds.CommunityMeta do @general_options %{ editors_ids: [], subscribed_user_ids: [], - contributes_digest: [] + contributes_digest: [], + apply_msg: "", + apply_category: "" } @optional_fields Map.keys(@general_options) ++ @@ -53,6 +55,9 @@ defmodule GroupherServer.CMS.Model.Embeds.CommunityMeta do # 关注相关 field(:subscribed_user_ids, {:array, :integer}, default: []) field(:contributes_digest, {:array, :integer}, default: []) + # 申请信息 + field(:apply_msg, :string, default: "") + field(:apply_category, :string, default: "") end def changeset(struct, params) do diff --git a/lib/groupher_server_web/resolvers/cms_resolver.ex b/lib/groupher_server_web/resolvers/cms_resolver.ex index ca836df3d..95cdfd175 100644 --- a/lib/groupher_server_web/resolvers/cms_resolver.ex +++ b/lib/groupher_server_web/resolvers/cms_resolver.ex @@ -14,31 +14,48 @@ defmodule GroupherServerWeb.Resolvers.CMS do # ####################### # community .. # ####################### - def community(_root, args, %{context: %{cur_user: user}}) do - case Enum.empty?(args) do - false -> CMS.read_community(args, user) - true -> {:error, "please provide community id or title or raw"} - end + def community(_root, %{raw: raw}, %{context: %{cur_user: user}}) do + CMS.read_community(raw, user) end - def community(_root, args, _info) do - case Enum.empty?(args) do - false -> CMS.read_community(args) - true -> {:error, "please provide community id or title or raw"} - end + def community(_root, %{raw: raw}, _info) do + CMS.read_community(raw) end def paged_communities(_root, ~m(filter)a, _info), do: Community |> ORM.find_all(filter) def create_community(_root, args, %{context: %{cur_user: user}}) do args = args |> Map.merge(%{user_id: user.id}) - Community |> ORM.create(args) + CMS.create_community(args) end - def update_community(_root, args, _info), do: Community |> ORM.find_update(args) + def update_community(_root, args, _info) do + CMS.update_community(args.id, args) + end def delete_community(_root, %{id: id}, _info), do: Community |> ORM.find_delete!(id) + def apply_community(_root, args, %{context: %{cur_user: user}}) do + args = args |> Map.merge(%{user_id: user.id}) + CMS.apply_community(args) + end + + def approve_community_apply(_root, %{id: id}, _) do + CMS.approve_community_apply(id) + end + + def deny_community_apply(_root, %{id: id}, _) do + CMS.deny_community_apply(id) + end + + def is_community_exist?(_root, %{raw: raw}, _) do + CMS.is_community_exist?(raw) + end + + def has_pending_community_apply?(_root, _, %{context: %{cur_user: user}}) do + CMS.has_pending_community_apply?(user) + end + # ####################### # community thread (post, job), login user should be logged # ####################### diff --git a/lib/groupher_server_web/schema/cms/cms_queries.ex b/lib/groupher_server_web/schema/cms/cms_queries.ex index aced179a8..8e6ce00da 100644 --- a/lib/groupher_server_web/schema/cms/cms_queries.ex +++ b/lib/groupher_server_web/schema/cms/cms_queries.ex @@ -10,12 +10,25 @@ defmodule GroupherServerWeb.Schema.CMS.Queries do @desc "spec community info" field :community, :community do # arg(:id, non_null(:id)) - arg(:id, :id) - arg(:title, :string) - arg(:raw, :string) + # arg(:title, :string) + arg(:raw, non_null(:string)) resolve(&R.CMS.community/3) end + @desc "if use has pending apply" + field :has_pending_community_apply, :check_state do + middleware(M.Authorize, :login) + resolve(&R.CMS.has_pending_community_apply?/3) + end + + @desc "if the community exist or not" + field :is_community_exist, :check_state do + arg(:raw, non_null(:string)) + + middleware(M.Authorize, :login) + resolve(&R.CMS.is_community_exist?/3) + end + @desc "communities with pagination info" field :paged_communities, :paged_communities do arg(:filter, non_null(:communities_filter)) diff --git a/lib/groupher_server_web/schema/cms/cms_types.ex b/lib/groupher_server_web/schema/cms/cms_types.ex index 4cb09b338..4f5c8885d 100644 --- a/lib/groupher_server_web/schema/cms/cms_types.ex +++ b/lib/groupher_server_web/schema/cms/cms_types.ex @@ -15,6 +15,10 @@ defmodule GroupherServerWeb.Schema.CMS.Types do import_types(Schema.CMS.Metrics) + object :check_state do + field(:exist, :boolean) + end + ###### # common stands for minimal info of the type # usually used in abuse_report, feeds, etc .. @@ -24,6 +28,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do field(:nickname, :string) field(:avatar, :string) field(:bio, :string) + field(:shortbio, :string) end object :common_article do @@ -265,6 +270,8 @@ defmodule GroupherServerWeb.Schema.CMS.Types do field(:viewer_has_subscribed, :boolean) field(:viewer_is_editor, :boolean) + field(:pending, :integer) + # TODO: remove field :threads_count, :integer do resolve(&R.CMS.threads_count/3) @@ -461,5 +468,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do object :community_meta do threads_count_fields() + field(:apply_msg, :string) + field(:apply_category, :string) end end diff --git a/lib/groupher_server_web/schema/cms/mutations/community.ex b/lib/groupher_server_web/schema/cms/mutations/community.ex index 6d14b4e48..ee2381878 100644 --- a/lib/groupher_server_web/schema/cms/mutations/community.ex +++ b/lib/groupher_server_web/schema/cms/mutations/community.ex @@ -11,7 +11,6 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Community do arg(:desc, non_null(:string)) arg(:raw, non_null(:string)) arg(:logo, non_null(:string)) - # arg(:category, non_null(:string)) middleware(M.Authorize, :login) middleware(M.Passport, claim: "cms->community.create") @@ -43,6 +42,37 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Community do resolve(&R.CMS.delete_community/3) end + @desc "apply to create a community" + field :apply_community, :community do + arg(:title, non_null(:string)) + arg(:desc, non_null(:string)) + arg(:raw, non_null(:string)) + arg(:logo, non_null(:string)) + arg(:apply_msg, :string) + arg(:apply_category, :string) + + middleware(M.Authorize, :login) + resolve(&R.CMS.apply_community/3) + end + + @desc "approve the apply to create a community" + field :approve_community_apply, :community do + arg(:id, non_null(:id)) + + middleware(M.Authorize, :login) + middleware(M.Passport, claim: "cms->community.apply.approve") + resolve(&R.CMS.approve_community_apply/3) + end + + @desc "deny the apply to create a community" + field :deny_community_apply, :community do + arg(:id, non_null(:id)) + + middleware(M.Authorize, :login) + middleware(M.Passport, claim: "cms->community.apply.deny") + resolve(&R.CMS.deny_community_apply/3) + end + @desc "create category" field :create_category, :category do arg(:title, non_null(:string)) diff --git a/lib/helper/certification.ex b/lib/helper/certification.ex index 85e80e838..e1875710c 100644 --- a/lib/helper/certification.ex +++ b/lib/helper/certification.ex @@ -85,7 +85,10 @@ defmodule Helper.Certification do "category.update", "category.set", "category.unset", - "thread.create" + "thread.create", + "community.apply.approve", + "community.apply.deny" + # ], community: [ "thread.set", diff --git a/lib/helper/error_code.ex b/lib/helper/error_code.ex index 4a954169f..a7f96aa43 100644 --- a/lib/helper/error_code.ex +++ b/lib/helper/error_code.ex @@ -35,6 +35,7 @@ defmodule Helper.ErrorCode do def ecode(:bill_state), do: @default_base + 12 def ecode(:bill_action), do: @default_base + 13 def ecode(:editor_data_parse), do: @default_base + 14 + def ecode(:community_exist), do: @default_base + 15 # throttle def ecode(:throttle_inverval), do: @throttle_base + 1 def ecode(:throttle_hour), do: @throttle_base + 2 diff --git a/lib/helper/orm.ex b/lib/helper/orm.ex index 4793919c0..d44904d63 100644 --- a/lib/helper/orm.ex +++ b/lib/helper/orm.ex @@ -109,22 +109,23 @@ defmodule Helper.ORM do @doc """ Require queryable has a views fields to count the views of the queryable Modal """ - def read(queryable, id, inc: :views) do + def read(queryable, id, inc: :views) when is_number(id) or is_binary(id) do with {:ok, result} <- find(queryable, id) do result |> inc_views_count(queryable) |> done() end end - def read(article, inc: :views) do - article |> inc_views_count(article.__struct__) |> done() - end - def read_by(queryable, clauses, inc: :views) do with {:ok, result} <- find_by(queryable, clauses) do result |> inc_views_count(queryable) |> done() end end + # content counld be article/community + def read(content, inc: :views) do + content |> inc_views_count(content.__struct__) |> done() + end + defp inc_views_count(content, queryable) do {1, [result]} = Repo.update_all( diff --git a/lib/helper/scheduler.ex b/lib/helper/scheduler.ex index ffb31ec78..9a8f55d57 100644 --- a/lib/helper/scheduler.ex +++ b/lib/helper/scheduler.ex @@ -4,7 +4,7 @@ defmodule Helper.Scheduler do """ use Quantum.Scheduler, otp_app: :groupher_server - import Helper.Utils, only: [get_config: 2] + import Helper.Utils, only: [get_config: 2, done: 1] alias GroupherServer.CMS alias CMS.Delegate.Hooks @@ -23,10 +23,12 @@ defmodule Helper.Scheduler do """ def archive_artiments() do Enum.map(@article_threads, &CMS.archive_articles(&1)) + |> done end def arthive_comments() do CMS.archive_comments() + |> done end def articles_audition() do @@ -42,6 +44,7 @@ defmodule Helper.Scheduler do Enum.map(paged_comments.entries, fn comment -> Hooks.Audition.handle(comment) end) + |> done end end @@ -52,6 +55,7 @@ defmodule Helper.Scheduler do # the free audition service's QPS is limit to 2 Process.sleep(500) end) + |> done end end end diff --git a/lib/helper/utils/utils.ex b/lib/helper/utils/utils.ex index 823637636..a0cd5aad2 100644 --- a/lib/helper/utils/utils.ex +++ b/lib/helper/utils/utils.ex @@ -67,6 +67,7 @@ defmodule Helper.Utils do def done(false), do: {:error, false} def done(true), do: {:ok, true} def done(nil), do: {:error, "record not found."} + def done([]), do: {:ok, []} def done(:ok), do: {:ok, :pass} def done(nil, :boolean), do: {:ok, false} def done(_, :boolean), do: {:ok, true} diff --git a/priv/repo/migrations/20211114024657_adjust_community_uniq_field.exs b/priv/repo/migrations/20211114024657_adjust_community_uniq_field.exs new file mode 100644 index 000000000..450b02bd1 --- /dev/null +++ b/priv/repo/migrations/20211114024657_adjust_community_uniq_field.exs @@ -0,0 +1,8 @@ +defmodule GroupherServer.Repo.Migrations.AdjustCommunityUniqField do + use Ecto.Migration + + def change do + drop(unique_index(:communities, [:title])) + create(unique_index(:communities, [:raw])) + end +end diff --git a/priv/repo/migrations/20211114085157_add_pending_to_community.exs b/priv/repo/migrations/20211114085157_add_pending_to_community.exs new file mode 100644 index 000000000..59b53ff33 --- /dev/null +++ b/priv/repo/migrations/20211114085157_add_pending_to_community.exs @@ -0,0 +1,7 @@ +defmodule GroupherServer.Repo.Migrations.AddPendingToCommunity do + use Ecto.Migration + + def change do + alter(table(:communities), do: add(:pending, :integer, default: 0)) + end +end diff --git a/test/groupher_server/cms/community/community_meta_test.exs b/test/groupher_server/cms/community/community_meta_test.exs index 9deb67659..01ced35ca 100644 --- a/test/groupher_server/cms/community/community_meta_test.exs +++ b/test/groupher_server/cms/community/community_meta_test.exs @@ -29,6 +29,7 @@ defmodule GroupherServer.Test.Community.CommunityMeta do assert community.meta |> strip_struct == @default_meta end + @tag :wip test "update legacy community should add default meta", ~m(community)a do assert is_nil(community.meta) diff --git a/test/groupher_server/cms/community/community_test.exs b/test/groupher_server/cms/community/community_test.exs index dcd2a0c84..bfae6147e 100644 --- a/test/groupher_server/cms/community/community_test.exs +++ b/test/groupher_server/cms/community/community_test.exs @@ -8,6 +8,11 @@ defmodule GroupherServer.Test.CMS.Community do alias Helper.ORM + alias CMS.Constant + + @community_normal Constant.pending(:normal) + @community_applying Constant.pending(:applying) + setup do {:ok, user} = db_insert(:user) {:ok, user2} = db_insert(:user) @@ -18,42 +23,86 @@ defmodule GroupherServer.Test.CMS.Community do {:ok, ~m(user community article_tag_attrs user2)a} end + describe "[cms community apply]" do + @tag :wip + test "apply a community should have pending and can not be read", ~m(user)a do + attrs = mock_attrs(:community) |> Map.merge(%{user_id: user.id, apply_msg: "apply msg"}) + {:ok, community} = CMS.apply_community(attrs) + + assert community.meta.apply_msg == "apply msg" + assert community.meta.apply_category == "PUBLIC" + + {:ok, community} = ORM.find(Community, community.id) + assert community.pending == @community_applying + assert {:error, _} = CMS.read_community(community.raw) + + {:ok, community} = CMS.approve_community_apply(community.id) + + {:ok, community} = ORM.find(Community, community.id) + assert community.pending == @community_normal + assert {:ok, _} = CMS.read_community(community.raw) + end + + @tag :wip + test "apply can be deny", ~m(user)a do + attrs = mock_attrs(:community) |> Map.merge(%{user_id: user.id}) + {:ok, community} = CMS.apply_community(attrs) + {:ok, community} = CMS.deny_community_apply(community.id) + + {:error, _} = ORM.find(Community, community.id) + end + + @tag :wip2 + test "user can query has pending apply or not", ~m(user user2)a do + attrs = mock_attrs(:community) |> Map.merge(%{user_id: user.id}) + {:ok, _community} = CMS.apply_community(attrs) + + {:ok, state} = CMS.has_pending_community_apply?(user) + assert state.exist + + {:ok, state} = CMS.has_pending_community_apply?(user2) + assert not state.exist + end + end + describe "[cms community read]" do + @tag :wip test "read community should inc views", ~m(community)a do - {:ok, community} = CMS.read_community(%{id: community.id}) - + {:ok, community} = CMS.read_community(community.raw) assert community.views == 1 - {:ok, community} = CMS.read_community(%{title: community.title}) + {:ok, community} = CMS.read_community(community.raw) assert community.views == 2 - {:ok, community} = CMS.read_community(%{raw: community.raw}) + {:ok, community} = CMS.read_community(community.raw) assert community.views == 3 end + @tag :wip test "read subscribed community should have a flag", ~m(community user user2)a do {:ok, _} = CMS.subscribe_community(community, user) - {:ok, community} = CMS.read_community(%{id: community.id}, user) + {:ok, community} = CMS.read_community(community.raw, user) assert community.viewer_has_subscribed assert user.id in community.meta.subscribed_user_ids - {:ok, community} = CMS.read_community(%{id: community.id}, user2) + {:ok, community} = CMS.read_community(community.raw, user2) assert not community.viewer_has_subscribed assert user2.id not in community.meta.subscribed_user_ids end + @tag :wip test "read editored community should have a flag", ~m(community user user2)a do title = "chief editor" {:ok, community} = CMS.set_editor(community, title, user) - {:ok, community} = CMS.read_community(%{id: community.id}, user) + {:ok, community} = CMS.read_community(community.raw, user) assert community.viewer_is_editor - {:ok, community} = CMS.read_community(%{id: community.id}, user2) + {:ok, community} = CMS.read_community(community.raw, user2) assert not community.viewer_is_editor {:ok, community} = CMS.unset_editor(community, user) - {:ok, community} = CMS.read_community(%{id: community.id}, user) + {:ok, community} = CMS.read_community(community.raw, user) assert not community.viewer_is_editor end end diff --git a/test/groupher_server/cms/hooks/audit_post_comment_test.exs b/test/groupher_server/cms/hooks/audit_post_comment_test.exs index 7e3725410..67a422592 100644 --- a/test/groupher_server/cms/hooks/audit_post_comment_test.exs +++ b/test/groupher_server/cms/hooks/audit_post_comment_test.exs @@ -5,7 +5,7 @@ defmodule GroupherServer.Test.CMS.Hooks.AuditPostComment do alias GroupherServer.{CMS} alias CMS.Delegate.Hooks - alias Helper.ORM + alias Helper.{ORM, Scheduler} alias CMS.Constant @audit_legal Constant.pending(:legal) @@ -68,5 +68,9 @@ defmodule GroupherServer.Test.CMS.Hooks.AuditPostComment do {:ok, paged_comments} = CMS.paged_audit_failed_comments(%{page: 1, size: 30}) assert paged_comments.total_count == 0 end + + test "can handle paged audit failed comments from Scheduler" do + {:ok, _results} = Scheduler.comments_audition() + end end end diff --git a/test/groupher_server/statistics/statistics_test.exs b/test/groupher_server/statistics/statistics_test.exs index 1ddd03718..24e418bcf 100644 --- a/test/groupher_server/statistics/statistics_test.exs +++ b/test/groupher_server/statistics/statistics_test.exs @@ -100,6 +100,7 @@ defmodule GroupherServer.Test.Statistics do end describe "[statistics community_contribute] " do + @tag :wip test "should inserted a community contribute when create community", ~m(community)a do community_id = community.id assert {:error, _} = ORM.find_by(CommunityContribute, ~m(community_id)a) diff --git a/test/groupher_server_web/mutation/cms/cms_test.exs b/test/groupher_server_web/mutation/cms/cms_test.exs index 464750d07..cdcc76da9 100644 --- a/test/groupher_server_web/mutation/cms/cms_test.exs +++ b/test/groupher_server_web/mutation/cms/cms_test.exs @@ -9,6 +9,10 @@ defmodule GroupherServer.Test.Mutation.CMS.Basic do alias CMS.Model.{Category, Community, CommunityEditor, Passport} alias Helper.ORM + alias CMS.Constant + + @community_normal Constant.pending(:normal) + @community_applying Constant.pending(:applying) setup do {:ok, category} = db_insert(:category) @@ -210,15 +214,51 @@ defmodule GroupherServer.Test.Mutation.CMS.Basic do assert created["id"] == to_string(found.id) end + @tag :wip + test "can create community with some title, different raw" do + rule_conn = simu_conn(:user, cms: %{"community.create" => true}) + variables = mock_attrs(:community, %{title: "elixir", raw: "elixir1"}) + rule_conn |> mutation_result(@create_community_query, variables, "createCommunity") + variables = mock_attrs(:community, %{title: "elixir", raw: "elixir2"}) + rule_conn |> mutation_result(@create_community_query, variables, "createCommunity") + + {:ok, community} = Community |> ORM.find_by(%{raw: "elixir1"}) + assert community.title == "elixir" + + {:ok, community} = Community |> ORM.find_by(%{raw: "elixir2"}) + assert community.title == "elixir" + end + + @tag :wip + test "can not create community with some raw" do + rule_conn = simu_conn(:user, cms: %{"community.create" => true}) + variables = mock_attrs(:community, %{title: "elixir1", raw: "elixir"}) + + first = + rule_conn + |> mutation_result(@create_community_query, variables, "createCommunity") + + assert not is_nil(first) + + variables = mock_attrs(:community, %{title: "elixir2", raw: "elixir"}) + + last = + rule_conn + |> mutation_result(@create_community_query, variables, "createCommunity") + + assert is_nil(last) + end + @update_community_query """ - mutation($id: ID!, $title: String, $desc: String, $logo: String, $raw: String) { - updateCommunity(id: $id, title: $title, desc: $desc, logo: $logo, raw: $raw) { + mutation($id: ID!, $title: String, $desc: String, $logo: String) { + updateCommunity(id: $id, title: $title, desc: $desc, logo: $logo) { id title desc } } """ + @tag :wip test "update community with valid attrs", ~m(community)a do rule_conn = simu_conn(:user, cms: %{"community.update" => true}) variables = %{id: community.id, title: "new title"} @@ -231,12 +271,14 @@ defmodule GroupherServer.Test.Mutation.CMS.Basic do assert updated["title"] == variables.title end + @tag :wip test "update community with empty attrs return the same", ~m(community)a do rule_conn = simu_conn(:user, cms: %{"community.update" => true}) variables = %{id: community.id} updated = - rule_conn |> mutation_result(@update_community_query, variables, "updateCommunity") + rule_conn + |> mutation_result(@update_community_query, variables, "updateCommunity") {:ok, found} = Community |> ORM.find(updated["id"]) assert updated["id"] == to_string(found.id) @@ -685,4 +727,83 @@ defmodule GroupherServer.Test.Mutation.CMS.Basic do assert Map.equal?(rules, %{"post.tag.edit" => true}) end end + + describe "mutation cms community apply" do + @apply_community_query """ + mutation($title: String!, $desc: String!, $logo: String!, $raw: String!, $applyMsg: String, $applyCategory: String) { + applyCommunity(title: $title, desc: $desc, logo: $logo, raw: $raw, applyMsg: $applyMsg, applyCategory: $applyCategory) { + id + pending + + meta { + applyMsg + applyCategory + } + } + } + """ + @tag :wip + test "can apply a community with or without apply info", ~m(user_conn)a do + variables = mock_attrs(:community) + created = user_conn |> mutation_result(@apply_community_query, variables, "applyCommunity") + + {:ok, found} = Community |> ORM.find(created["id"]) + assert created["id"] == to_string(found.id) + assert created["pending"] == @community_applying + + variables = mock_attrs(:community, %{applyMsg: "apply msg", applyCategory: "CITY"}) + created = user_conn |> mutation_result(@apply_community_query, variables, "applyCommunity") + + assert created["pending"] == @community_applying + + assert created |> get_in(["meta", "applyMsg"]) == "apply msg" + assert created |> get_in(["meta", "applyCategory"]) == "CITY" + end + + @approve_community_query """ + mutation($id: ID!) { + approveCommunityApply(id: $id) { + id + pending + } + } + """ + @tag :wip + test "can approve a community apply2", ~m(user_conn)a do + variables = mock_attrs(:community) + created = user_conn |> mutation_result(@apply_community_query, variables, "applyCommunity") + + variables = %{id: created["id"]} + rule_conn = simu_conn(:user, cms: %{"community.apply.approve" => true}) + + rule_conn + |> mutation_result(@approve_community_query, variables, "approveCommunityApply") + + {:ok, found} = Community |> ORM.find(created["id"]) + assert found.pending == @community_normal + end + + @deny_community_query """ + mutation($id: ID!) { + denyCommunityApply(id: $id) { + id + pending + } + } + """ + @tag :wip + test "can deny a community apply", ~m(user_conn)a do + variables = mock_attrs(:community) + created = user_conn |> mutation_result(@apply_community_query, variables, "applyCommunity") + assert {:ok, _} = Community |> ORM.find(created["id"]) + + variables = %{id: created["id"]} + rule_conn = simu_conn(:user, cms: %{"community.apply.deny" => true}) + + rule_conn + |> mutation_result(@deny_community_query, variables, "denyCommunityApply") + + assert {:error, _} = Community |> ORM.find(created["id"]) + end + end end diff --git a/test/groupher_server_web/query/cms/cms_test.exs b/test/groupher_server_web/query/cms/cms_test.exs index 3df2a27fd..ec4ed9a20 100644 --- a/test/groupher_server_web/query/cms/cms_test.exs +++ b/test/groupher_server_web/query/cms/cms_test.exs @@ -14,6 +14,68 @@ defmodule GroupherServer.Test.Query.CMS.Basic do {:ok, ~m(guest_conn community user)a} end + describe "apply community" do + @check_community_pending_query """ + query { + hasPendingCommunityApply { + exist + } + } + """ + @tag :wip + test "can check if user has penging apply", ~m(user)a do + user_conn = simu_conn(:user, user) + + check_state = + user_conn + |> query_result(@check_community_pending_query, %{}, "hasPendingCommunityApply") + + assert not check_state["exist"] + + attrs = mock_attrs(:community) |> Map.merge(%{user_id: user.id}) + {:ok, _community} = CMS.apply_community(attrs) + + user_conn = simu_conn(:user, user) + + check_state = + user_conn + |> query_result(@check_community_pending_query, %{}, "hasPendingCommunityApply") + + assert check_state["exist"] + end + + @check_community_exist_query """ + query($raw: String!) { + isCommunityExist(raw: $raw) { + exist + } + } + """ + @tag :wip + test "can check if a community is exist", ~m(user)a do + rule_conn = simu_conn(:user, cms: %{"community.create" => true}) + + check_state = + rule_conn + |> query_result( + @check_community_exist_query, + %{raw: "elixir"}, + "isCommunityExist" + ) + + assert not check_state["exist"] + + community_attrs = mock_attrs(:community, %{raw: "elixir", user_id: user.id}) + {:ok, _community} = CMS.create_community(community_attrs) + + check_state = + rule_conn + |> query_result(@check_community_exist_query, %{raw: "elixir"}, "isCommunityExist") + + assert check_state["exist"] + end + end + describe "[cms communities]" do @query """ query($id: ID, $raw: String) {