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

Commit 53d24c1

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

File tree

6 files changed

+213
-262
lines changed

6 files changed

+213
-262
lines changed

lib/groupher_server/cms/delegates/article_comment.ex

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
66
import Helper.Utils, only: [done: 1, get_config: 2]
77
import Helper.ErrorCode
88

9+
import GroupherServer.CMS.Delegate.Helper, only: [mark_viewer_emotion_states: 3]
910
import GroupherServer.CMS.Helper.Matcher2
1011
import ShortMaps
1112

@@ -216,7 +217,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
216217
|> QueryBuilder.filter_pack(Map.merge(filters, %{sort: sort}))
217218
|> ORM.paginater(~m(page size)a)
218219
|> add_pined_comments_ifneed(thread, article_id, filters)
219-
|> set_viewer_emotion_ifneed(user)
220+
|> mark_viewer_emotion_states(user, :comment)
220221
|> mark_viewer_has_upvoted(user)
221222
|> done()
222223
end
@@ -233,7 +234,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
233234
|> where(^where_query)
234235
|> QueryBuilder.filter_pack(filters)
235236
|> ORM.paginater(~m(page size)a)
236-
|> set_viewer_emotion_ifneed(user)
237+
|> mark_viewer_emotion_states(user, :comment)
237238
|> mark_viewer_has_upvoted(user)
238239
|> done()
239240
end
@@ -273,30 +274,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
273274

274275
defp add_pined_comments_ifneed(paged_comments, _thread, _article_id, _), do: paged_comments
275276

276-
defp user_in_logins?([], _), do: false
277-
defp user_in_logins?(ids_list, %User{login: login}), do: Enum.member?(ids_list, login)
278-
279-
defp set_viewer_emotion_ifneed(paged_comments, nil), do: paged_comments
280-
defp set_viewer_emotion_ifneed(%{entries: []} = paged_comments, _), do: paged_comments
281-
282-
defp set_viewer_emotion_ifneed(%{entries: entries} = paged_comments, %User{} = user) do
283-
new_entries =
284-
Enum.map(entries, fn comment ->
285-
update_viewed_status =
286-
@supported_emotions
287-
|> Enum.reduce([], fn emotion, acc ->
288-
already_emotioned = user_in_logins?(comment.emotions[:"#{emotion}_user_logins"], user)
289-
acc ++ ["viewer_has_#{emotion}ed": already_emotioned]
290-
end)
291-
|> Enum.into(%{})
292-
293-
updated_emotions = Map.merge(comment.emotions, update_viewed_status)
294-
Map.put(comment, :emotions, updated_emotions)
295-
end)
296-
297-
%{paged_comments | entries: new_entries}
298-
end
299-
300277
defp mark_viewer_has_upvoted(paged_comments, nil), do: paged_comments
301278

302279
defp mark_viewer_has_upvoted(%{entries: entries} = paged_comments, %User{} = user) do

lib/groupher_server/cms/delegates/article_comment_emotion.ex

Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentEmotion do
44
"""
55
import Ecto.Query, warn: false
66

7+
import GroupherServer.CMS.Delegate.Helper, only: [update_emotions_field: 4]
8+
79
alias Helper.ORM
810
alias GroupherServer.{Accounts, CMS, Repo}
911

@@ -15,8 +17,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentEmotion do
1517
@type t_user_list :: [%{login: String.t()}]
1618
@type t_mention_status :: %{user_list: t_user_list, user_count: Integer.t()}
1719

18-
@max_latest_emotion_users_count ArticleComment.max_latest_emotion_users_count()
19-
2020
@doc "make emotion to a comment"
2121
def emotion_to_comment(comment_id, emotion, %User{} = user) do
2222
with {:ok, comment} <- ORM.find(ArticleComment, comment_id, preload: :author) do
@@ -31,18 +31,18 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentEmotion do
3131
args = Map.put(target, :"#{emotion}", true)
3232

3333
case ORM.find_by(ArticleCommentUserEmotion, target) do
34-
{:ok, article_comment_user_emotion} -> article_comment_user_emotion |> ORM.update(args)
35-
{:error, _} -> ArticleCommentUserEmotion |> ORM.create(args)
34+
{:ok, article_comment_user_emotion} -> ORM.update(article_comment_user_emotion, args)
35+
{:error, _} -> ORM.create(ArticleCommentUserEmotion, args)
3636
end
3737
end)
38-
|> Multi.run(:query_emotion_status, fn _, _ ->
39-
query_emotion_status(comment, emotion)
38+
|> Multi.run(:query_emotion_states, fn _, _ ->
39+
query_emotion_states(comment, emotion)
4040
end)
41-
|> Multi.run(:update_comment_emotion, fn _, %{query_emotion_status: status} ->
42-
update_comment_emotion(comment, emotion, status, user)
41+
|> Multi.run(:update_emotions_field, fn _, %{query_emotion_states: status} ->
42+
update_emotions_field(comment, emotion, status, user)
4343
end)
4444
|> Repo.transaction()
45-
|> update_emotion_result
45+
|> update_emotions_field_result
4646
end
4747
end
4848

@@ -60,19 +60,19 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentEmotion do
6060
args = Map.put(target, :"#{emotion}", false)
6161
article_comment_user_emotion |> ORM.update(args)
6262
end)
63-
|> Multi.run(:query_emotion_status, fn _, _ ->
64-
query_emotion_status(comment, emotion)
63+
|> Multi.run(:query_emotion_states, fn _, _ ->
64+
query_emotion_states(comment, emotion)
6565
end)
66-
|> Multi.run(:update_comment_emotion, fn _, %{query_emotion_status: status} ->
67-
update_comment_emotion(comment, emotion, status, user)
66+
|> Multi.run(:update_emotions_field, fn _, %{query_emotion_states: status} ->
67+
update_emotions_field(comment, emotion, status, user)
6868
end)
6969
|> Repo.transaction()
70-
|> update_emotion_result
70+
|> update_emotions_field_result
7171
end
7272
end
7373

74-
@spec query_emotion_status(ArticleComment.t(), Atom.t()) :: {:ok, t_mention_status}
75-
defp query_emotion_status(comment, emotion) do
74+
@spec query_emotion_states(ArticleComment.t(), Atom.t()) :: {:ok, t_mention_status}
75+
defp query_emotion_states(comment, emotion) do
7676
# 每次被 emotion 动作触发后重新查询,主要原因
7777
# 1.并发下保证数据准确,类似 views 阅读数的统计
7878
# 2. 前端使用 nickname 而非 login 展示,如果用户改了 nickname, 可以"自动纠正"
@@ -91,41 +91,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentEmotion do
9191
{:ok, %{user_list: emotioned_user_info_list, user_count: emotioned_user_count}}
9292
end
9393

94-
@spec update_comment_emotion(ArticleComment.t(), Atom.t(), t_mention_status, User.t()) ::
95-
{:ok, ArticleComment.t()} | {:error, any}
96-
defp update_comment_emotion(comment, emotion, status, user) do
97-
%{user_count: user_count, user_list: user_list} = status
98-
99-
emotions =
100-
%{}
101-
|> Map.put(:"#{emotion}_count", user_count)
102-
|> Map.put(:"#{emotion}_user_logins", user_list |> Enum.map(& &1.login))
103-
|> Map.put(
104-
:"latest_#{emotion}_users",
105-
Enum.slice(user_list, 0, @max_latest_emotion_users_count)
106-
)
107-
108-
viewer_has_emotioned = user.login in Map.get(emotions, :"#{emotion}_user_logins")
109-
emotions = emotions |> Map.put(:"viewer_has_#{emotion}ed", viewer_has_emotioned)
110-
111-
comment
112-
|> Ecto.Changeset.change()
113-
|> Ecto.Changeset.put_embed(:emotions, emotions)
114-
|> Repo.update()
115-
# virtual field can not be updated
116-
|> add_viewer_emotioned_ifneed(emotions)
117-
end
118-
119-
defp add_viewer_emotioned_ifneed({:error, error}, _), do: {:error, error}
120-
121-
defp add_viewer_emotioned_ifneed({:ok, comment}, emotions) do
122-
# Map.merge(comment, %{emotion: emotions})
123-
{:ok, Map.merge(comment, %{emotion: emotions})}
124-
end
125-
126-
defp update_emotion_result({:ok, %{update_comment_emotion: result}}), do: {:ok, result}
94+
defp update_emotions_field_result({:ok, %{update_emotions_field: result}}), do: {:ok, result}
12795

128-
defp update_emotion_result({:error, _, result, _steps}) do
96+
defp update_emotions_field_result({:error, _, result, _steps}) do
12997
{:error, result}
13098
end
13199
end

lib/groupher_server/cms/delegates/article_curd.ex

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ 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, get_config: 2]
10+
import Helper.Utils, only: [done: 1, pick_by: 2, integerfy: 1, strip_struct: 1]
11+
import GroupherServer.CMS.Delegate.Helper, only: [mark_viewer_emotion_states: 2]
1112
import Helper.ErrorCode
1213
import ShortMaps
1314

@@ -21,7 +22,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
2122

2223
alias Ecto.Multi
2324

24-
@supported_emotions get_config(:article, :supported_emotions)
2525
@default_emotions Embeds.ArticleEmotion.default_emotions()
2626
@default_article_meta Embeds.ArticleMeta.default_meta()
2727

@@ -75,8 +75,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
7575
|> QueryBuilder.filter_pack(filter)
7676
|> ORM.paginater(~m(page size)a)
7777
|> add_pin_contents_ifneed(info.model, filter)
78-
|> IO.inspect(label: "before --")
79-
|> set_viewer_emotion_ifneed(user)
78+
|> mark_viewer_emotion_states(user)
8079
|> mark_viewer_has_states(user)
8180
|> done()
8281
end
@@ -111,30 +110,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
111110
}
112111
end
113112

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-
138113
@doc """
139114
Creates a content(post/job ...), and set community.
140115

lib/groupher_server/cms/delegates/article_emotion.ex

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleEmotion do
55
import Ecto.Query, warn: false
66
import GroupherServer.CMS.Helper.Matcher2
77

8+
import GroupherServer.CMS.Delegate.Helper, only: [update_emotions_field: 4]
9+
810
alias Helper.ORM
911
alias GroupherServer.{Accounts, CMS, Repo}
1012

@@ -16,9 +18,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleEmotion do
1618
@type t_user_list :: [%{login: String.t()}]
1719
@type t_mention_status :: %{user_list: t_user_list, user_count: Integer.t()}
1820

19-
# ArticleComment.max_latest_emotion_users_count()
20-
@max_latest_emotion_users_count 4
21-
2221
@doc "make emotion to a comment"
2322
def emotion_to_article(thread, article_id, emotion, %User{} = user) do
2423
with {:ok, info} <- match(thread),
@@ -32,18 +31,18 @@ defmodule GroupherServer.CMS.Delegate.ArticleEmotion do
3231
args = Map.put(target, :"#{emotion}", true)
3332

3433
case ORM.find_by(ArticleUserEmotion, target) do
35-
{:ok, article_user_emotion} -> article_user_emotion |> ORM.update(args)
36-
{:error, _} -> ArticleUserEmotion |> ORM.create(args)
34+
{:ok, article_user_emotion} -> ORM.update(article_user_emotion, args)
35+
{:error, _} -> ORM.create(ArticleUserEmotion, args)
3736
end
3837
end)
3938
|> Multi.run(:query_emotion_status, fn _, _ ->
4039
query_emotion_status(thread, article.id, emotion)
4140
end)
42-
|> Multi.run(:update_emotion, fn _, %{query_emotion_status: status} ->
43-
update_emotion(article, emotion, status, user)
41+
|> Multi.run(:update_emotions_field, fn _, %{query_emotion_status: status} ->
42+
update_emotions_field(article, emotion, status, user)
4443
end)
4544
|> Repo.transaction()
46-
|> update_emotion_result
45+
|> update_emotions_field_result
4746
end
4847
end
4948

@@ -53,7 +52,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleEmotion do
5352
Multi.new()
5453
|> Multi.run(:update_user_emotion, fn _, _ ->
5554
target =
56-
%{recived_user_id: article.author.id, user_id: user.id}
55+
%{recived_user_id: article.author.user_id, user_id: user.id}
5756
|> Map.put(info.foreign_key, article_id)
5857

5958
{:ok, article_user_emotion} = ORM.find_by(ArticleUserEmotion, target)
@@ -63,11 +62,11 @@ defmodule GroupherServer.CMS.Delegate.ArticleEmotion do
6362
|> Multi.run(:query_emotion_status, fn _, _ ->
6463
query_emotion_status(thread, article.id, emotion)
6564
end)
66-
|> Multi.run(:update_emotion, fn _, %{query_emotion_status: status} ->
67-
update_emotion(article, emotion, status, user)
65+
|> Multi.run(:update_emotions_field, fn _, %{query_emotion_status: status} ->
66+
update_emotions_field(article, emotion, status, user)
6867
end)
6968
|> Repo.transaction()
70-
|> update_emotion_result
69+
|> update_emotions_field_result
7170
end
7271
end
7372

@@ -93,41 +92,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleEmotion do
9392
end
9493
end
9594

96-
@spec update_emotion(ArticleComment.t(), Atom.t(), t_mention_status, User.t()) ::
97-
{:ok, ArticleComment.t()} | {:error, any}
98-
defp update_emotion(comment, emotion, status, user) do
99-
%{user_count: user_count, user_list: user_list} = status
100-
101-
emotions =
102-
%{}
103-
|> Map.put(:"#{emotion}_count", user_count)
104-
|> Map.put(:"#{emotion}_user_logins", user_list |> Enum.map(& &1.login))
105-
|> Map.put(
106-
:"latest_#{emotion}_users",
107-
Enum.slice(user_list, 0, @max_latest_emotion_users_count)
108-
)
109-
110-
viewer_has_emotioned = user.login in Map.get(emotions, :"#{emotion}_user_logins")
111-
emotions = emotions |> Map.put(:"viewer_has_#{emotion}ed", viewer_has_emotioned)
112-
113-
comment
114-
|> Ecto.Changeset.change()
115-
|> Ecto.Changeset.put_embed(:emotions, emotions)
116-
|> Repo.update()
117-
# virtual field can not be updated
118-
|> add_viewer_emotioned_ifneed(emotions)
119-
end
120-
121-
defp add_viewer_emotioned_ifneed({:error, error}, _), do: {:error, error}
122-
123-
defp add_viewer_emotioned_ifneed({:ok, comment}, emotions) do
124-
# Map.merge(comment, %{emotion: emotions})
125-
{:ok, Map.merge(comment, %{emotion: emotions})}
126-
end
127-
128-
defp update_emotion_result({:ok, %{update_emotion: result}}), do: {:ok, result}
95+
defp update_emotions_field_result({:ok, %{update_emotions_field: result}}), do: {:ok, result}
12996

130-
defp update_emotion_result({:error, _, result, _steps}) do
97+
defp update_emotions_field_result({:error, _, result, _steps}) do
13198
{:error, result}
13299
end
133100
end

0 commit comments

Comments
 (0)