diff --git a/lib/groupher_server/accounts/collect_folder.ex b/lib/groupher_server/accounts/collect_folder.ex index 34efc1b7b..6ae561e53 100644 --- a/lib/groupher_server/accounts/collect_folder.ex +++ b/lib/groupher_server/accounts/collect_folder.ex @@ -12,10 +12,6 @@ defmodule GroupherServer.Accounts.CollectFolder do @required_fields ~w(user_id title)a @optional_fields ~w(index total_count private desc last_updated)a - @supported_threads [:post, :job, :repo] - - def supported_threads, do: @supported_threads - @type t :: %CollectFolder{} schema "collect_folders" do belongs_to(:user, User, foreign_key: :user_id) diff --git a/lib/groupher_server/accounts/delegates/collect_folder.ex b/lib/groupher_server/accounts/delegates/collect_folder.ex index 55a21ce40..ab87d60dc 100644 --- a/lib/groupher_server/accounts/delegates/collect_folder.ex +++ b/lib/groupher_server/accounts/delegates/collect_folder.ex @@ -9,7 +9,7 @@ defmodule GroupherServer.Accounts.Delegate.CollectFolder do alias Helper.QueryBuilder import Helper.ErrorCode - import Helper.Utils, only: [done: 1] + import Helper.Utils, only: [done: 1, get_config: 2] import ShortMaps @@ -24,7 +24,7 @@ defmodule GroupherServer.Accounts.Delegate.CollectFolder do # @max_article_count_per_collect_folder 300 @default_meta Embeds.CollectFolderMeta.default_meta() - @supported_collect_threads [:post, :job] + @article_threads get_config(:article, :article_threads) @doc """ list a user's not-private collect folders @@ -75,9 +75,15 @@ defmodule GroupherServer.Accounts.Delegate.CollectFolder do end defp do_paged_collect_folder_articles(folder, filter) do - Repo.preload(folder.collects, @supported_collect_threads) + article_preload = + @article_threads + |> Enum.reduce([], fn thread, acc -> + acc ++ Keyword.new([{thread, [author: :user]}]) + end) + + Repo.preload(folder.collects, article_preload) |> ORM.embeds_paginater(filter) - |> ORM.extract_articles(@supported_collect_threads) + |> ORM.extract_articles() |> done() end diff --git a/lib/groupher_server/accounts/delegates/publish.ex b/lib/groupher_server/accounts/delegates/publish.ex index bc295c625..cb3236950 100644 --- a/lib/groupher_server/accounts/delegates/publish.ex +++ b/lib/groupher_server/accounts/delegates/publish.ex @@ -75,9 +75,8 @@ defmodule GroupherServer.Accounts.Delegate.Publish do thread = thread |> to_string |> String.upcase() thread_atom = thread |> String.downcase() |> String.to_atom() - # article_preload = Keyword.new([{thread_atom, :author}]) - # query = from(comment in ArticleComment, preload: ^article_preload) - query = from(comment in ArticleComment, preload: ^thread_atom) + article_preload = Keyword.new([{thread_atom, [author: :user]}]) + query = from(comment in ArticleComment, preload: ^article_preload) query |> join(:inner, [comment], author in assoc(comment, :author)) diff --git a/lib/groupher_server/accounts/delegates/upvoted_articles.ex b/lib/groupher_server/accounts/delegates/upvoted_articles.ex index 4f8cc65e7..a78ae161f 100644 --- a/lib/groupher_server/accounts/delegates/upvoted_articles.ex +++ b/lib/groupher_server/accounts/delegates/upvoted_articles.ex @@ -3,7 +3,7 @@ defmodule GroupherServer.Accounts.Delegate.UpvotedArticles do get contents(posts, jobs ...) that user upvotes """ import Ecto.Query, warn: false - import Helper.Utils, only: [done: 1] + import Helper.Utils, only: [done: 1, get_config: 2] import ShortMaps alias Helper.{ORM, QueryBuilder} @@ -11,8 +11,7 @@ defmodule GroupherServer.Accounts.Delegate.UpvotedArticles do alias GroupherServer.CMS alias CMS.{ArticleUpvote} - # TODO: move to Model - @supported_uovoted_threads [:post, :job] + @article_threads get_config(:article, :article_threads) @doc """ get paged upvoted articles @@ -31,13 +30,19 @@ defmodule GroupherServer.Accounts.Delegate.UpvotedArticles do end defp load_upvoted_articles(where_query, %{page: page, size: size} = filter) do - query = from(a in ArticleUpvote, preload: ^@supported_uovoted_threads) + article_preload = + @article_threads + |> Enum.reduce([], fn thread, acc -> + acc ++ Keyword.new([{thread, [author: :user]}]) + end) + + query = from(a in ArticleUpvote, preload: ^article_preload) query |> where(^where_query) |> QueryBuilder.filter_pack(filter) |> ORM.paginater(~m(page size)a) - |> ORM.extract_articles(@supported_uovoted_threads) + |> ORM.extract_articles() |> done() end end diff --git a/lib/groupher_server/accounts/embeds/collect_folder_meta.ex b/lib/groupher_server/accounts/embeds/collect_folder_meta.ex index e90416143..83104e3d2 100644 --- a/lib/groupher_server/accounts/embeds/collect_folder_meta.ex +++ b/lib/groupher_server/accounts/embeds/collect_folder_meta.ex @@ -10,12 +10,12 @@ defmodule GroupherServer.Accounts.Embeds.CollectFolderMeta.Macros do field(:has_repo, :boolean, default: false) field(:repo_count, :integer, default: 0) """ - alias GroupherServer.Accounts.CollectFolder + import Helper.Utils, only: [get_config: 2] - @supported_threads CollectFolder.supported_threads() + @article_threads get_config(:article, :article_threads) defmacro threads_fields() do - @supported_threads + @article_threads |> Enum.map(fn thread -> quote do field(unquote(:"has_#{thread}"), :boolean, default: false) @@ -27,21 +27,20 @@ end defmodule GroupherServer.Accounts.Embeds.CollectFolderMeta do @moduledoc """ - general article meta info for article-like content, like @supported_threads + general article meta info for articles """ use Ecto.Schema import Ecto.Changeset import GroupherServer.Accounts.Embeds.CollectFolderMeta.Macros + import Helper.Utils, only: [get_config: 2] - alias GroupherServer.Accounts.CollectFolder + @article_threads get_config(:article, :article_threads) - @supported_threads CollectFolder.supported_threads() - - @optional_fields Enum.map(@supported_threads, &:"#{&1}_count") ++ - Enum.map(@supported_threads, &:"has_#{&1}") + @optional_fields Enum.map(@article_threads, &:"#{&1}_count") ++ + Enum.map(@article_threads, &:"has_#{&1}") def default_meta() do - @supported_threads + @article_threads |> Enum.reduce([], fn thread, acc -> acc ++ ["#{thread}_count": 0, "has_#{thread}": false] end) |> Enum.into(%{}) end diff --git a/lib/groupher_server_web/schema/Helper/fields.ex b/lib/groupher_server_web/schema/Helper/fields.ex index 7acab3621..169088ddb 100644 --- a/lib/groupher_server_web/schema/Helper/fields.ex +++ b/lib/groupher_server_web/schema/Helper/fields.ex @@ -9,7 +9,6 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do @page_size get_config(:general, :page_size) @supported_emotions get_config(:article, :supported_emotions) @supported_comment_emotions get_config(:article, :comment_supported_emotions) - @supported_collect_folder_threads Accounts.CollectFolder.supported_threads() @article_threads get_config(:article, :article_threads) @@ -242,7 +241,7 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do general collect folder meta info """ defmacro collect_folder_meta_fields() do - @supported_collect_folder_threads + @article_threads |> Enum.map(fn thread -> quote do field(unquote(:"has_#{thread}"), :boolean) diff --git a/lib/groupher_server_web/schema/cms/cms_types.ex b/lib/groupher_server_web/schema/cms/cms_types.ex index ffe3b02bf..80c01623c 100644 --- a/lib/groupher_server_web/schema/cms/cms_types.ex +++ b/lib/groupher_server_web/schema/cms/cms_types.ex @@ -27,7 +27,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do field(:id, :id) # field(:body_html, :string) field(:title, :string) - field(:author, :user, resolve: dataloader(CMS, :author)) + field(:author, :common_user) end object :common_article_comment do diff --git a/lib/helper/orm.ex b/lib/helper/orm.ex index e16f627af..d72e59e15 100644 --- a/lib/helper/orm.ex +++ b/lib/helper/orm.ex @@ -295,6 +295,9 @@ defmodule Helper.ORM do |> Repo.update() end + @doc """ + extract common article info and assign it to 'article' field + """ def extract_and_assign_article(%{entries: entries} = paged_articles) do entries = Enum.map(entries, fn item -> @@ -307,24 +310,27 @@ defmodule Helper.ORM do @doc "extract common articles info" @spec extract_articles(T.paged_data(), [Atom.t()]) :: T.paged_article_common() - def extract_articles(%{entries: entries} = paged_articles, supported_threads) do + def extract_articles(%{entries: entries} = paged_articles, threads \\ @article_threads) do paged_articles - |> Map.put(:entries, Enum.map(entries, &extract_article_info(&1, supported_threads))) + |> Map.put(:entries, Enum.map(entries, &extract_article_info(&1, threads))) end - defp extract_article_info(reaction, supported_threads) do - thread = Enum.find(supported_threads, &(not is_nil(Map.get(reaction, &1)))) + defp extract_article_info(reaction, threads) do + thread = Enum.find(threads, &(not is_nil(Map.get(reaction, &1)))) article = Map.get(reaction, thread) export_article_info(thread, article) end defp export_article_info(thread, article) do + author = article.author.user + %{ thread: thread, id: article.id, title: article.title, - upvotes_count: Map.get(article, :upvotes_count) + upvotes_count: Map.get(article, :upvotes_count), + author: author } end end diff --git a/test/groupher_server/accounts/published/published_jobs_test.exs b/test/groupher_server/accounts/published/published_jobs_test.exs index 39be4b13b..1d1a0c797 100644 --- a/test/groupher_server/accounts/published/published_jobs_test.exs +++ b/test/groupher_server/accounts/published/published_jobs_test.exs @@ -72,7 +72,6 @@ defmodule GroupherServer.Test.Accounts.Published.Job do end describe "[publised job comments]" do - @tag :wip2 test "can get published article comments", ~m(job user)a do total_count = 10 diff --git a/test/groupher_server/accounts/published/published_posts_test.exs b/test/groupher_server/accounts/published/published_posts_test.exs index 38697221e..ed54e432a 100644 --- a/test/groupher_server/accounts/published/published_posts_test.exs +++ b/test/groupher_server/accounts/published/published_posts_test.exs @@ -72,7 +72,6 @@ defmodule GroupherServer.Test.Accounts.Published.Post do end describe "[publised post comments]" do - @tag :wip2 test "can get published article comments", ~m(post user)a do total_count = 10 diff --git a/test/groupher_server/accounts/published/published_repos_test.exs b/test/groupher_server/accounts/published/published_repos_test.exs index 8032e719a..102bad639 100644 --- a/test/groupher_server/accounts/published/published_repos_test.exs +++ b/test/groupher_server/accounts/published/published_repos_test.exs @@ -72,7 +72,6 @@ defmodule GroupherServer.Test.Accounts.Published.Repo do end describe "[publised repo comments]" do - @tag :wip2 test "can get published article comments", ~m(repo user)a do total_count = 10 diff --git a/test/groupher_server/accounts/reacted_articles_test.exs b/test/groupher_server/accounts/reacted_articles_test.exs index 3d90cc29d..43237e749 100644 --- a/test/groupher_server/accounts/reacted_articles_test.exs +++ b/test/groupher_server/accounts/reacted_articles_test.exs @@ -14,6 +14,7 @@ defmodule GroupherServer.Test.Accounts.ReactedContents do end describe "[user upvoted articles]" do + @tag :wip2 test "user can get paged upvoted common articles", ~m(user post job)a do {:ok, _} = CMS.upvote_article(:post, post.id, user) {:ok, _} = CMS.upvote_article(:job, job.id, user) @@ -28,8 +29,8 @@ defmodule GroupherServer.Test.Accounts.ReactedContents do assert job.id == article_job |> Map.get(:id) assert post.id == article_post |> Map.get(:id) - assert [:id, :thread, :title, :upvotes_count] == article_post |> Map.keys() - assert [:id, :thread, :title, :upvotes_count] == article_job |> Map.keys() + assert [:author, :id, :thread, :title, :upvotes_count] == article_post |> Map.keys() + assert [:author, :id, :thread, :title, :upvotes_count] == article_job |> Map.keys() end test "user can get paged upvoted posts by thread filter", ~m(user post job)a do diff --git a/test/groupher_server_web/query/accounts/published/published_jobs_test.exs b/test/groupher_server_web/query/accounts/published/published_jobs_test.exs index 7b01e0a75..7da20bf96 100644 --- a/test/groupher_server_web/query/accounts/published/published_jobs_test.exs +++ b/test/groupher_server_web/query/accounts/published/published_jobs_test.exs @@ -34,7 +34,7 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Jobs do } } """ - @tag :wip2 + test "can get published jobs", ~m(guest_conn community user)a do job_attrs = mock_attrs(:job, %{community_id: community.id}) @@ -62,6 +62,10 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Jobs do article { id title + author { + nickname + login + } } } totalPages @@ -71,7 +75,7 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Jobs do } } """ - @tag :wip2 + test "user can get paged published comments on job", ~m(guest_conn user job)a do pub_comments = Enum.reduce(1..@publish_count, [], fn _, acc -> @@ -82,14 +86,18 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Jobs do random_comment_id = pub_comments |> Enum.random() |> Map.get(:id) |> to_string variables = %{login: user.login, thread: "JOB", filter: %{page: 1, size: 20}} + results = guest_conn |> query_result(@query, variables, "pagedPublishedArticleComments") + entries = results["entries"] assert results |> is_valid_pagination? assert results["totalCount"] == @publish_count - assert results["entries"] |> Enum.all?(&(&1["article"]["id"] == to_string(job.id))) - assert results["entries"] |> Enum.all?(&(&1["author"]["id"] == to_string(user.id))) - assert results["entries"] |> Enum.any?(&(&1["id"] == random_comment_id)) + assert entries |> Enum.all?(&(not is_nil(&1["article"]["author"]))) + + assert entries |> Enum.all?(&(&1["article"]["id"] == to_string(job.id))) + assert entries |> Enum.all?(&(&1["author"]["id"] == to_string(user.id))) + assert entries |> Enum.any?(&(&1["id"] == random_comment_id)) end end end diff --git a/test/groupher_server_web/query/accounts/published/published_posts_test.exs b/test/groupher_server_web/query/accounts/published/published_posts_test.exs index 64ffb5a17..7bf490ad5 100644 --- a/test/groupher_server_web/query/accounts/published/published_posts_test.exs +++ b/test/groupher_server_web/query/accounts/published/published_posts_test.exs @@ -34,7 +34,7 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Posts do } } """ - @tag :wip2 + test "can get published posts", ~m(guest_conn community user)a do post_attrs = mock_attrs(:post, %{community_id: community.id}) @@ -62,6 +62,10 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Posts do article { id title + author { + nickname + login + } } } totalPages @@ -71,7 +75,7 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Posts do } } """ - @tag :wip2 + test "user can get paged published comments on post", ~m(guest_conn user post)a do pub_comments = Enum.reduce(1..@publish_count, [], fn _, acc -> @@ -82,14 +86,18 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Posts do random_comment_id = pub_comments |> Enum.random() |> Map.get(:id) |> to_string variables = %{login: user.login, thread: "POST", filter: %{page: 1, size: 20}} + results = guest_conn |> query_result(@query, variables, "pagedPublishedArticleComments") + entries = results["entries"] assert results |> is_valid_pagination? assert results["totalCount"] == @publish_count - assert results["entries"] |> Enum.all?(&(&1["article"]["id"] == to_string(post.id))) - assert results["entries"] |> Enum.all?(&(&1["author"]["id"] == to_string(user.id))) - assert results["entries"] |> Enum.any?(&(&1["id"] == random_comment_id)) + assert entries |> Enum.all?(&(not is_nil(&1["article"]["author"]))) + + assert entries |> Enum.all?(&(&1["article"]["id"] == to_string(post.id))) + assert entries |> Enum.all?(&(&1["author"]["id"] == to_string(user.id))) + assert entries |> Enum.any?(&(&1["id"] == random_comment_id)) end end end diff --git a/test/groupher_server_web/query/accounts/published/published_repos_test.exs b/test/groupher_server_web/query/accounts/published/published_repos_test.exs index 39265cc8c..19227f564 100644 --- a/test/groupher_server_web/query/accounts/published/published_repos_test.exs +++ b/test/groupher_server_web/query/accounts/published/published_repos_test.exs @@ -34,7 +34,7 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Repos do } } """ - @tag :wip2 + test "can get published repos", ~m(guest_conn community user)a do repo_attrs = mock_attrs(:repo, %{community_id: community.id}) @@ -62,6 +62,10 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Repos do article { id title + author { + nickname + login + } } } totalPages @@ -71,7 +75,7 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Repos do } } """ - @tag :wip2 + test "user can get paged published comments on repo", ~m(guest_conn user repo)a do pub_comments = Enum.reduce(1..@publish_count, [], fn _, acc -> @@ -82,14 +86,18 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Repos do random_comment_id = pub_comments |> Enum.random() |> Map.get(:id) |> to_string variables = %{login: user.login, thread: "REPO", filter: %{page: 1, size: 20}} + results = guest_conn |> query_result(@query, variables, "pagedPublishedArticleComments") + entries = results["entries"] assert results |> is_valid_pagination? assert results["totalCount"] == @publish_count - assert results["entries"] |> Enum.all?(&(&1["article"]["id"] == to_string(repo.id))) - assert results["entries"] |> Enum.all?(&(&1["author"]["id"] == to_string(user.id))) - assert results["entries"] |> Enum.any?(&(&1["id"] == random_comment_id)) + assert entries |> Enum.all?(&(not is_nil(&1["article"]["author"]))) + + assert entries |> Enum.all?(&(&1["article"]["id"] == to_string(repo.id))) + assert entries |> Enum.all?(&(&1["author"]["id"] == to_string(user.id))) + assert entries |> Enum.any?(&(&1["id"] == random_comment_id)) end end end