diff --git a/lib/groupher_server/cms/delegates/article_curd.ex b/lib/groupher_server/cms/delegates/article_curd.ex index 48236866e..0ca271631 100644 --- a/lib/groupher_server/cms/delegates/article_curd.ex +++ b/lib/groupher_server/cms/delegates/article_curd.ex @@ -12,7 +12,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do pick_by: 2, integerfy: 1, strip_struct: 1, - module_to_thread: 1, + module_to_atom: 1, get_config: 2, ensure: 2 ] @@ -333,7 +333,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do end defp add_pin_articles_ifneed(articles, querable, %{community: community} = filter) do - thread = module_to_thread(querable) + thread = module_to_atom(querable) with true <- should_add_pin?(filter), true <- 1 == Map.get(articles, :page_number), diff --git a/lib/groupher_server_web/schema/Helper/fields.ex b/lib/groupher_server_web/schema/Helper/fields.ex index aa72569db..e58440d2c 100644 --- a/lib/groupher_server_web/schema/Helper/fields.ex +++ b/lib/groupher_server_web/schema/Helper/fields.ex @@ -12,6 +12,50 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do @article_threads get_config(:article, :threads) + @doc "general article fields for grqphql resolve fields" + defmacro general_article_fields() do + quote do + field(:id, :id) + field(:title, :string) + field(:views, :integer) + field(:is_pinned, :boolean) + field(:mark_delete, :boolean) + + field(:article_tags, list_of(:article_tag), resolve: dataloader(CMS, :article_tags)) + field(:author, :user, resolve: dataloader(CMS, :author)) + field(:original_community, :community, resolve: dataloader(CMS, :original_community)) + field(:communities, list_of(:community), resolve: dataloader(CMS, :communities)) + + field(:meta, :article_meta) + field(:upvotes_count, :integer) + field(:collects_count, :integer) + field(:emotions, :article_emotions) + + field(:viewer_has_collected, :boolean) + field(:viewer_has_upvoted, :boolean) + field(:viewer_has_viewed, :boolean) + field(:viewer_has_reported, :boolean) + end + end + + defmacro article_thread_enums do + @article_threads + |> Enum.map(fn thread -> + quote do + enum(unquote(:"#{thread}_thread"), do: value(unquote(thread))) + end + end) + end + + defmacro article_values do + @article_threads + |> Enum.map(fn thread -> + quote do + value(unquote(thread)) + end + end) + end + defmacro timestamp_fields(:article) do quote do field(:inserted_at, :datetime) @@ -86,15 +130,7 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do end) end - defmacro viewer_has_state_fields do - quote do - field(:viewer_has_collected, :boolean) - field(:viewer_has_upvoted, :boolean) - field(:viewer_has_viewed, :boolean) - field(:viewer_has_reported, :boolean) - end - end - + # TODO: remove defmacro comments_fields do quote do field(:id, :id) @@ -120,16 +156,6 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do middleware(M.ConvertToInt) end - field :viewer_has_liked, :boolean do - arg(:viewer_did, :viewer_did_type, default_value: :viewer_did) - - middleware(M.Authorize, :login) - # put current user into dataloader's args - middleware(M.PutCurrentUser) - resolve(dataloader(CMS, :likes)) - middleware(M.ViewerDidConvert) - end - field :replies, list_of(:comment) do arg(:filter, :members_filter) @@ -157,6 +183,7 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do end end + # TODO: remove defmacro comments_counter_fields(thread) do quote do # @dec "total comments of the post" @@ -170,7 +197,6 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do # @desc "unique participator list of a the comments" field :comments_participators, list_of(:user) do arg(:filter, :members_filter) - arg(:unique, :unique_type, default_value: true) # middleware(M.ForceLoader) middleware(M.PageSizeProof) diff --git a/lib/groupher_server_web/schema/cms/cms_metrics.ex b/lib/groupher_server_web/schema/cms/cms_metrics.ex index 37a2f5923..034cd66b9 100644 --- a/lib/groupher_server_web/schema/cms/cms_metrics.ex +++ b/lib/groupher_server_web/schema/cms/cms_metrics.ex @@ -4,51 +4,36 @@ defmodule GroupherServerWeb.Schema.CMS.Metrics do """ use Absinthe.Schema.Notation import GroupherServerWeb.Schema.Helper.Fields - alias GroupherServer.CMS - alias CMS.Model.{Post, Job, Repo} + import Helper.Utils, only: [module_to_atom: 1] @default_inner_page_size 5 - enum(:post_thread, do: value(:post)) - enum(:job_thread, do: value(:job)) - enum(:repo_thread, do: value(:repo)) - - enum(:community_type, do: value(:community)) - enum(:comment_replies_type, do: value(:comment_replies_type)) - - enum(:count_type, do: value(:count)) - enum(:viewer_did_type, do: value(:viewer_did)) - - enum(:star_action, do: value(:star)) - enum(:comment_action, do: value(:comment)) - - enum :unique_type do - value(true) - value(false) - end + @doc """ + only used for reaction result, like: upvote/collect/watch ... + """ + interface :article do + # article 所包含的共同字段 + field(:id, :id) + field(:title, :string) + field(:views, :integer) - enum :react_action do - value(:collect) - value(:upvote) - # value(:watch) + # 这里只是遵循 absinthe 的规范,并不是指返回以下的字段 + resolve_type(fn parent_module, _ -> module_to_atom(parent_module) end) end - enum :reactable_action do - value(:upvote) - # value(:collect) - # value(:watch) - end + article_thread_enums() + # TODO: remove + enum(:count_type, do: value(:count)) + # TODO: remove enum :cms_comment do value(:post_comment) end enum :thread do - value(:post) - value(:job) + article_values() value(:user) - value(:repo) value(:wiki) value(:cheatsheet) # home community @@ -266,9 +251,7 @@ defmodule GroupherServerWeb.Schema.CMS.Metrics do end enum :report_content_type do - value(:post) - value(:job) - value(:repo) + article_values() value(:account) value(:article_comment) # value(:community) @@ -285,47 +268,4 @@ defmodule GroupherServerWeb.Schema.CMS.Metrics do # min_case_count, # max_case_count, end - - @doc """ - only used for reaction result, like: upvote/collect/watch ... - """ - interface :article do - field(:id, :id) - # field(:title, :string) - - # TODO: remove the domain part - resolve_type(fn - %Post{}, _ -> :post - %Job{}, _ -> :job - %Repo{}, _ -> :repo - _, _ -> nil - end) - end - - # @desc """ - # The `DateTime` scalar type represents a date and time in the UTC - # timezone. The DateTime appears in a JSON response as an ISO8601 formatted - # string, including UTC timezone ("Z"). The parsed date and time string will - # be converted to UTC and any UTC offset other than 0 will be rejected. - # """ - # scalar :datetime, name: "DateTime" do - # serialize &DateTime.to_iso8601/1 - # parse &parse_datetime/1 - # end - - # @spec parse_datetime(Absinthe.Blueprint.Input.String.t) :: {:ok, DateTime.t} | :error - # @spec parse_datetime(Absinthe.Blueprint.Input.Null.t) :: {:ok, nil} - # defp parse_datetime(%Absinthe.Blueprint.Input.String{value: value}) do - # case DateTime.from_iso8601(value) do - # {:ok, datetime, 0} -> {:ok, datetime} - # {:ok, _datetime, _offset} -> :error - # _error -> :error - # end - # end - # defp parse_datetime(%Absinthe.Blueprint.Input.Null{}) do - # {:ok, nil} - # end - # defp parse_datetime(_) do - # :error - # end end diff --git a/lib/groupher_server_web/schema/cms/cms_types.ex b/lib/groupher_server_web/schema/cms/cms_types.ex index a3029bc84..e7cd7c8dd 100644 --- a/lib/groupher_server_web/schema/cms/cms_types.ex +++ b/lib/groupher_server_web/schema/cms/cms_types.ex @@ -52,25 +52,15 @@ defmodule GroupherServerWeb.Schema.CMS.Types do object :post do meta(:cache, max_age: 30) interface(:article) - field(:id, :id) - field(:title, :string) + + general_article_fields() + article_comments_fields() + field(:digest, :string) field(:length, :integer) field(:link_addr, :string) field(:copy_right, :string) field(:body, :string) - field(:views, :integer) - # NOTE: only meaningful in paged-xxx queries - field(:is_pinned, :boolean) - field(:mark_delete, :boolean) - field(:article_tags, list_of(:article_tag), resolve: dataloader(CMS, :article_tags)) - - field(:author, :user, resolve: dataloader(CMS, :author)) - field(:original_community, :community, resolve: dataloader(CMS, :original_community)) - field(:communities, list_of(:community), resolve: dataloader(CMS, :communities)) - - field(:meta, :article_meta) - field(:emotions, :article_emotions) field :comments, list_of(:comment) do arg(:filter, :members_filter) @@ -79,8 +69,6 @@ defmodule GroupherServerWeb.Schema.CMS.Types do resolve(dataloader(CMS, :comments)) end - # comments_count - # comments_participators / paged comments_counter_fields(:post) @desc "totalCount of unique participator list of a the comments" @@ -94,9 +82,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do end) end - article_comments_fields() - viewer_has_state_fields() - # upvoted_count + # upvoted_count ? # collected_count timestamp_fields(:article) @@ -104,8 +90,10 @@ defmodule GroupherServerWeb.Schema.CMS.Types do object :job do interface(:article) - field(:id, :id) - field(:title, :string) + + general_article_fields() + article_comments_fields() + field(:desc, :string) field(:company, :string) field(:company_link, :string) @@ -114,34 +102,19 @@ defmodule GroupherServerWeb.Schema.CMS.Types do field(:link_addr, :string) field(:copy_right, :string) field(:body, :string) - field(:views, :integer) - - field(:is_pinned, :boolean) - field(:mark_delete, :boolean) - field(:author, :user, resolve: dataloader(CMS, :author)) - field(:article_tags, list_of(:article_tag), resolve: dataloader(CMS, :article_tags)) - field(:original_community, :community, resolve: dataloader(CMS, :original_community)) - field(:communities, list_of(:community), resolve: dataloader(CMS, :communities)) - - field(:meta, :article_meta) - field(:emotions, :article_emotions) - - # comments_count - # comments_participators - article_comments_fields() - viewer_has_state_fields() timestamp_fields(:article) end object :repo do - # interface(:article) - field(:id, :id) - field(:title, :string) + interface(:article) + + general_article_fields() + article_comments_fields() + field(:owner_name, :string) field(:owner_url, :string) field(:repo_url, :string) - field(:author, :user, resolve: dataloader(CMS, :author)) field(:desc, :string) field(:homepage_url, :string) @@ -159,21 +132,8 @@ defmodule GroupherServerWeb.Schema.CMS.Types do field(:contributors, list_of(:repo_contributor)) - field(:views, :integer) - field(:is_pinned, :boolean) - field(:mark_delete, :boolean) - field(:last_sync, :datetime) - field(:article_tags, list_of(:article_tag), resolve: dataloader(CMS, :article_tags)) - field(:original_community, :community, resolve: dataloader(CMS, :original_community)) - field(:communities, list_of(:community), resolve: dataloader(CMS, :communities)) - - article_comments_fields() - viewer_has_state_fields() - # comments_count - # comments_participators - timestamp_fields(:article) end diff --git a/lib/helper/utils/utils.ex b/lib/helper/utils/utils.ex index e7d206aef..62a978c72 100644 --- a/lib/helper/utils/utils.ex +++ b/lib/helper/utils/utils.ex @@ -182,16 +182,25 @@ defmodule Helper.Utils do def strip_struct(map) when is_map(map), do: map - @doc "turn GroupherServer.CMS.Model.Post -> :post" - def module_to_thread(module) do - module - |> to_string - |> String.split(".") + @doc """ + get atom name of a module + """ + def module_to_atom(%{__struct__: module_struct}) do + module_struct + |> Module.split() |> List.last() |> String.downcase() |> String.to_atom() end + def module_to_atom(module_struct) do + try do + module_struct |> struct |> module_to_atom + rescue + _ -> nil + end + end + @doc "html uniq id generator for editorjs" @spec uid(:html, map) :: String.t() def uid(:html, %{"id" => id}) when g_none_empty_str(id), do: id diff --git a/test/groupher_server_web/mutation/cms/comments/post_comment_test.exs b/test/groupher_server_web/mutation/cms/comments/post_comment_test.exs index 1c20e808c..c40d226cc 100644 --- a/test/groupher_server_web/mutation/cms/comments/post_comment_test.exs +++ b/test/groupher_server_web/mutation/cms/comments/post_comment_test.exs @@ -167,10 +167,11 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do mutation($id: ID!, $communityId: ID!){ lockPostComment(id: $id, communityId: $communityId) { id + title } } """ - + @tag :wip test "can lock a post's comment", ~m(community post)a do variables = %{id: post.id, communityId: community.id} passport_rules = %{community.raw => %{"post.lock_comment" => true}} diff --git a/test/helper/utils_test.exs b/test/helper/utils_test.exs index dfc543f9f..05a9ccc6e 100644 --- a/test/helper/utils_test.exs +++ b/test/helper/utils_test.exs @@ -1,6 +1,7 @@ defmodule GroupherServer.Test.Helper.UtilsTest do use GroupherServerWeb.ConnCase, async: true + alias GroupherServer.CMS.Model.Post alias Helper.Utils describe "map atom value up upcase str" do @@ -194,4 +195,17 @@ defmodule GroupherServer.Test.Helper.UtilsTest do assert "exsit_id" == Utils.uid(:html, %{"id" => "exsit_id"}) end end + + describe "[others]" do + @tag :wip + test "module_to_atom should work" do + assert :post == Post |> Utils.module_to_atom() + assert :post == %Post{} |> Utils.module_to_atom() + + # invalid case + assert nil == "whatever" |> Utils.module_to_atom() + assert nil == :whatever |> Utils.module_to_atom() + assert nil == 8848 |> Utils.module_to_atom() + end + end end