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

Commit 4be6356

Browse files
committed
refactor(article-emotions): wip
1 parent 7ce9f06 commit 4be6356

File tree

3 files changed

+170
-143
lines changed

3 files changed

+170
-143
lines changed

lib/groupher_server/cms/delegates/article_comment.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
215215
# |> QueryBuilder.filter_pack(Map.merge(filters, %{sort: :asc_inserted}))
216216
|> QueryBuilder.filter_pack(Map.merge(filters, %{sort: sort}))
217217
|> ORM.paginater(~m(page size)a)
218-
|> set_viewer_emotion_ifneed(user)
219218
|> add_pined_comments_ifneed(thread, article_id, filters)
219+
|> set_viewer_emotion_ifneed(user)
220220
|> mark_viewer_has_upvoted(user)
221221
|> done()
222222
end

lib/groupher_server/cms/delegates/article_curd.ex

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
77
import GroupherServer.CMS.Helper.Matcher2
88
import GroupherServer.CMS.Helper.Matcher, only: [match_action: 2]
99

10-
import Helper.Utils, only: [done: 1, pick_by: 2, integerfy: 1, strip_struct: 1]
10+
import Helper.Utils, only: [done: 1, pick_by: 2, integerfy: 1, strip_struct: 1, get_config: 2]
1111
import Helper.ErrorCode
12+
import ShortMaps
1213

13-
alias Helper.{Later, ORM}
14+
alias Helper.{Later, ORM, QueryBuilder}
1415
alias GroupherServer.{Accounts, CMS, Delivery, Email, Repo, Statistics}
1516

1617
alias Accounts.User
@@ -20,6 +21,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
2021

2122
alias Ecto.Multi
2223

24+
@supported_emotions get_config(:article, :supported_emotions)
2325
@default_emotions Embeds.ArticleEmotion.default_emotions()
2426
@default_article_meta Embeds.ArticleMeta.default_meta()
2527

@@ -50,31 +52,41 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
5052
end
5153

5254
def paged_articles(thread, filter) do
55+
%{page: page, size: size} = filter
56+
5357
with {:ok, info} <- match(thread) do
5458
info.model
5559
|> domain_filter_query(filter)
5660
|> community_with_flag_query(filter)
57-
|> ORM.find_all(filter)
61+
|> QueryBuilder.filter_pack(filter)
62+
|> ORM.paginater(~m(page size)a)
5863
|> add_pin_contents_ifneed(info.model, filter)
64+
|> done()
5965
end
6066
end
6167

6268
def paged_articles(thread, filter, %User{} = user) do
69+
%{page: page, size: size} = filter
70+
6371
with {:ok, info} <- match(thread) do
6472
info.model
6573
|> domain_filter_query(filter)
6674
|> community_with_flag_query(filter)
67-
|> ORM.find_all(filter)
75+
|> QueryBuilder.filter_pack(filter)
76+
|> ORM.paginater(~m(page size)a)
6877
|> add_pin_contents_ifneed(info.model, filter)
78+
|> IO.inspect(label: "before --")
79+
|> set_viewer_emotion_ifneed(user)
6980
|> mark_viewer_has_states(user)
81+
|> done()
7082
end
7183
end
7284

73-
defp mark_viewer_has_states({:ok, %{entries: []} = contents}, _), do: {:ok, contents}
85+
defp mark_viewer_has_states(%{entries: []} = contents, _), do: contents
7486

75-
defp mark_viewer_has_states({:ok, %{entries: entries} = contents}, user) do
87+
defp mark_viewer_has_states(%{entries: entries} = contents, user) do
7688
entries = Enum.map(entries, &Map.merge(&1, do_mark_viewer_has_states(&1.meta, user)))
77-
{:ok, Map.merge(contents, %{entries: entries})}
89+
Map.merge(contents, %{entries: entries})
7890
end
7991

8092
defp mark_viewer_has_states({:error, reason}, _), do: {:error, reason}
@@ -99,6 +111,30 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
99111
}
100112
end
101113

114+
defp user_in_logins?([], _), do: false
115+
defp user_in_logins?(ids_list, %User{login: login}), do: Enum.member?(ids_list, login)
116+
117+
defp set_viewer_emotion_ifneed(paged_articles, nil), do: paged_articles
118+
defp set_viewer_emotion_ifneed(%{entries: []} = paged_articles, _), do: paged_articles
119+
120+
defp set_viewer_emotion_ifneed(%{entries: entries} = paged_articles, %User{} = user) do
121+
new_entries =
122+
Enum.map(entries, fn article ->
123+
update_viewed_status =
124+
@supported_emotions
125+
|> Enum.reduce([], fn emotion, acc ->
126+
already_emotioned = user_in_logins?(article.emotions[:"#{emotion}_user_logins"], user)
127+
acc ++ ["viewer_has_#{emotion}ed": already_emotioned]
128+
end)
129+
|> Enum.into(%{})
130+
131+
updated_emotions = Map.merge(article.emotions, update_viewed_status)
132+
Map.put(article, :emotions, updated_emotions)
133+
end)
134+
135+
%{paged_articles | entries: new_entries}
136+
end
137+
102138
@doc """
103139
Creates a content(post/job ...), and set community.
104140
@@ -303,12 +339,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
303339
|> select([p, c, content], content)
304340
# 10 pined contents per community/thread, at most
305341
|> ORM.paginater(%{page: 1, size: 10})
306-
|> done()
307342

308343
concat_contents(pined_content, normal_contents)
309344
else
310-
_error ->
311-
contents
345+
_error -> contents
312346
end
313347
end
314348

@@ -327,7 +361,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
327361

328362
defp should_add_pin?(_filter), do: {:error, :pass}
329363

330-
defp concat_contents(%{total_count: 0}, normal_contents), do: {:ok, normal_contents}
364+
defp concat_contents(%{total_count: 0}, normal_contents), do: normal_contents
331365

332366
defp concat_contents(pined_content, normal_contents) do
333367
pind_entries =
@@ -349,7 +383,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
349383
# those two are equals
350384
# |> Map.put(:total_count, pind_count + normal_count - pind_count)
351385
|> Map.put(:total_count, normal_count)
352-
|> done
353386
end
354387

355388
defp create_content_result({:ok, %{create_content: result}}) do

0 commit comments

Comments
 (0)