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

Commit a424b36

Browse files
committed
refactor: improve extract article method
1 parent 290ee65 commit a424b36

File tree

7 files changed

+30
-18
lines changed

7 files changed

+30
-18
lines changed

lib/groupher_server/accounts/delegates/collect_folder.ex

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule GroupherServer.Accounts.Delegate.CollectFolder do
99
alias Helper.QueryBuilder
1010

1111
import Helper.ErrorCode
12-
import Helper.Utils, only: [done: 1]
12+
import Helper.Utils, only: [done: 1, get_config: 2]
1313

1414
import ShortMaps
1515

@@ -24,7 +24,7 @@ defmodule GroupherServer.Accounts.Delegate.CollectFolder do
2424
# @max_article_count_per_collect_folder 300
2525

2626
@default_meta Embeds.CollectFolderMeta.default_meta()
27-
@supported_collect_threads [:post, :job]
27+
@article_threads get_config(:article, :article_threads)
2828

2929
@doc """
3030
list a user's not-private collect folders
@@ -75,9 +75,15 @@ defmodule GroupherServer.Accounts.Delegate.CollectFolder do
7575
end
7676

7777
defp do_paged_collect_folder_articles(folder, filter) do
78-
Repo.preload(folder.collects, @supported_collect_threads)
78+
article_preload =
79+
@article_threads
80+
|> Enum.reduce([], fn thread, acc ->
81+
acc ++ Keyword.new([{thread, [author: :user]}])
82+
end)
83+
84+
Repo.preload(folder.collects, article_preload)
7985
|> ORM.embeds_paginater(filter)
80-
|> ORM.extract_articles(@supported_collect_threads)
86+
|> ORM.extract_articles()
8187
|> done()
8288
end
8389

lib/groupher_server/accounts/delegates/upvoted_articles.ex

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@ defmodule GroupherServer.Accounts.Delegate.UpvotedArticles do
33
get contents(posts, jobs ...) that user upvotes
44
"""
55
import Ecto.Query, warn: false
6-
import Helper.Utils, only: [done: 1]
6+
import Helper.Utils, only: [done: 1, get_config: 2]
77
import ShortMaps
88

99
alias Helper.{ORM, QueryBuilder}
1010

1111
alias GroupherServer.CMS
1212
alias CMS.{ArticleUpvote}
1313

14-
# TODO: move to Model
15-
@supported_uovoted_threads [:post, :job]
14+
@article_threads get_config(:article, :article_threads)
1615

1716
@doc """
1817
get paged upvoted articles
@@ -31,13 +30,19 @@ defmodule GroupherServer.Accounts.Delegate.UpvotedArticles do
3130
end
3231

3332
defp load_upvoted_articles(where_query, %{page: page, size: size} = filter) do
34-
query = from(a in ArticleUpvote, preload: ^@supported_uovoted_threads)
33+
article_preload =
34+
@article_threads
35+
|> Enum.reduce([], fn thread, acc ->
36+
acc ++ Keyword.new([{thread, [author: :user]}])
37+
end)
38+
39+
query = from(a in ArticleUpvote, preload: ^article_preload)
3540

3641
query
3742
|> where(^where_query)
3843
|> QueryBuilder.filter_pack(filter)
3944
|> ORM.paginater(~m(page size)a)
40-
|> ORM.extract_articles(@supported_uovoted_threads)
45+
|> ORM.extract_articles()
4146
|> done()
4247
end
4348
end

lib/helper/orm.ex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,13 +310,13 @@ defmodule Helper.ORM do
310310

311311
@doc "extract common articles info"
312312
@spec extract_articles(T.paged_data(), [Atom.t()]) :: T.paged_article_common()
313-
def extract_articles(%{entries: entries} = paged_articles, supported_threads) do
313+
def extract_articles(%{entries: entries} = paged_articles, threads \\ @article_threads) do
314314
paged_articles
315-
|> Map.put(:entries, Enum.map(entries, &extract_article_info(&1, supported_threads)))
315+
|> Map.put(:entries, Enum.map(entries, &extract_article_info(&1, threads)))
316316
end
317317

318-
defp extract_article_info(reaction, supported_threads) do
319-
thread = Enum.find(supported_threads, &(not is_nil(Map.get(reaction, &1))))
318+
defp extract_article_info(reaction, threads) do
319+
thread = Enum.find(threads, &(not is_nil(Map.get(reaction, &1))))
320320
article = Map.get(reaction, thread)
321321

322322
export_article_info(thread, article)

test/groupher_server/accounts/reacted_articles_test.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ defmodule GroupherServer.Test.Accounts.ReactedContents do
1414
end
1515

1616
describe "[user upvoted articles]" do
17+
@tag :wip2
1718
test "user can get paged upvoted common articles", ~m(user post job)a do
1819
{:ok, _} = CMS.upvote_article(:post, post.id, user)
1920
{:ok, _} = CMS.upvote_article(:job, job.id, user)
@@ -28,8 +29,8 @@ defmodule GroupherServer.Test.Accounts.ReactedContents do
2829
assert job.id == article_job |> Map.get(:id)
2930
assert post.id == article_post |> Map.get(:id)
3031

31-
assert [:id, :thread, :title, :upvotes_count] == article_post |> Map.keys()
32-
assert [:id, :thread, :title, :upvotes_count] == article_job |> Map.keys()
32+
assert [:author, :id, :thread, :title, :upvotes_count] == article_post |> Map.keys()
33+
assert [:author, :id, :thread, :title, :upvotes_count] == article_job |> Map.keys()
3334
end
3435

3536
test "user can get paged upvoted posts by thread filter", ~m(user post job)a do

test/groupher_server_web/query/accounts/published/published_jobs_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Jobs do
7575
}
7676
}
7777
"""
78-
@tag :wip2
78+
7979
test "user can get paged published comments on job", ~m(guest_conn user job)a do
8080
pub_comments =
8181
Enum.reduce(1..@publish_count, [], fn _, acc ->

test/groupher_server_web/query/accounts/published/published_posts_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Posts do
7575
}
7676
}
7777
"""
78-
@tag :wip2
78+
7979
test "user can get paged published comments on post", ~m(guest_conn user post)a do
8080
pub_comments =
8181
Enum.reduce(1..@publish_count, [], fn _, acc ->

test/groupher_server_web/query/accounts/published/published_repos_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Repos do
7575
}
7676
}
7777
"""
78-
@tag :wip2
78+
7979
test "user can get paged published comments on repo", ~m(guest_conn user repo)a do
8080
pub_comments =
8181
Enum.reduce(1..@publish_count, [], fn _, acc ->

0 commit comments

Comments
 (0)