Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 380330d

Browse files
authored
refactor(cite-workflow): cross cite fix & test (#403)
1 parent d4a95a7 commit 380330d

File tree

5 files changed

+74
-14
lines changed

5 files changed

+74
-14
lines changed

lib/groupher_server/cms/delegates/cited_content.ex

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,38 +38,32 @@ defmodule GroupherServer.CMS.Delegate.CitedContent do
3838
Map.put(paged_contents, :entries, entries)
3939
end
4040

41-
defp thread_to_atom(thread), do: thread |> String.downcase() |> String.to_atom()
42-
4341
# shape comment cite
4442
@spec shape_article(CitedContent.t()) :: T.cite_info()
4543
defp shape_article(%CitedContent{comment_id: comment_id} = cited) when not is_nil(comment_id) do
46-
%{
47-
block_linker: block_linker,
48-
cited_by_type: cited_by_type,
49-
comment: comment,
50-
inserted_at: inserted_at
51-
} = cited
44+
%{block_linker: block_linker, comment: comment, inserted_at: inserted_at} = cited
5245

5346
comment_thread = comment.thread |> String.downcase() |> String.to_atom()
5447
article = comment |> Map.get(comment_thread)
48+
article_thread = thread_to_atom(article.meta.thread)
5549
user = comment.author |> Map.take([:login, :nickname, :avatar])
5650

5751
article
5852
|> Map.take([:id, :title])
5953
|> Map.merge(%{
6054
inserted_at: inserted_at,
6155
user: user,
62-
thread: thread_to_atom(cited_by_type),
56+
thread: article_thread,
6357
comment_id: comment.id,
6458
block_linker: block_linker
6559
})
6660
end
6761

6862
# shape general article cite
6963
defp shape_article(%CitedContent{} = cited) do
70-
%{block_linker: block_linker, cited_by_type: cited_by_type, inserted_at: inserted_at} = cited
64+
%{block_linker: block_linker, inserted_at: inserted_at} = cited
7165

72-
thread = thread_to_atom(cited_by_type)
66+
thread = citing_thread(cited)
7367
article = Map.get(cited, thread)
7468

7569
user = get_in(article, [:author, :user]) |> Map.take([:login, :nickname, :avatar])
@@ -83,4 +77,12 @@ defmodule GroupherServer.CMS.Delegate.CitedContent do
8377
inserted_at: inserted_at
8478
})
8579
end
80+
81+
# find thread_id that not empty
82+
# only used for shape_article
83+
defp citing_thread(cited) do
84+
@article_threads |> Enum.find(fn thread -> not is_nil(Map.get(cited, :"#{thread}_id")) end)
85+
end
86+
87+
defp thread_to_atom(thread), do: thread |> String.downcase() |> String.to_atom()
8688
end

test/groupher_server/cms/cite_contents/cite_post_test.exs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,62 @@ defmodule GroupherServer.Test.CMS.CiteContent.Post do
218218
assert result.total_count == 3
219219
end
220220
end
221+
222+
describe "[cross cite]" do
223+
@tag :wip
224+
test "can citing multi type thread and comment in one time", ~m(user community post2)a do
225+
post_attrs = mock_attrs(:post, %{community_id: community.id})
226+
job_attrs = mock_attrs(:job, %{community_id: community.id})
227+
blog_attrs = mock_attrs(:blog, %{community_id: community.id})
228+
229+
body = mock_rich_text(~s(the <a href=#{@site_host}/post/#{post2.id} />))
230+
231+
{:ok, post} =
232+
CMS.create_article(community, :post, Map.merge(post_attrs, %{body: body}), user)
233+
234+
CiteTasks.handle(post)
235+
236+
Process.sleep(1000)
237+
238+
{:ok, job} = CMS.create_article(community, :job, Map.merge(job_attrs, %{body: body}), user)
239+
CiteTasks.handle(job)
240+
241+
Process.sleep(1000)
242+
243+
comment_body = mock_comment(~s(the <a href=#{@site_host}/post/#{post2.id} />))
244+
{:ok, comment} = CMS.create_comment(:job, job.id, comment_body, user)
245+
246+
CiteTasks.handle(comment)
247+
248+
Process.sleep(1000)
249+
250+
{:ok, blog} =
251+
CMS.create_article(community, :blog, Map.merge(blog_attrs, %{body: body}), user)
252+
253+
CiteTasks.handle(blog)
254+
255+
{:ok, result} = CMS.paged_citing_contents("POST", post2.id, %{page: 1, size: 10})
256+
# IO.inspect(result, label: "the result")
257+
258+
result.total_count == 4
259+
260+
result_post = result.entries |> List.first()
261+
result_job = result.entries |> Enum.at(1)
262+
result_comment = result.entries |> Enum.at(2)
263+
result_blog = result.entries |> List.last()
264+
265+
assert result_post.id == post.id
266+
assert result_post.thread == :post
267+
268+
assert result_job.id == job.id
269+
assert result_job.thread == :job
270+
271+
assert result_comment.id == job.id
272+
assert result_comment.thread == :job
273+
assert result_comment.comment_id == comment.id
274+
275+
assert result_blog.id == blog.id
276+
assert result_blog.thread == :blog
277+
end
278+
end
221279
end

test/groupher_server_web/query/cms/citings/blog_citing_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ defmodule GroupherServer.Test.Query.AbuseReports.BlogCiting do
4444
}
4545
}
4646
"""
47-
@tag :wip
47+
4848
test "should get paged cittings", ~m(guest_conn community blog_attrs user)a do
4949
{:ok, blog2} = db_insert(:blog)
5050

test/groupher_server_web/query/cms/citings/job_citing_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ defmodule GroupherServer.Test.Query.AbuseReports.JobCiting do
4444
}
4545
}
4646
"""
47-
@tag :wip
47+
4848
test "should get paged cittings", ~m(guest_conn community job_attrs user)a do
4949
{:ok, job2} = db_insert(:job)
5050

test/groupher_server_web/query/cms/citings/post_citing_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ defmodule GroupherServer.Test.Query.AbuseReports.PostCiting do
4444
}
4545
}
4646
"""
47-
@tag :wip
47+
4848
test "should get paged cittings", ~m(guest_conn community post_attrs user)a do
4949
{:ok, post2} = db_insert(:post)
5050

0 commit comments

Comments
 (0)