Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commitd5a0672

Browse files
committed
refactor(cite-task): fix citing self edge-case
1 parentfa6efa1 commitd5a0672

File tree

4 files changed

+74
-21
lines changed

4 files changed

+74
-21
lines changed

‎lib/groupher_server/cms/delegates/cite_tasks.ex‎

Lines changed: 19 additions & 21 deletions
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
defpupdate_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-
withtrue<-{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-
defpdo_parse_cited_info(%Comment{}=comment,block_id,links)do
170-
# IO.inspect(links, label: "links -> ")
171-
172-
Enum.reduce(links,[],fnlink,acc->
173-
caseparse_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-
defpdo_parse_cited_info(article,block_id,links)do
171+
defpdo_parse_cited_info(content,block_id,links)do
185172
Enum.reduce(links,[],fnlink,acc->
186-
caseparse_cited(link)do
187-
{:ok,cited}->List.insert_at(acc,0,shape_cited(article,cited,block_id))
173+
caseparse_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+
defpparse_valid_cited(content_id,link)do
183+
with{:ok,cited}<-parse_cited(link),
184+
%{content:content}<-citeddo
185+
casecontent.id!==content_iddo
186+
true->{:ok,cited}
187+
false->{:error,"citing itself"}
188+
end
189+
end
190+
end
191+
194192
# cite article in comment
195193
# 在评论中引用文章
196194
defpshape_cited(%Comment{}=comment,%{type::article,content:cited},block_id)do

‎test/groupher_server/cms/cite_contents/cite_blog_test.exs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,24 @@ defmodule GroupherServer.Test.CMS.CiteContent.Blog do
7373
assertblog.meta.citing_count==0
7474
end
7575

76+
@tag:wip
77+
test"cited comment itself should not work",~m(user blog)ado
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+
assertcited_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)ado
7896
{:ok,comment}=CMS.create_comment(:blog,blog.id,mock_rich_text("hello"),user)

‎test/groupher_server/cms/cite_contents/cite_job_test.exs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,24 @@ defmodule GroupherServer.Test.CMS.CiteContent.Job do
7373
assertjob.meta.citing_count==0
7474
end
7575

76+
@tag:wip
77+
test"cited comment itself should not work",~m(user job)ado
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+
assertcited_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)ado
7896
{:ok,comment}=CMS.create_comment(:job,job.id,mock_rich_text("hello"),user)

‎test/groupher_server/cms/cite_contents/cite_post_test.exs‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ defmodule GroupherServer.Test.CMS.CiteContent.Post do
6262
assertpost5.meta.citing_count==1
6363
end
6464

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

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

78+
@tag:wip
79+
test"cited comment itself should not work",~m(user post)ado
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+
assertcited_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)ado
7998
{:ok,comment}=CMS.create_comment(:post,post.id,mock_rich_text("hello"),user)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp