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

Commit f6d4db1

Browse files
committed
refactor(cite-workflow): wip
1 parent 8e19455 commit f6d4db1

File tree

6 files changed

+122
-44
lines changed

6 files changed

+122
-44
lines changed

lib/groupher_server/cms/cms.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ defmodule GroupherServer.CMS do
9797
defdelegate sink_article(thread, id), to: ArticleCURD
9898
defdelegate undo_sink_article(thread, id), to: ArticleCURD
9999

100-
defdelegate paged_citing_contents(id, filter), to: CitedContent
100+
defdelegate paged_citing_contents(type, id, filter), to: CitedContent
101101

102102
defdelegate upvote_article(thread, article_id, user), to: ArticleUpvote
103103
defdelegate undo_upvote_article(thread, article_id, user), to: ArticleUpvote

lib/groupher_server/cms/delegates/cited_content.ex

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule GroupherServer.CMS.Delegate.CitedContent do
99

1010
alias Helper.Types, as: T
1111
alias GroupherServer.{CMS, Repo}
12-
alias Helper.ORM
12+
alias Helper.{ORM, QueryBuilder}
1313

1414
alias CMS.Model.CitedContent
1515

@@ -21,9 +21,10 @@ defmodule GroupherServer.CMS.Delegate.CitedContent do
2121
@cited_preloads @article_preloads ++ [[comment: :author] ++ @comment_article_preloads]
2222

2323
@doc "get paged citing contents"
24-
def paged_citing_contents(cited_by_id, %{page: page, size: size}) do
24+
def paged_citing_contents(cited_by_type, cited_by_id, %{page: page, size: size} = filter) do
2525
CitedContent
26-
|> where([c], c.cited_by_id == ^cited_by_id)
26+
|> where([c], c.cited_by_id == ^cited_by_id and c.cited_by_type == ^cited_by_type)
27+
|> QueryBuilder.filter_pack(Map.merge(filter, %{sort: :asc_inserted}))
2728
|> ORM.paginater(~m(page size)a)
2829
|> extract_contents
2930
|> done
@@ -40,7 +41,12 @@ defmodule GroupherServer.CMS.Delegate.CitedContent do
4041
# shape comment cite
4142
@spec shape_article(CitedContent.t()) :: T.cite_info()
4243
defp shape_article(%CitedContent{comment_id: comment_id} = cited) when not is_nil(comment_id) do
43-
%{block_linker: block_linker, cited_by_type: cited_by_type, comment: comment} = cited
44+
%{
45+
block_linker: block_linker,
46+
cited_by_type: cited_by_type,
47+
comment: comment,
48+
inserted_at: inserted_at
49+
} = cited
4450

4551
comment_thread = comment.thread |> String.downcase() |> String.to_atom()
4652
article = comment |> Map.get(comment_thread)
@@ -49,7 +55,7 @@ defmodule GroupherServer.CMS.Delegate.CitedContent do
4955
article
5056
|> Map.take([:id, :title])
5157
|> Map.merge(%{
52-
updated_at: comment.updated_at,
58+
inserted_at: inserted_at,
5359
user: user,
5460
thread: thread_to_atom(cited_by_type),
5561
comment_id: comment.id,
@@ -59,15 +65,20 @@ defmodule GroupherServer.CMS.Delegate.CitedContent do
5965

6066
# shape general article cite
6167
defp shape_article(%CitedContent{} = cited) do
62-
%{block_linker: block_linker, cited_by_type: cited_by_type} = cited
68+
%{block_linker: block_linker, cited_by_type: cited_by_type, inserted_at: inserted_at} = cited
6369

6470
thread = thread_to_atom(cited_by_type)
6571
article = Map.get(cited, thread)
6672

6773
user = get_in(article, [:author, :user]) |> Map.take([:login, :nickname, :avatar])
6874

6975
article
70-
|> Map.take([:id, :title, :updated_at])
71-
|> Map.merge(%{user: user, thread: thread, block_linker: block_linker})
76+
|> Map.take([:id, :title])
77+
|> Map.merge(%{
78+
user: user,
79+
thread: thread,
80+
block_linker: block_linker,
81+
inserted_at: inserted_at
82+
})
7283
end
7384
end

lib/helper/types.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ defmodule Helper.Types do
187187
id: Integer.t(),
188188
thread: article_thread,
189189
title: String.t(),
190-
updated_at: String.t(),
190+
inserted_at: String.t(),
191191
block_linker: [String.t()],
192192
comment_id: Integer.t() | nil,
193193
user: %{

test/groupher_server/cms/cite_contents/cite_blog_test.exs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ defmodule GroupherServer.Test.CMS.CiteContent.Blog do
173173
user
174174
)
175175

176+
Process.sleep(1000)
177+
176178
body =
177179
mock_rich_text(
178180
~s(the <a href=#{@site_host}/blog/#{blog2.id} />),
@@ -182,6 +184,7 @@ defmodule GroupherServer.Test.CMS.CiteContent.Blog do
182184
blog_attrs = blog_attrs |> Map.merge(%{body: body})
183185
{:ok, blog_x} = CMS.create_article(community, :blog, blog_attrs, user)
184186

187+
Process.sleep(1000)
185188
body = mock_rich_text(~s(the <a href=#{@site_host}/blog/#{blog2.id} />))
186189
blog_attrs = blog_attrs |> Map.merge(%{body: body})
187190
{:ok, blog_y} = CMS.create_article(community, :blog, blog_attrs, user)
@@ -190,25 +193,27 @@ defmodule GroupherServer.Test.CMS.CiteContent.Blog do
190193
CiteTasks.handle(comment)
191194
CiteTasks.handle(blog_y)
192195

193-
{:ok, result} = CMS.paged_citing_contents(blog2.id, %{page: 1, size: 10})
196+
{:ok, result} = CMS.paged_citing_contents("BLOG", blog2.id, %{page: 1, size: 10})
194197

195198
entries = result.entries
196-
first = entries |> List.first()
197-
middle = entries |> Enum.at(1)
198-
last = entries |> List.last()
199-
article_map_keys = [:block_linker, :id, :thread, :title, :updated_at, :user]
200-
201-
assert first.id == blog_x.id
202-
assert first.block_linker |> length == 2
203-
assert first |> Map.keys() == article_map_keys
204-
205-
assert middle.comment_id == comment.id
206-
assert middle.id == blog2.id
207-
assert middle.title == blog2.title
208-
209-
assert last.id == blog_y.id
210-
assert last.block_linker |> length == 1
211-
assert last |> Map.keys() == article_map_keys
199+
200+
result_comment = entries |> List.first()
201+
result_blog_x = entries |> Enum.at(1)
202+
result_blog_y = entries |> List.last()
203+
204+
article_map_keys = [:block_linker, :id, :inserted_at, :thread, :title, :user]
205+
206+
assert result_comment.comment_id == comment.id
207+
assert result_comment.id == blog2.id
208+
assert result_comment.title == blog2.title
209+
210+
assert result_blog_x.id == blog_x.id
211+
assert result_blog_x.block_linker |> length == 2
212+
assert result_blog_x |> Map.keys() == article_map_keys
213+
214+
assert result_blog_y.id == blog_y.id
215+
assert result_blog_y.block_linker |> length == 1
216+
assert result_blog_y |> Map.keys() == article_map_keys
212217

213218
assert result |> is_valid_pagination?(:raw)
214219
assert result.total_count == 3

test/groupher_server/cms/cite_contents/cite_job_test.exs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,61 @@ defmodule GroupherServer.Test.CMS.CiteContent.Job do
161161
assert job5.meta.citing_count == 1
162162
end
163163
end
164+
165+
describe "[cite pagi]" do
166+
@tag :wip
167+
test "can get paged cited articles.", ~m(user community job2 job_attrs)a do
168+
{:ok, comment} =
169+
CMS.create_comment(
170+
:job,
171+
job2.id,
172+
mock_comment(~s(the <a href=#{@site_host}/job/#{job2.id} />)),
173+
user
174+
)
175+
176+
Process.sleep(1000)
177+
178+
body =
179+
mock_rich_text(
180+
~s(the <a href=#{@site_host}/job/#{job2.id} />),
181+
~s(the <a href=#{@site_host}/job/#{job2.id} />)
182+
)
183+
184+
job_attrs = job_attrs |> Map.merge(%{body: body})
185+
{:ok, job_x} = CMS.create_article(community, :job, job_attrs, user)
186+
Process.sleep(1000)
187+
body = mock_rich_text(~s(the <a href=#{@site_host}/job/#{job2.id} />))
188+
job_attrs = job_attrs |> Map.merge(%{body: body})
189+
{:ok, job_y} = CMS.create_article(community, :job, job_attrs, user)
190+
191+
CiteTasks.handle(job_x)
192+
CiteTasks.handle(comment)
193+
CiteTasks.handle(job_y)
194+
195+
{:ok, result} = CMS.paged_citing_contents("JOB", job2.id, %{page: 1, size: 10})
196+
197+
entries = result.entries
198+
199+
result_comment = entries |> List.first()
200+
result_job_x = entries |> Enum.at(1)
201+
result_job_y = entries |> List.last()
202+
203+
article_map_keys = [:block_linker, :id, :inserted_at, :thread, :title, :user]
204+
205+
assert result_comment.comment_id == comment.id
206+
assert result_comment.id == job2.id
207+
assert result_comment.title == job2.title
208+
209+
assert result_job_x.id == job_x.id
210+
assert result_job_x.block_linker |> length == 2
211+
assert result_job_x |> Map.keys() == article_map_keys
212+
213+
assert result_job_y.id == job_y.id
214+
assert result_job_y.block_linker |> length == 1
215+
assert result_job_y |> Map.keys() == article_map_keys
216+
217+
assert result |> is_valid_pagination?(:raw)
218+
assert result.total_count == 3
219+
end
220+
end
164221
end

test/groupher_server/cms/cite_contents/cite_post_test.exs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ defmodule GroupherServer.Test.CMS.CiteContent.Post do
174174
user
175175
)
176176

177+
Process.sleep(1000)
178+
177179
body =
178180
mock_rich_text(
179181
~s(the <a href=#{@site_host}/post/#{post2.id} />),
@@ -183,6 +185,7 @@ defmodule GroupherServer.Test.CMS.CiteContent.Post do
183185
post_attrs = post_attrs |> Map.merge(%{body: body})
184186
{:ok, post_x} = CMS.create_article(community, :post, post_attrs, user)
185187

188+
Process.sleep(1000)
186189
body = mock_rich_text(~s(the <a href=#{@site_host}/post/#{post2.id} />))
187190
post_attrs = post_attrs |> Map.merge(%{body: body})
188191
{:ok, post_y} = CMS.create_article(community, :post, post_attrs, user)
@@ -191,25 +194,27 @@ defmodule GroupherServer.Test.CMS.CiteContent.Post do
191194
CiteTasks.handle(comment)
192195
CiteTasks.handle(post_y)
193196

194-
{:ok, result} = CMS.paged_citing_contents(post2.id, %{page: 1, size: 10})
197+
{:ok, result} = CMS.paged_citing_contents("POST", post2.id, %{page: 1, size: 10})
195198

196199
entries = result.entries
197-
first = entries |> List.first()
198-
middle = entries |> Enum.at(1)
199-
last = entries |> List.last()
200-
article_map_keys = [:block_linker, :id, :thread, :title, :updated_at, :user]
201-
202-
assert first.id == post_x.id
203-
assert first.block_linker |> length == 2
204-
assert first |> Map.keys() == article_map_keys
205-
206-
assert middle.comment_id == comment.id
207-
assert middle.id == post2.id
208-
assert middle.title == post2.title
209-
210-
assert last.id == post_y.id
211-
assert last.block_linker |> length == 1
212-
assert last |> Map.keys() == article_map_keys
200+
201+
result_comment = entries |> List.first()
202+
result_post_x = entries |> Enum.at(1)
203+
result_post_y = entries |> List.last()
204+
205+
article_map_keys = [:block_linker, :id, :inserted_at, :thread, :title, :user]
206+
207+
assert result_comment.comment_id == comment.id
208+
assert result_comment.id == post2.id
209+
assert result_comment.title == post2.title
210+
211+
assert result_post_x.id == post_x.id
212+
assert result_post_x.block_linker |> length == 2
213+
assert result_post_x |> Map.keys() == article_map_keys
214+
215+
assert result_post_y.id == post_y.id
216+
assert result_post_y.block_linker |> length == 1
217+
assert result_post_y |> Map.keys() == article_map_keys
213218

214219
assert result |> is_valid_pagination?(:raw)
215220
assert result.total_count == 3

0 commit comments

Comments
 (0)