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

Commit d5a0672

Browse files
committed
refactor(cite-task): fix citing self edge-case
1 parent fa6efa1 commit d5a0672

File tree

4 files changed

+74
-21
lines changed

4 files changed

+74
-21
lines changed

lib/groupher_server/cms/delegates/cite_tasks.ex

+19-21
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ defmodule GroupherServer.CMS.Delegate.CiteTasks do
7676
end
7777
end
7878

79-
# defp batch_done
80-
79+
# batch insert CitedContent record and update citing count
8180
defp update_cited_info(cited_contents) do
8281
# see: https://github.com/elixir-ecto/ecto/issues/1932#issuecomment-314083252
8382
clean_cited_contents =
@@ -86,10 +85,9 @@ defmodule GroupherServer.CMS.Delegate.CiteTasks do
8685
|> Enum.map(&Map.delete(&1, :cited_content))
8786
|> Enum.map(&Map.delete(&1, :citing_time))
8887

89-
with true <- {0, nil} !== Repo.insert_all(CitedContent, clean_cited_contents) do
90-
update_citing_count(cited_contents)
91-
else
92-
_ -> {:error, "insert cited content error"}
88+
case {0, nil} !== Repo.insert_all(CitedContent, clean_cited_contents) do
89+
true -> update_citing_count(cited_contents)
90+
false -> {:error, "insert cited content error"}
9391
end
9492
end
9593

@@ -166,31 +164,31 @@ defmodule GroupherServer.CMS.Delegate.CiteTasks do
166164
do_parse_cited_info(content, block_id, links)
167165
end
168166

169-
defp do_parse_cited_info(%Comment{} = comment, block_id, links) do
170-
# IO.inspect(links, label: "links -> ")
171-
172-
Enum.reduce(links, [], fn link, acc ->
173-
case parse_cited(link) do
174-
{:ok, cited} -> List.insert_at(acc, 0, shape_cited(comment, cited, block_id))
175-
_ -> acc
176-
end
177-
end)
178-
|> Enum.uniq()
179-
end
180-
181167
# links Floki parsed fmt
168+
# content means both article and comment
182169
# e.g:
183170
# [{"a", [{"href", "https://coderplanets.com/post/195675"}], []},]
184-
defp do_parse_cited_info(article, block_id, links) do
171+
defp do_parse_cited_info(content, block_id, links) do
185172
Enum.reduce(links, [], fn link, acc ->
186-
case parse_cited(link) do
187-
{:ok, cited} -> List.insert_at(acc, 0, shape_cited(article, cited, block_id))
173+
case parse_valid_cited(content.id, link) do
174+
{:ok, cited} -> List.insert_at(acc, 0, shape_cited(content, cited, block_id))
188175
_ -> acc
189176
end
190177
end)
191178
|> Enum.uniq()
192179
end
193180

181+
# parse cited with check if citing link is point to itself
182+
defp parse_valid_cited(content_id, link) do
183+
with {:ok, cited} <- parse_cited(link),
184+
%{content: content} <- cited do
185+
case content.id !== content_id do
186+
true -> {:ok, cited}
187+
false -> {:error, "citing itself"}
188+
end
189+
end
190+
end
191+
194192
# cite article in comment
195193
# 在评论中引用文章
196194
defp shape_cited(%Comment{} = comment, %{type: :article, content: cited}, block_id) do

test/groupher_server/cms/cite_contents/cite_blog_test.exs

+18
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,24 @@ defmodule GroupherServer.Test.CMS.CiteContent.Blog do
7373
assert blog.meta.citing_count == 0
7474
end
7575

76+
@tag :wip
77+
test "cited comment itself should not work", ~m(user blog)a do
78+
{:ok, cited_comment} = CMS.create_comment(:blog, blog.id, mock_rich_text("hello"), user)
79+
80+
{:ok, comment} =
81+
CMS.update_comment(
82+
cited_comment,
83+
mock_comment(
84+
~s(the <a href=#{@site_host}/blog/#{blog.id}?comment_id=#{cited_comment.id} />)
85+
)
86+
)
87+
88+
CiteTasks.handle(comment)
89+
90+
{:ok, cited_comment} = ORM.find(Comment, cited_comment.id)
91+
assert cited_comment.meta.citing_count == 0
92+
end
93+
7694
@tag :wip
7795
test "can cite blog's comment in blog", ~m(community user blog blog2 blog_attrs)a do
7896
{:ok, comment} = CMS.create_comment(:blog, blog.id, mock_rich_text("hello"), user)

test/groupher_server/cms/cite_contents/cite_job_test.exs

+18
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,24 @@ defmodule GroupherServer.Test.CMS.CiteContent.Job do
7373
assert job.meta.citing_count == 0
7474
end
7575

76+
@tag :wip
77+
test "cited comment itself should not work", ~m(user job)a do
78+
{:ok, cited_comment} = CMS.create_comment(:job, job.id, mock_rich_text("hello"), user)
79+
80+
{:ok, comment} =
81+
CMS.update_comment(
82+
cited_comment,
83+
mock_comment(
84+
~s(the <a href=#{@site_host}/job/#{job.id}?comment_id=#{cited_comment.id} />)
85+
)
86+
)
87+
88+
CiteTasks.handle(comment)
89+
90+
{:ok, cited_comment} = ORM.find(Comment, cited_comment.id)
91+
assert cited_comment.meta.citing_count == 0
92+
end
93+
7694
@tag :wip
7795
test "can cite job's comment in job", ~m(community user job job2 job_attrs)a do
7896
{:ok, comment} = CMS.create_comment(:job, job.id, mock_rich_text("hello"), user)

test/groupher_server/cms/cite_contents/cite_post_test.exs

+19
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ defmodule GroupherServer.Test.CMS.CiteContent.Post do
6262
assert post5.meta.citing_count == 1
6363
end
6464

65+
@tag :wip
6566
test "cited post itself should not work", ~m(user community post_attrs)a do
6667
{:ok, post} = CMS.create_article(community, :post, post_attrs, user)
6768

@@ -74,6 +75,24 @@ defmodule GroupherServer.Test.CMS.CiteContent.Post do
7475
assert post.meta.citing_count == 0
7576
end
7677

78+
@tag :wip
79+
test "cited comment itself should not work", ~m(user post)a do
80+
{:ok, cited_comment} = CMS.create_comment(:post, post.id, mock_rich_text("hello"), user)
81+
82+
{:ok, comment} =
83+
CMS.update_comment(
84+
cited_comment,
85+
mock_comment(
86+
~s(the <a href=#{@site_host}/post/#{post.id}?comment_id=#{cited_comment.id} />)
87+
)
88+
)
89+
90+
CiteTasks.handle(comment)
91+
92+
{:ok, cited_comment} = ORM.find(Comment, cited_comment.id)
93+
assert cited_comment.meta.citing_count == 0
94+
end
95+
7796
@tag :wip
7897
test "can cite post's comment in post", ~m(community user post post2 post_attrs)a do
7998
{:ok, comment} = CMS.create_comment(:post, post.id, mock_rich_text("hello"), user)

0 commit comments

Comments
 (0)