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

Commit 157ac79

Browse files
authored
fix(user): improve common article extract (#375)
* fix(user): add common user to common article * refactor: improve extract article method * refactor(article-reaction): remove support_thread concept * refactor(article-reaction): remove support_thread concept
1 parent 205f66d commit 157ac79

File tree

15 files changed

+86
-54
lines changed

15 files changed

+86
-54
lines changed

lib/groupher_server/accounts/collect_folder.ex

-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ defmodule GroupherServer.Accounts.CollectFolder do
1212
@required_fields ~w(user_id title)a
1313
@optional_fields ~w(index total_count private desc last_updated)a
1414

15-
@supported_threads [:post, :job, :repo]
16-
17-
def supported_threads, do: @supported_threads
18-
1915
@type t :: %CollectFolder{}
2016
schema "collect_folders" do
2117
belongs_to(:user, User, foreign_key: :user_id)

lib/groupher_server/accounts/delegates/collect_folder.ex

+10-4
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/publish.ex

+2-3
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@ defmodule GroupherServer.Accounts.Delegate.Publish do
7575
thread = thread |> to_string |> String.upcase()
7676
thread_atom = thread |> String.downcase() |> String.to_atom()
7777

78-
# article_preload = Keyword.new([{thread_atom, :author}])
79-
# query = from(comment in ArticleComment, preload: ^article_preload)
80-
query = from(comment in ArticleComment, preload: ^thread_atom)
78+
article_preload = Keyword.new([{thread_atom, [author: :user]}])
79+
query = from(comment in ArticleComment, preload: ^article_preload)
8180

8281
query
8382
|> join(:inner, [comment], author in assoc(comment, :author))

lib/groupher_server/accounts/delegates/upvoted_articles.ex

+10-5
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/groupher_server/accounts/embeds/collect_folder_meta.ex

+9-10
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ defmodule GroupherServer.Accounts.Embeds.CollectFolderMeta.Macros do
1010
field(:has_repo, :boolean, default: false)
1111
field(:repo_count, :integer, default: 0)
1212
"""
13-
alias GroupherServer.Accounts.CollectFolder
13+
import Helper.Utils, only: [get_config: 2]
1414

15-
@supported_threads CollectFolder.supported_threads()
15+
@article_threads get_config(:article, :article_threads)
1616

1717
defmacro threads_fields() do
18-
@supported_threads
18+
@article_threads
1919
|> Enum.map(fn thread ->
2020
quote do
2121
field(unquote(:"has_#{thread}"), :boolean, default: false)
@@ -27,21 +27,20 @@ end
2727

2828
defmodule GroupherServer.Accounts.Embeds.CollectFolderMeta do
2929
@moduledoc """
30-
general article meta info for article-like content, like @supported_threads
30+
general article meta info for articles
3131
"""
3232
use Ecto.Schema
3333
import Ecto.Changeset
3434
import GroupherServer.Accounts.Embeds.CollectFolderMeta.Macros
35+
import Helper.Utils, only: [get_config: 2]
3536

36-
alias GroupherServer.Accounts.CollectFolder
37+
@article_threads get_config(:article, :article_threads)
3738

38-
@supported_threads CollectFolder.supported_threads()
39-
40-
@optional_fields Enum.map(@supported_threads, &:"#{&1}_count") ++
41-
Enum.map(@supported_threads, &:"has_#{&1}")
39+
@optional_fields Enum.map(@article_threads, &:"#{&1}_count") ++
40+
Enum.map(@article_threads, &:"has_#{&1}")
4241

4342
def default_meta() do
44-
@supported_threads
43+
@article_threads
4544
|> Enum.reduce([], fn thread, acc -> acc ++ ["#{thread}_count": 0, "has_#{thread}": false] end)
4645
|> Enum.into(%{})
4746
end

lib/groupher_server_web/schema/Helper/fields.ex

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
99
@page_size get_config(:general, :page_size)
1010
@supported_emotions get_config(:article, :supported_emotions)
1111
@supported_comment_emotions get_config(:article, :comment_supported_emotions)
12-
@supported_collect_folder_threads Accounts.CollectFolder.supported_threads()
1312

1413
@article_threads get_config(:article, :article_threads)
1514

@@ -242,7 +241,7 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
242241
general collect folder meta info
243242
"""
244243
defmacro collect_folder_meta_fields() do
245-
@supported_collect_folder_threads
244+
@article_threads
246245
|> Enum.map(fn thread ->
247246
quote do
248247
field(unquote(:"has_#{thread}"), :boolean)

lib/groupher_server_web/schema/cms/cms_types.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
2727
field(:id, :id)
2828
# field(:body_html, :string)
2929
field(:title, :string)
30-
field(:author, :user, resolve: dataloader(CMS, :author))
30+
field(:author, :common_user)
3131
end
3232

3333
object :common_article_comment do

lib/helper/orm.ex

+11-5
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,9 @@ defmodule Helper.ORM do
295295
|> Repo.update()
296296
end
297297

298+
@doc """
299+
extract common article info and assign it to 'article' field
300+
"""
298301
def extract_and_assign_article(%{entries: entries} = paged_articles) do
299302
entries =
300303
Enum.map(entries, fn item ->
@@ -307,24 +310,27 @@ defmodule Helper.ORM do
307310

308311
@doc "extract common articles info"
309312
@spec extract_articles(T.paged_data(), [Atom.t()]) :: T.paged_article_common()
310-
def extract_articles(%{entries: entries} = paged_articles, supported_threads) do
313+
def extract_articles(%{entries: entries} = paged_articles, threads \\ @article_threads) do
311314
paged_articles
312-
|> Map.put(:entries, Enum.map(entries, &extract_article_info(&1, supported_threads)))
315+
|> Map.put(:entries, Enum.map(entries, &extract_article_info(&1, threads)))
313316
end
314317

315-
defp extract_article_info(reaction, supported_threads) do
316-
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))))
317320
article = Map.get(reaction, thread)
318321

319322
export_article_info(thread, article)
320323
end
321324

322325
defp export_article_info(thread, article) do
326+
author = article.author.user
327+
323328
%{
324329
thread: thread,
325330
id: article.id,
326331
title: article.title,
327-
upvotes_count: Map.get(article, :upvotes_count)
332+
upvotes_count: Map.get(article, :upvotes_count),
333+
author: author
328334
}
329335
end
330336
end

test/groupher_server/accounts/published/published_jobs_test.exs

-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ defmodule GroupherServer.Test.Accounts.Published.Job do
7272
end
7373

7474
describe "[publised job comments]" do
75-
@tag :wip2
7675
test "can get published article comments", ~m(job user)a do
7776
total_count = 10
7877

test/groupher_server/accounts/published/published_posts_test.exs

-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ defmodule GroupherServer.Test.Accounts.Published.Post do
7272
end
7373

7474
describe "[publised post comments]" do
75-
@tag :wip2
7675
test "can get published article comments", ~m(post user)a do
7776
total_count = 10
7877

test/groupher_server/accounts/published/published_repos_test.exs

-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ defmodule GroupherServer.Test.Accounts.Published.Repo do
7272
end
7373

7474
describe "[publised repo comments]" do
75-
@tag :wip2
7675
test "can get published article comments", ~m(repo user)a do
7776
total_count = 10
7877

test/groupher_server/accounts/reacted_articles_test.exs

+3-2
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

+13-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Jobs do
3434
}
3535
}
3636
"""
37-
@tag :wip2
37+
3838
test "can get published jobs", ~m(guest_conn community user)a do
3939
job_attrs = mock_attrs(:job, %{community_id: community.id})
4040

@@ -62,6 +62,10 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Jobs do
6262
article {
6363
id
6464
title
65+
author {
66+
nickname
67+
login
68+
}
6569
}
6670
}
6771
totalPages
@@ -71,7 +75,7 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Jobs do
7175
}
7276
}
7377
"""
74-
@tag :wip2
78+
7579
test "user can get paged published comments on job", ~m(guest_conn user job)a do
7680
pub_comments =
7781
Enum.reduce(1..@publish_count, [], fn _, acc ->
@@ -82,14 +86,18 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Jobs do
8286
random_comment_id = pub_comments |> Enum.random() |> Map.get(:id) |> to_string
8387

8488
variables = %{login: user.login, thread: "JOB", filter: %{page: 1, size: 20}}
89+
8590
results = guest_conn |> query_result(@query, variables, "pagedPublishedArticleComments")
8691

92+
entries = results["entries"]
8793
assert results |> is_valid_pagination?
8894
assert results["totalCount"] == @publish_count
8995

90-
assert results["entries"] |> Enum.all?(&(&1["article"]["id"] == to_string(job.id)))
91-
assert results["entries"] |> Enum.all?(&(&1["author"]["id"] == to_string(user.id)))
92-
assert results["entries"] |> Enum.any?(&(&1["id"] == random_comment_id))
96+
assert entries |> Enum.all?(&(not is_nil(&1["article"]["author"])))
97+
98+
assert entries |> Enum.all?(&(&1["article"]["id"] == to_string(job.id)))
99+
assert entries |> Enum.all?(&(&1["author"]["id"] == to_string(user.id)))
100+
assert entries |> Enum.any?(&(&1["id"] == random_comment_id))
93101
end
94102
end
95103
end

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

+13-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Posts do
3434
}
3535
}
3636
"""
37-
@tag :wip2
37+
3838
test "can get published posts", ~m(guest_conn community user)a do
3939
post_attrs = mock_attrs(:post, %{community_id: community.id})
4040

@@ -62,6 +62,10 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Posts do
6262
article {
6363
id
6464
title
65+
author {
66+
nickname
67+
login
68+
}
6569
}
6670
}
6771
totalPages
@@ -71,7 +75,7 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Posts do
7175
}
7276
}
7377
"""
74-
@tag :wip2
78+
7579
test "user can get paged published comments on post", ~m(guest_conn user post)a do
7680
pub_comments =
7781
Enum.reduce(1..@publish_count, [], fn _, acc ->
@@ -82,14 +86,18 @@ defmodule GroupherServer.Test.Query.Accounts.Published.Posts do
8286
random_comment_id = pub_comments |> Enum.random() |> Map.get(:id) |> to_string
8387

8488
variables = %{login: user.login, thread: "POST", filter: %{page: 1, size: 20}}
89+
8590
results = guest_conn |> query_result(@query, variables, "pagedPublishedArticleComments")
8691

92+
entries = results["entries"]
8793
assert results |> is_valid_pagination?
8894
assert results["totalCount"] == @publish_count
8995

90-
assert results["entries"] |> Enum.all?(&(&1["article"]["id"] == to_string(post.id)))
91-
assert results["entries"] |> Enum.all?(&(&1["author"]["id"] == to_string(user.id)))
92-
assert results["entries"] |> Enum.any?(&(&1["id"] == random_comment_id))
96+
assert entries |> Enum.all?(&(not is_nil(&1["article"]["author"])))
97+
98+
assert entries |> Enum.all?(&(&1["article"]["id"] == to_string(post.id)))
99+
assert entries |> Enum.all?(&(&1["author"]["id"] == to_string(user.id)))
100+
assert entries |> Enum.any?(&(&1["id"] == random_comment_id))
93101
end
94102
end
95103
end

0 commit comments

Comments
 (0)