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

Commit 8e19455

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

File tree

4 files changed

+155
-53
lines changed

4 files changed

+155
-53
lines changed

lib/groupher_server/cms/delegates/cited_content.ex

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,29 @@ defmodule GroupherServer.CMS.Delegate.CitedContent do
44
"""
55
import Ecto.Query, warn: false
66

7-
import GroupherServer.CMS.Helper.Matcher
87
import Helper.Utils, only: [done: 1, get_config: 2]
98
import ShortMaps
109

10+
alias Helper.Types, as: T
1111
alias GroupherServer.{CMS, Repo}
1212
alias Helper.ORM
1313

1414
alias CMS.Model.CitedContent
1515

1616
@article_threads get_config(:article, :threads)
17-
@cited_preloads @article_threads |> Enum.map(&Keyword.new([{&1, [author: :user]}]))
1817

19-
"""
20-
article:
21-
thread title timestamp who
22-
23-
comment:
24-
thread title/的评论(digest)中 timestamp who
25-
26-
%Article {
27-
thread: "",
28-
id: "",
29-
title: "",
30-
updatedAt: "",
31-
user: %User{},
32-
block_linker: [],
18+
@article_preloads @article_threads |> Enum.map(&Keyword.new([{&1, [author: :user]}]))
3319

34-
in_comment: boolean
35-
36-
user: %User{},
37-
}
38-
"""
20+
@comment_article_preloads @article_threads |> Enum.map(&Keyword.new([{:comment, &1}]))
21+
@cited_preloads @article_preloads ++ [[comment: :author] ++ @comment_article_preloads]
3922

4023
@doc "get paged citing contents"
41-
def paged_citing_contents(cited_by_id, %{page: page, size: size} = filter) do
24+
def paged_citing_contents(cited_by_id, %{page: page, size: size}) do
4225
CitedContent
4326
|> where([c], c.cited_by_id == ^cited_by_id)
4427
|> ORM.paginater(~m(page size)a)
4528
|> extract_contents
46-
|> IO.inspect(label: "bb")
29+
|> done
4730
end
4831

4932
def extract_contents(%{entries: entries} = paged_contents) do
@@ -52,17 +35,39 @@ defmodule GroupherServer.CMS.Delegate.CitedContent do
5235
Map.put(paged_contents, :entries, entries)
5336
end
5437

55-
def shape_article(%CitedContent{} = cited) do
56-
thread = cited.cited_by_type |> String.downcase() |> String.to_atom()
57-
# original_community
58-
block_linker = cited.block_linker
38+
defp thread_to_atom(thread), do: thread |> String.downcase() |> String.to_atom()
39+
40+
# shape comment cite
41+
@spec shape_article(CitedContent.t()) :: T.cite_info()
42+
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+
comment_thread = comment.thread |> String.downcase() |> String.to_atom()
46+
article = comment |> Map.get(comment_thread)
47+
user = comment.author |> Map.take([:login, :nickname, :avatar])
48+
49+
article
50+
|> Map.take([:id, :title])
51+
|> Map.merge(%{
52+
updated_at: comment.updated_at,
53+
user: user,
54+
thread: thread_to_atom(cited_by_type),
55+
comment_id: comment.id,
56+
block_linker: block_linker
57+
})
58+
end
59+
60+
# shape general article cite
61+
defp shape_article(%CitedContent{} = cited) do
62+
%{block_linker: block_linker, cited_by_type: cited_by_type} = cited
63+
64+
thread = thread_to_atom(cited_by_type)
5965
article = Map.get(cited, thread)
6066

61-
thread = get_in(article, [:meta]) |> Map.get(:thread)
6267
user = get_in(article, [:author, :user]) |> Map.take([:login, :nickname, :avatar])
6368

6469
article
65-
|> Map.take([:title, :updated_at])
70+
|> Map.take([:id, :title, :updated_at])
6671
|> Map.merge(%{user: user, thread: thread, block_linker: block_linker})
6772
end
6873
end

lib/helper/types.ex

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ defmodule Helper.Types do
3333
company: nil | String.t()
3434
}
3535

36-
@type article_thread :: :post | :job | :repo
36+
@type article_thread :: :post | :job | :repo | :blog
3737

3838
@type paged_filter :: %{
3939
page: Integer.t(),
@@ -182,4 +182,18 @@ defmodule Helper.Types do
182182
html fragment
183183
"""
184184
@type html :: String.t()
185+
186+
@type cite_info :: %{
187+
id: Integer.t(),
188+
thread: article_thread,
189+
title: String.t(),
190+
updated_at: String.t(),
191+
block_linker: [String.t()],
192+
comment_id: Integer.t() | nil,
193+
user: %{
194+
login: String.t(),
195+
avatar: String.t(),
196+
nickname: String.t()
197+
}
198+
}
185199
end

test/groupher_server/cms/cite_contents/cite_blog_test.exs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,57 @@ defmodule GroupherServer.Test.CMS.CiteContent.Blog do
161161
assert blog5.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 blog2 blog_attrs)a do
168+
{:ok, comment} =
169+
CMS.create_comment(
170+
:blog,
171+
blog2.id,
172+
mock_comment(~s(the <a href=#{@site_host}/blog/#{blog2.id} />)),
173+
user
174+
)
175+
176+
body =
177+
mock_rich_text(
178+
~s(the <a href=#{@site_host}/blog/#{blog2.id} />),
179+
~s(the <a href=#{@site_host}/blog/#{blog2.id} />)
180+
)
181+
182+
blog_attrs = blog_attrs |> Map.merge(%{body: body})
183+
{:ok, blog_x} = CMS.create_article(community, :blog, blog_attrs, user)
184+
185+
body = mock_rich_text(~s(the <a href=#{@site_host}/blog/#{blog2.id} />))
186+
blog_attrs = blog_attrs |> Map.merge(%{body: body})
187+
{:ok, blog_y} = CMS.create_article(community, :blog, blog_attrs, user)
188+
189+
CiteTasks.handle(blog_x)
190+
CiteTasks.handle(comment)
191+
CiteTasks.handle(blog_y)
192+
193+
{:ok, result} = CMS.paged_citing_contents(blog2.id, %{page: 1, size: 10})
194+
195+
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
212+
213+
assert result |> is_valid_pagination?(:raw)
214+
assert result.total_count == 3
215+
end
216+
end
164217
end

test/groupher_server/cms/cite_contents/cite_post_test.exs

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,6 @@ defmodule GroupherServer.Test.CMS.CiteContent.Post do
2727
{:ok, ~m(user user2 community post post2 post3 post4 post5 post_attrs)a}
2828
end
2929

30-
describe "[cite pagi]" do
31-
@tag :wip
32-
test "--can get paged cited articles.", ~m(user community post2 post_attrs)a do
33-
body =
34-
mock_rich_text(
35-
~s(the <a href=#{@site_host}/post/#{post2.id} />),
36-
~s(the <a href=#{@site_host}/post/#{post2.id} />)
37-
)
38-
39-
post_attrs = post_attrs |> Map.merge(%{body: body})
40-
{:ok, post_x} = CMS.create_article(community, :post, post_attrs, user)
41-
42-
body = mock_rich_text(~s(the <a href=#{@site_host}/post/#{post2.id} />))
43-
post_attrs = post_attrs |> Map.merge(%{body: body})
44-
{:ok, post_y} = CMS.create_article(community, :post, post_attrs, user)
45-
46-
CiteTasks.handle(post_x)
47-
CiteTasks.handle(post_y)
48-
49-
CMS.paged_citing_contents(post2.id, %{page: 1, size: 10})
50-
end
51-
end
52-
5330
describe "[cite basic]" do
5431
#
5532
test "cited multi post should work", ~m(user community post2 post3 post4 post5 post_attrs)a do
@@ -185,4 +162,57 @@ defmodule GroupherServer.Test.CMS.CiteContent.Post do
185162
assert post5.meta.citing_count == 1
186163
end
187164
end
165+
166+
describe "[cite pagi]" do
167+
@tag :wip
168+
test "can get paged cited articles.", ~m(user community post2 post_attrs)a do
169+
{:ok, comment} =
170+
CMS.create_comment(
171+
:post,
172+
post2.id,
173+
mock_comment(~s(the <a href=#{@site_host}/post/#{post2.id} />)),
174+
user
175+
)
176+
177+
body =
178+
mock_rich_text(
179+
~s(the <a href=#{@site_host}/post/#{post2.id} />),
180+
~s(the <a href=#{@site_host}/post/#{post2.id} />)
181+
)
182+
183+
post_attrs = post_attrs |> Map.merge(%{body: body})
184+
{:ok, post_x} = CMS.create_article(community, :post, post_attrs, user)
185+
186+
body = mock_rich_text(~s(the <a href=#{@site_host}/post/#{post2.id} />))
187+
post_attrs = post_attrs |> Map.merge(%{body: body})
188+
{:ok, post_y} = CMS.create_article(community, :post, post_attrs, user)
189+
190+
CiteTasks.handle(post_x)
191+
CiteTasks.handle(comment)
192+
CiteTasks.handle(post_y)
193+
194+
{:ok, result} = CMS.paged_citing_contents(post2.id, %{page: 1, size: 10})
195+
196+
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
213+
214+
assert result |> is_valid_pagination?(:raw)
215+
assert result.total_count == 3
216+
end
217+
end
188218
end

0 commit comments

Comments
 (0)