diff --git a/lib/groupher_server/accounts/delegates/upvoted_articles.ex b/lib/groupher_server/accounts/delegates/upvoted_articles.ex index 04f4cda40..a015fc093 100644 --- a/lib/groupher_server/accounts/delegates/upvoted_articles.ex +++ b/lib/groupher_server/accounts/delegates/upvoted_articles.ex @@ -17,8 +17,8 @@ defmodule GroupherServer.Accounts.Delegate.UpvotedArticles do get paged upvoted articles """ def paged_upvoted_articles(user_id, %{thread: thread} = filter) do - thread_upcase = thread |> to_string |> String.upcase() - where_query = dynamic([a], a.user_id == ^user_id and a.thread == ^thread_upcase) + thread = thread |> to_string |> String.upcase() + where_query = dynamic([a], a.user_id == ^user_id and a.thread == ^thread) load_upvoted_articles(where_query, filter) end diff --git a/lib/groupher_server/cms/cms.ex b/lib/groupher_server/cms/cms.ex index 4ae88df50..d883a9935 100644 --- a/lib/groupher_server/cms/cms.ex +++ b/lib/groupher_server/cms/cms.ex @@ -12,6 +12,7 @@ defmodule GroupherServer.CMS do ArticleCURD, ArticleCommunity, ArticleEmotion, + CitedContent, CommentCurd, ArticleCollect, ArticleUpvote, @@ -96,6 +97,8 @@ defmodule GroupherServer.CMS do defdelegate sink_article(thread, id), to: ArticleCURD defdelegate undo_sink_article(thread, id), to: ArticleCURD + defdelegate paged_citing_contents(type, id, filter), to: CitedContent + defdelegate upvote_article(thread, article_id, user), to: ArticleUpvote defdelegate undo_upvote_article(thread, article_id, user), to: ArticleUpvote diff --git a/lib/groupher_server/cms/delegates/article_collect.ex b/lib/groupher_server/cms/delegates/article_collect.ex index 5da7f898f..19126bb99 100644 --- a/lib/groupher_server/cms/delegates/article_collect.ex +++ b/lib/groupher_server/cms/delegates/article_collect.ex @@ -46,8 +46,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleCollect do update_article_reaction_user_list(:collect, article, user_id, :add) end) |> Multi.run(:create_collect, fn _, _ -> - thread_upcase = thread |> to_string |> String.upcase() - args = Map.put(%{user_id: user_id, thread: thread_upcase}, info.foreign_key, article.id) + thread = thread |> to_string |> String.upcase() + args = Map.put(%{user_id: user_id, thread: thread}, info.foreign_key, article.id) ORM.create(ArticleCollect, args) end) @@ -131,8 +131,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleCollect do defp collection_findby_args(thread, article_id, user_id) do with {:ok, info} <- match(thread) do - thread_upcase = thread |> to_string |> String.upcase() - %{thread: thread_upcase, user_id: user_id} |> Map.put(info.foreign_key, article_id) + thread = thread |> to_string |> String.upcase() + %{thread: thread, user_id: user_id} |> Map.put(info.foreign_key, article_id) end end diff --git a/lib/groupher_server/cms/delegates/article_community.ex b/lib/groupher_server/cms/delegates/article_community.ex index f11d2d352..f6b2d6c7b 100644 --- a/lib/groupher_server/cms/delegates/article_community.ex +++ b/lib/groupher_server/cms/delegates/article_community.ex @@ -41,10 +41,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do defp pack_pin_args(thread, article_id, community_id) do with {:ok, info} <- match(thread), {:ok, community} <- ORM.find(Community, community_id) do - thread_upcase = thread |> to_string |> String.upcase() + thread = thread |> to_string |> String.upcase() Map.put( - %{community_id: community.id, thread: thread_upcase}, + %{community_id: community.id, thread: thread}, info.foreign_key, article_id ) @@ -145,12 +145,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do # check if the thread has aready enough pinned articles defp check_pinned_article_count(community_id, thread) do - thread_upcase = thread |> to_string |> String.upcase() + thread = thread |> to_string |> String.upcase() query = - from(p in PinnedArticle, - where: p.community_id == ^community_id and p.thread == ^thread_upcase - ) + from(p in PinnedArticle, where: p.community_id == ^community_id and p.thread == ^thread) pinned_articles = query |> Repo.all() diff --git a/lib/groupher_server/cms/delegates/article_upvote.ex b/lib/groupher_server/cms/delegates/article_upvote.ex index 6b5478987..9f0cec4c7 100644 --- a/lib/groupher_server/cms/delegates/article_upvote.ex +++ b/lib/groupher_server/cms/delegates/article_upvote.ex @@ -43,8 +43,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleUpvote do Accounts.achieve(%User{id: achiever_id}, :inc, :upvote) end) |> Multi.run(:create_upvote, fn _, _ -> - thread_upcase = thread |> to_string |> String.upcase() - args = Map.put(%{user_id: user_id, thread: thread_upcase}, info.foreign_key, article.id) + thread = thread |> to_string |> String.upcase() + args = Map.put(%{user_id: user_id, thread: thread}, info.foreign_key, article.id) with {:ok, _} <- ORM.create(ArticleUpvote, args) do ORM.find(info.model, article.id) diff --git a/lib/groupher_server/cms/delegates/cited_content.ex b/lib/groupher_server/cms/delegates/cited_content.ex new file mode 100644 index 000000000..fbcd82bd4 --- /dev/null +++ b/lib/groupher_server/cms/delegates/cited_content.ex @@ -0,0 +1,86 @@ +defmodule GroupherServer.CMS.Delegate.CitedContent do + @moduledoc """ + CURD operation on post/job ... + """ + import Ecto.Query, warn: false + + import Helper.Utils, only: [done: 1, get_config: 2] + import ShortMaps + + alias Helper.Types, as: T + alias GroupherServer.{CMS, Repo} + alias Helper.{ORM, QueryBuilder} + + alias CMS.Model.CitedContent + + @article_threads get_config(:article, :threads) + + @article_preloads @article_threads |> Enum.map(&Keyword.new([{&1, [author: :user]}])) + + @comment_article_preloads @article_threads |> Enum.map(&Keyword.new([{:comment, &1}])) + @cited_preloads @article_preloads ++ [[comment: :author] ++ @comment_article_preloads] + + @doc "get paged citing contents" + def paged_citing_contents(cited_by_type, cited_by_id, %{page: page, size: size} = filter) do + cited_by_type = cited_by_type |> to_string |> String.upcase() + + CitedContent + |> where([c], c.cited_by_id == ^cited_by_id and c.cited_by_type == ^cited_by_type) + |> QueryBuilder.filter_pack(Map.merge(filter, %{sort: :asc_inserted})) + |> ORM.paginater(~m(page size)a) + |> extract_contents + |> done + end + + def extract_contents(%{entries: entries} = paged_contents) do + entries = entries |> Repo.preload(@cited_preloads) |> Enum.map(&shape_article(&1)) + + Map.put(paged_contents, :entries, entries) + end + + defp thread_to_atom(thread), do: thread |> String.downcase() |> String.to_atom() + + # shape comment cite + @spec shape_article(CitedContent.t()) :: T.cite_info() + defp shape_article(%CitedContent{comment_id: comment_id} = cited) when not is_nil(comment_id) do + %{ + block_linker: block_linker, + cited_by_type: cited_by_type, + comment: comment, + inserted_at: inserted_at + } = cited + + comment_thread = comment.thread |> String.downcase() |> String.to_atom() + article = comment |> Map.get(comment_thread) + user = comment.author |> Map.take([:login, :nickname, :avatar]) + + article + |> Map.take([:id, :title]) + |> Map.merge(%{ + inserted_at: inserted_at, + user: user, + thread: thread_to_atom(cited_by_type), + comment_id: comment.id, + block_linker: block_linker + }) + end + + # shape general article cite + defp shape_article(%CitedContent{} = cited) do + %{block_linker: block_linker, cited_by_type: cited_by_type, inserted_at: inserted_at} = cited + + thread = thread_to_atom(cited_by_type) + article = Map.get(cited, thread) + + user = get_in(article, [:author, :user]) |> Map.take([:login, :nickname, :avatar]) + + article + |> Map.take([:id, :title]) + |> Map.merge(%{ + user: user, + thread: thread, + block_linker: block_linker, + inserted_at: inserted_at + }) + end +end diff --git a/lib/groupher_server_web/resolvers/cms_resolver.ex b/lib/groupher_server_web/resolvers/cms_resolver.ex index 3c081d0ac..7cf3fad7c 100644 --- a/lib/groupher_server_web/resolvers/cms_resolver.ex +++ b/lib/groupher_server_web/resolvers/cms_resolver.ex @@ -102,6 +102,10 @@ defmodule GroupherServerWeb.Resolvers.CMS do CMS.undo_report_article(thread, id, user) end + def paged_citing_contents(_root, ~m(content id filter)a, _info) do + CMS.paged_citing_contents(content, id, filter) + end + # ####################### # thread reaction .. # ####################### diff --git a/lib/groupher_server_web/schema/cms/cms_metrics.ex b/lib/groupher_server_web/schema/cms/cms_metrics.ex index eba72595f..5257fc809 100644 --- a/lib/groupher_server_web/schema/cms/cms_metrics.ex +++ b/lib/groupher_server_web/schema/cms/cms_metrics.ex @@ -41,6 +41,11 @@ defmodule GroupherServerWeb.Schema.CMS.Metrics do value(:share) end + enum :content do + article_values() + value(:comment) + end + enum :when_enum do value(:today) value(:this_week) diff --git a/lib/groupher_server_web/schema/cms/cms_queries.ex b/lib/groupher_server_web/schema/cms/cms_queries.ex index ad64f0dc3..c45be857a 100644 --- a/lib/groupher_server_web/schema/cms/cms_queries.ex +++ b/lib/groupher_server_web/schema/cms/cms_queries.ex @@ -124,6 +124,15 @@ defmodule GroupherServerWeb.Schema.CMS.Queries do resolve(&R.CMS.paged_reports/3) end + @desc "paged citings list" + field :paged_citing_contents, :paged_citings do + arg(:id, non_null(:id)) + arg(:content, :content, default_value: :post) + arg(:filter, :paged_filter) + + resolve(&R.CMS.paged_citing_contents/3) + end + @desc "search communities by title" field :search_communities, :paged_communities do arg(:title, non_null(:string)) diff --git a/lib/groupher_server_web/schema/cms/cms_types.ex b/lib/groupher_server_web/schema/cms/cms_types.ex index d521bb860..4e774fdfd 100644 --- a/lib/groupher_server_web/schema/cms/cms_types.ex +++ b/lib/groupher_server_web/schema/cms/cms_types.ex @@ -317,6 +317,17 @@ defmodule GroupherServerWeb.Schema.CMS.Types do timestamp_fields() end + object :citing do + field(:id, :id) + field(:thread, :string) + field(:title, :string) + field(:block_linker, list_of(:string)) + field(:comment_id, :id) + field(:user, :common_user) + + timestamp_fields() + end + paged_article_objects() object :paged_reports do @@ -324,6 +335,11 @@ defmodule GroupherServerWeb.Schema.CMS.Types do pagination_fields() end + object :paged_citings do + field(:entries, list_of(:citing)) + pagination_fields() + end + object :paged_categories do field(:entries, list_of(:category)) pagination_fields() diff --git a/lib/helper/types.ex b/lib/helper/types.ex index 6d251d40b..f55404bd9 100644 --- a/lib/helper/types.ex +++ b/lib/helper/types.ex @@ -33,7 +33,7 @@ defmodule Helper.Types do company: nil | String.t() } - @type article_thread :: :post | :job | :repo + @type article_thread :: :post | :job | :repo | :blog @type paged_filter :: %{ page: Integer.t(), @@ -182,4 +182,18 @@ defmodule Helper.Types do html fragment """ @type html :: String.t() + + @type cite_info :: %{ + id: Integer.t(), + thread: article_thread, + title: String.t(), + inserted_at: String.t(), + block_linker: [String.t()], + comment_id: Integer.t() | nil, + user: %{ + login: String.t(), + avatar: String.t(), + nickname: String.t() + } + } end diff --git a/test/groupher_server/cms/cite_contents/cite_blog_test.exs b/test/groupher_server/cms/cite_contents/cite_blog_test.exs index 31868364b..27ae3da51 100644 --- a/test/groupher_server/cms/cite_contents/cite_blog_test.exs +++ b/test/groupher_server/cms/cite_contents/cite_blog_test.exs @@ -73,7 +73,6 @@ defmodule GroupherServer.Test.CMS.CiteContent.Blog do assert blog.meta.citing_count == 0 end - @tag :wip test "cited comment itself should not work", ~m(user blog)a do {:ok, cited_comment} = CMS.create_comment(:blog, blog.id, mock_rich_text("hello"), user) @@ -91,7 +90,6 @@ defmodule GroupherServer.Test.CMS.CiteContent.Blog do assert cited_comment.meta.citing_count == 0 end - @tag :wip test "can cite blog's comment in blog", ~m(community user blog blog2 blog_attrs)a do {:ok, comment} = CMS.create_comment(:blog, blog.id, mock_rich_text("hello"), user) @@ -111,7 +109,6 @@ defmodule GroupherServer.Test.CMS.CiteContent.Blog do assert cite_content.cited_by_type == "COMMENT" end - @tag :wip test "can cite a comment in a comment", ~m(user blog)a do {:ok, cited_comment} = CMS.create_comment(:blog, blog.id, mock_rich_text("hello"), user) @@ -164,4 +161,61 @@ defmodule GroupherServer.Test.CMS.CiteContent.Blog do assert blog5.meta.citing_count == 1 end end + + describe "[cite pagi]" do + test "can get paged cited articles.", ~m(user community blog2 blog_attrs)a do + {:ok, comment} = + CMS.create_comment( + :blog, + blog2.id, + mock_comment(~s(the )), + user + ) + + Process.sleep(1000) + + body = + mock_rich_text( + ~s(the ), + ~s(the ) + ) + + blog_attrs = blog_attrs |> Map.merge(%{body: body}) + {:ok, blog_x} = CMS.create_article(community, :blog, blog_attrs, user) + + Process.sleep(1000) + body = mock_rich_text(~s(the )) + blog_attrs = blog_attrs |> Map.merge(%{body: body}) + {:ok, blog_y} = CMS.create_article(community, :blog, blog_attrs, user) + + CiteTasks.handle(blog_x) + CiteTasks.handle(comment) + CiteTasks.handle(blog_y) + + {:ok, result} = CMS.paged_citing_contents("BLOG", blog2.id, %{page: 1, size: 10}) + + entries = result.entries + + result_comment = entries |> List.first() + result_blog_x = entries |> Enum.at(1) + result_blog_y = entries |> List.last() + + article_map_keys = [:block_linker, :id, :inserted_at, :thread, :title, :user] + + assert result_comment.comment_id == comment.id + assert result_comment.id == blog2.id + assert result_comment.title == blog2.title + + assert result_blog_x.id == blog_x.id + assert result_blog_x.block_linker |> length == 2 + assert result_blog_x |> Map.keys() == article_map_keys + + assert result_blog_y.id == blog_y.id + assert result_blog_y.block_linker |> length == 1 + assert result_blog_y |> Map.keys() == article_map_keys + + assert result |> is_valid_pagination?(:raw) + assert result.total_count == 3 + end + end end diff --git a/test/groupher_server/cms/cite_contents/cite_job_test.exs b/test/groupher_server/cms/cite_contents/cite_job_test.exs index 9e40ed24e..6710f2ba8 100644 --- a/test/groupher_server/cms/cite_contents/cite_job_test.exs +++ b/test/groupher_server/cms/cite_contents/cite_job_test.exs @@ -73,7 +73,6 @@ defmodule GroupherServer.Test.CMS.CiteContent.Job do assert job.meta.citing_count == 0 end - @tag :wip test "cited comment itself should not work", ~m(user job)a do {:ok, cited_comment} = CMS.create_comment(:job, job.id, mock_rich_text("hello"), user) @@ -91,7 +90,6 @@ defmodule GroupherServer.Test.CMS.CiteContent.Job do assert cited_comment.meta.citing_count == 0 end - @tag :wip test "can cite job's comment in job", ~m(community user job job2 job_attrs)a do {:ok, comment} = CMS.create_comment(:job, job.id, mock_rich_text("hello"), user) @@ -111,7 +109,6 @@ defmodule GroupherServer.Test.CMS.CiteContent.Job do assert cite_content.cited_by_type == "COMMENT" end - @tag :wip test "can cite a comment in a comment", ~m(user job)a do {:ok, cited_comment} = CMS.create_comment(:job, job.id, mock_rich_text("hello"), user) @@ -164,4 +161,60 @@ defmodule GroupherServer.Test.CMS.CiteContent.Job do assert job5.meta.citing_count == 1 end end + + describe "[cite pagi]" do + test "can get paged cited articles.", ~m(user community job2 job_attrs)a do + {:ok, comment} = + CMS.create_comment( + :job, + job2.id, + mock_comment(~s(the )), + user + ) + + Process.sleep(1000) + + body = + mock_rich_text( + ~s(the ), + ~s(the ) + ) + + job_attrs = job_attrs |> Map.merge(%{body: body}) + {:ok, job_x} = CMS.create_article(community, :job, job_attrs, user) + Process.sleep(1000) + body = mock_rich_text(~s(the )) + job_attrs = job_attrs |> Map.merge(%{body: body}) + {:ok, job_y} = CMS.create_article(community, :job, job_attrs, user) + + CiteTasks.handle(job_x) + CiteTasks.handle(comment) + CiteTasks.handle(job_y) + + {:ok, result} = CMS.paged_citing_contents("JOB", job2.id, %{page: 1, size: 10}) + + entries = result.entries + + result_comment = entries |> List.first() + result_job_x = entries |> Enum.at(1) + result_job_y = entries |> List.last() + + article_map_keys = [:block_linker, :id, :inserted_at, :thread, :title, :user] + + assert result_comment.comment_id == comment.id + assert result_comment.id == job2.id + assert result_comment.title == job2.title + + assert result_job_x.id == job_x.id + assert result_job_x.block_linker |> length == 2 + assert result_job_x |> Map.keys() == article_map_keys + + assert result_job_y.id == job_y.id + assert result_job_y.block_linker |> length == 1 + assert result_job_y |> Map.keys() == article_map_keys + + assert result |> is_valid_pagination?(:raw) + assert result.total_count == 3 + end + end end diff --git a/test/groupher_server/cms/cite_contents/cite_post_test.exs b/test/groupher_server/cms/cite_contents/cite_post_test.exs index f5f71fcfb..d610876c5 100644 --- a/test/groupher_server/cms/cite_contents/cite_post_test.exs +++ b/test/groupher_server/cms/cite_contents/cite_post_test.exs @@ -28,7 +28,6 @@ defmodule GroupherServer.Test.CMS.CiteContent.Post do end describe "[cite basic]" do - # test "cited multi post should work", ~m(user community post2 post3 post4 post5 post_attrs)a do body = mock_rich_text( @@ -62,7 +61,6 @@ defmodule GroupherServer.Test.CMS.CiteContent.Post do assert post5.meta.citing_count == 1 end - @tag :wip test "cited post itself should not work", ~m(user community post_attrs)a do {:ok, post} = CMS.create_article(community, :post, post_attrs, user) @@ -75,7 +73,6 @@ defmodule GroupherServer.Test.CMS.CiteContent.Post do assert post.meta.citing_count == 0 end - @tag :wip test "cited comment itself should not work", ~m(user post)a do {:ok, cited_comment} = CMS.create_comment(:post, post.id, mock_rich_text("hello"), user) @@ -93,7 +90,6 @@ defmodule GroupherServer.Test.CMS.CiteContent.Post do assert cited_comment.meta.citing_count == 0 end - @tag :wip test "can cite post's comment in post", ~m(community user post post2 post_attrs)a do {:ok, comment} = CMS.create_comment(:post, post.id, mock_rich_text("hello"), user) @@ -113,7 +109,6 @@ defmodule GroupherServer.Test.CMS.CiteContent.Post do assert cite_content.cited_by_type == "COMMENT" end - @tag :wip test "can cite a comment in a comment", ~m(user post)a do {:ok, cited_comment} = CMS.create_comment(:post, post.id, mock_rich_text("hello"), user) @@ -166,4 +161,61 @@ defmodule GroupherServer.Test.CMS.CiteContent.Post do assert post5.meta.citing_count == 1 end end + + describe "[cite pagi]" do + test "can get paged cited articles.", ~m(user community post2 post_attrs)a do + {:ok, comment} = + CMS.create_comment( + :post, + post2.id, + mock_comment(~s(the )), + user + ) + + Process.sleep(1000) + + body = + mock_rich_text( + ~s(the ), + ~s(the ) + ) + + post_attrs = post_attrs |> Map.merge(%{body: body}) + {:ok, post_x} = CMS.create_article(community, :post, post_attrs, user) + + Process.sleep(1000) + body = mock_rich_text(~s(the )) + post_attrs = post_attrs |> Map.merge(%{body: body}) + {:ok, post_y} = CMS.create_article(community, :post, post_attrs, user) + + CiteTasks.handle(post_x) + CiteTasks.handle(comment) + CiteTasks.handle(post_y) + + {:ok, result} = CMS.paged_citing_contents("POST", post2.id, %{page: 1, size: 10}) + + entries = result.entries + + result_comment = entries |> List.first() + result_post_x = entries |> Enum.at(1) + result_post_y = entries |> List.last() + + article_map_keys = [:block_linker, :id, :inserted_at, :thread, :title, :user] + + assert result_comment.comment_id == comment.id + assert result_comment.id == post2.id + assert result_comment.title == post2.title + + assert result_post_x.id == post_x.id + assert result_post_x.block_linker |> length == 2 + assert result_post_x |> Map.keys() == article_map_keys + + assert result_post_y.id == post_y.id + assert result_post_y.block_linker |> length == 1 + assert result_post_y |> Map.keys() == article_map_keys + + assert result |> is_valid_pagination?(:raw) + assert result.total_count == 3 + end + end end diff --git a/test/groupher_server_web/query/cms/citings/blog_citing_test.exs b/test/groupher_server_web/query/cms/citings/blog_citing_test.exs new file mode 100644 index 000000000..a464d829d --- /dev/null +++ b/test/groupher_server_web/query/cms/citings/blog_citing_test.exs @@ -0,0 +1,83 @@ +defmodule GroupherServer.Test.Query.AbuseReports.BlogCiting do + @moduledoc false + + use GroupherServer.TestTools + import Helper.Utils, only: [get_config: 2] + + alias GroupherServer.CMS + + alias CMS.Delegate.CiteTasks + + @site_host get_config(:general, :site_host) + + setup do + {:ok, blog} = db_insert(:blog) + {:ok, user} = db_insert(:user) + + {:ok, community} = db_insert(:community) + blog_attrs = mock_attrs(:blog, %{community_id: community.id}) + + guest_conn = simu_conn(:guest) + + {:ok, ~m(guest_conn community blog blog_attrs user)a} + end + + describe "[query paged_blogs filter pagination]" do + # id + @query """ + query($content: Content!, $id: ID!, $filter: PageFilter!) { + pagedCitingContents(id: $id, content: $content, filter: $filter) { + entries { + id + title + user { + login + nickname + avatar + } + commentId + } + totalPages + totalCount + pageSize + pageNumber + } + } + """ + @tag :wip + test "should get paged cittings", ~m(guest_conn community blog_attrs user)a do + {:ok, blog2} = db_insert(:blog) + + {:ok, comment} = + CMS.create_comment( + :blog, + blog2.id, + mock_comment(~s(the )), + user + ) + + body = + mock_rich_text( + ~s(the ), + ~s(the ) + ) + + blog_attrs = blog_attrs |> Map.merge(%{body: body}) + {:ok, blog_x} = CMS.create_article(community, :blog, blog_attrs, user) + + body = mock_rich_text(~s(the )) + blog_attrs = blog_attrs |> Map.merge(%{body: body}) + {:ok, blog_y} = CMS.create_article(community, :blog, blog_attrs, user) + + CiteTasks.handle(blog_x) + CiteTasks.handle(comment) + CiteTasks.handle(blog_y) + + variables = %{content: "BLOG", id: blog2.id, filter: %{page: 1, size: 10}} + results = guest_conn |> query_result(@query, variables, "pagedCitingContents") + + assert results |> is_valid_pagination? + assert results["totalCount"] == 3 + end + end +end diff --git a/test/groupher_server_web/query/cms/citings/job_citing_test.exs b/test/groupher_server_web/query/cms/citings/job_citing_test.exs new file mode 100644 index 000000000..156615c17 --- /dev/null +++ b/test/groupher_server_web/query/cms/citings/job_citing_test.exs @@ -0,0 +1,83 @@ +defmodule GroupherServer.Test.Query.AbuseReports.JobCiting do + @moduledoc false + + use GroupherServer.TestTools + import Helper.Utils, only: [get_config: 2] + + alias GroupherServer.CMS + + alias CMS.Delegate.CiteTasks + + @site_host get_config(:general, :site_host) + + setup do + {:ok, job} = db_insert(:job) + {:ok, user} = db_insert(:user) + + {:ok, community} = db_insert(:community) + job_attrs = mock_attrs(:job, %{community_id: community.id}) + + guest_conn = simu_conn(:guest) + + {:ok, ~m(guest_conn community job job_attrs user)a} + end + + describe "[query paged_jobs filter pagination]" do + # id + @query """ + query($content: Content!, $id: ID!, $filter: PageFilter!) { + pagedCitingContents(id: $id, content: $content, filter: $filter) { + entries { + id + title + user { + login + nickname + avatar + } + commentId + } + totalPages + totalCount + pageSize + pageNumber + } + } + """ + @tag :wip + test "should get paged cittings", ~m(guest_conn community job_attrs user)a do + {:ok, job2} = db_insert(:job) + + {:ok, comment} = + CMS.create_comment( + :job, + job2.id, + mock_comment(~s(the )), + user + ) + + body = + mock_rich_text( + ~s(the ), + ~s(the ) + ) + + job_attrs = job_attrs |> Map.merge(%{body: body}) + {:ok, job_x} = CMS.create_article(community, :job, job_attrs, user) + + body = mock_rich_text(~s(the )) + job_attrs = job_attrs |> Map.merge(%{body: body}) + {:ok, job_y} = CMS.create_article(community, :job, job_attrs, user) + + CiteTasks.handle(job_x) + CiteTasks.handle(comment) + CiteTasks.handle(job_y) + + variables = %{content: "JOB", id: job2.id, filter: %{page: 1, size: 10}} + results = guest_conn |> query_result(@query, variables, "pagedCitingContents") + + assert results |> is_valid_pagination? + assert results["totalCount"] == 3 + end + end +end diff --git a/test/groupher_server_web/query/cms/citings/post_citing_test.exs b/test/groupher_server_web/query/cms/citings/post_citing_test.exs new file mode 100644 index 000000000..22bdf9f9f --- /dev/null +++ b/test/groupher_server_web/query/cms/citings/post_citing_test.exs @@ -0,0 +1,83 @@ +defmodule GroupherServer.Test.Query.AbuseReports.PostCiting do + @moduledoc false + + use GroupherServer.TestTools + import Helper.Utils, only: [get_config: 2] + + alias GroupherServer.CMS + + alias CMS.Delegate.CiteTasks + + @site_host get_config(:general, :site_host) + + setup do + {:ok, post} = db_insert(:post) + {:ok, user} = db_insert(:user) + + {:ok, community} = db_insert(:community) + post_attrs = mock_attrs(:post, %{community_id: community.id}) + + guest_conn = simu_conn(:guest) + + {:ok, ~m(guest_conn community post post_attrs user)a} + end + + describe "[query paged_posts filter pagination]" do + # id + @query """ + query($content: Content!, $id: ID!, $filter: PageFilter!) { + pagedCitingContents(id: $id, content: $content, filter: $filter) { + entries { + id + title + user { + login + nickname + avatar + } + commentId + } + totalPages + totalCount + pageSize + pageNumber + } + } + """ + @tag :wip + test "should get paged cittings", ~m(guest_conn community post_attrs user)a do + {:ok, post2} = db_insert(:post) + + {:ok, comment} = + CMS.create_comment( + :post, + post2.id, + mock_comment(~s(the )), + user + ) + + body = + mock_rich_text( + ~s(the ), + ~s(the ) + ) + + post_attrs = post_attrs |> Map.merge(%{body: body}) + {:ok, post_x} = CMS.create_article(community, :post, post_attrs, user) + + body = mock_rich_text(~s(the )) + post_attrs = post_attrs |> Map.merge(%{body: body}) + {:ok, post_y} = CMS.create_article(community, :post, post_attrs, user) + + CiteTasks.handle(post_x) + CiteTasks.handle(comment) + CiteTasks.handle(post_y) + + variables = %{content: "POST", id: post2.id, filter: %{page: 1, size: 10}} + results = guest_conn |> query_result(@query, variables, "pagedCitingContents") + + assert results |> is_valid_pagination? + assert results["totalCount"] == 3 + end + end +end