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

Commit 018d6a4

Browse files
committed
refactor(cited-article): wip
1 parent e8384e7 commit 018d6a4

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

lib/groupher_server/cms/delegates/block_tasks.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,12 @@ defmodule GroupherServer.CMS.Delegate.BlockTasks do
158158
links_in_block = Floki.find(text, "a[href]")
159159

160160
Enum.reduce(links_in_block, [], fn link, acc ->
161-
with {:ok, cited_article} <- parse_cited_article(link) do
161+
with {:ok, cited_article} <- parse_cited_article(link),
162+
# do not cite artilce itself
163+
true <- article.id !== cited_article.id do
162164
List.insert_at(acc, 0, shape_cited_content(article, cited_article, block_id))
163165
else
164-
{:error, _} -> acc
166+
_ -> acc
165167
end
166168
end)
167169
|> Enum.uniq()

lib/helper/orm.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ defmodule Helper.ORM do
177177
delete all queryable if exist
178178
"""
179179
def delete_all(queryable, :if_exist) do
180-
case Repo.exists?(query) do
181-
true -> {:ok, Repo.delete_all(query)}
180+
case Repo.exists?(queryable) do
181+
true -> {:ok, Repo.delete_all(queryable)}
182182
false -> {:ok, :pass}
183183
end
184184
end

test/groupher_server/cms/cite_content_test.exs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ defmodule GroupherServer.Test.CMS.CiteContent do
55

66
alias Helper.ORM
77
alias GroupherServer.CMS
8-
alias Helper.Converter.{EditorToHTML, HtmlSanitizer, Converter}
98

10-
alias EditorToHTML.{Class, Validator}
11-
alias CMS.Model.{Author, Community, Post}
9+
alias CMS.Model.Post
1210

1311
alias CMS.Delegate.BlockTasks
1412

@@ -31,8 +29,8 @@ defmodule GroupherServer.Test.CMS.CiteContent do
3129
end
3230

3331
describe "[cite basic]" do
34-
@tag :wip
35-
test "basic", ~m(user community post2 post3 post4 post5 post_attrs)a do
32+
# @tag :wip
33+
test "cited multi post should work", ~m(user community post2 post3 post4 post5 post_attrs)a do
3634
body =
3735
mock_rich_text(
3836
~s(the <a href=#{@site_host}/post/#{post2.id} /> and <a href=#{@site_host}/post/#{
@@ -45,17 +43,37 @@ defmodule GroupherServer.Test.CMS.CiteContent do
4543
)
4644

4745
post_attrs = post_attrs |> Map.merge(%{body: body})
48-
4946
{:ok, post} = CMS.create_article(community, :post, post_attrs, user)
47+
48+
body = mock_rich_text(~s(the <a href=#{@site_host}/post/#{post3.id} />))
49+
post_attrs = post_attrs |> Map.merge(%{body: body})
50+
{:ok, post_n} = CMS.create_article(community, :post, post_attrs, user)
51+
5052
BlockTasks.handle(post)
53+
BlockTasks.handle(post_n)
5154

5255
{:ok, post2} = ORM.find(Post, post2.id)
5356
{:ok, post3} = ORM.find(Post, post3.id)
5457
{:ok, post4} = ORM.find(Post, post4.id)
58+
{:ok, post5} = ORM.find(Post, post5.id)
5559

5660
assert post2.meta.citing_count == 1
57-
assert post3.meta.citing_count == 1
61+
assert post3.meta.citing_count == 2
5862
assert post4.meta.citing_count == 1
63+
assert post5.meta.citing_count == 1
64+
end
65+
66+
@tag :wip
67+
test "cited post itself should not work", ~m(user community post post_attrs)a do
68+
{:ok, post} = CMS.create_article(community, :post, post_attrs, user)
69+
70+
body = mock_rich_text(~s(the <a href=#{@site_host}/post/#{post.id} />))
71+
{:ok, post} = CMS.update_article(post, %{body: body})
72+
73+
BlockTasks.handle(post)
74+
75+
{:ok, post} = ORM.find(Post, post.id)
76+
assert post.meta.citing_count == 0
5977
end
6078
end
6179
end

0 commit comments

Comments
 (0)