@@ -22,10 +22,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleReaction do
2222 load_reaction_users ( ArticleUpvote , thread , article_id , filter )
2323 end
2424
25- def collected_users ( thread , article_id , filter ) do
26- load_reaction_users ( ArticleCollect , thread , article_id , filter )
27- end
28-
2925 defp load_reaction_users ( schema , thread , article_id , % { page: page , size: size } = filter ) do
3026 with { :ok , info } <- match ( thread ) do
3127 schema
@@ -36,110 +32,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleReaction do
3632 end
3733 end
3834
39- def collect_article ( thread , article_id , % User { id: user_id } ) do
40- with { :ok , info } <- match ( thread ) ,
41- { :ok , article } <- ORM . find ( info . model , article_id , preload: [ author: :user ] ) do
42- Multi . new ( )
43- |> Multi . run ( :inc_author_achieve , fn _ , _ ->
44- Accounts . achieve ( article . author . user , :inc , :collect )
45- end )
46- |> Multi . run ( :inc_article_collects_count , fn _ , _ ->
47- update_article_upvotes_count ( info , article , :collects_count , :inc )
48- end )
49- |> Multi . run ( :update_article_reaction_user_list , fn _ , _ ->
50- update_article_reaction_user_list ( :collect , article , user_id , :add )
51- end )
52- |> Multi . run ( :create_collect , fn _ , _ ->
53- thread_upcase = thread |> to_string |> String . upcase ( )
54- args = Map . put ( % { user_id: user_id , thread: thread_upcase } , info . foreign_key , article . id )
55-
56- ORM . create ( ArticleCollect , args )
57- end )
58- |> Repo . transaction ( )
59- |> reaction_result ( )
60- end
61- end
62-
63- # 用于在收藏时,用户添加文章到不同的收藏夹中的情况
64- # 如果是同一篇文章,只创建一次,collect_article 不创建记录,只是后续设置不同的收藏夹即可
65- # 如果是第一次收藏,那么才创建文章收藏记录
66- # 避免因为同一篇文章在不同收藏夹内造成的统计和用户成就系统的混乱
67- def collect_article_ifneed ( thread , article_id , % User { id: user_id } = user ) do
68- with findby_args <- collection_findby_args ( thread , article_id , user_id ) do
69- already_collected = ORM . find_by ( ArticleCollect , findby_args )
70-
71- case already_collected do
72- { :ok , article_collect } -> { :ok , article_collect }
73- { :error , _ } -> collect_article ( thread , article_id , user )
74- end
75- end
76- end
77-
78- def undo_collect_article ( thread , article_id , % User { id: user_id } ) do
79- with { :ok , info } <- match ( thread ) ,
80- { :ok , article } <- ORM . find ( info . model , article_id , preload: [ author: :user ] ) do
81- Multi . new ( )
82- |> Multi . run ( :dec_author_achieve , fn _ , _ ->
83- Accounts . achieve ( article . author . user , :dec , :collect )
84- end )
85- |> Multi . run ( :inc_article_collects_count , fn _ , _ ->
86- update_article_upvotes_count ( info , article , :collects_count , :dec )
87- end )
88- |> Multi . run ( :update_article_reaction_user_list , fn _ , _ ->
89- update_article_reaction_user_list ( :collect , article , user_id , :remove )
90- end )
91- |> Multi . run ( :undo_collect , fn _ , _ ->
92- args = Map . put ( % { user_id: user_id } , info . foreign_key , article . id )
93-
94- ORM . findby_delete ( ArticleCollect , args )
95- end )
96- |> Repo . transaction ( )
97- |> reaction_result ( )
98- end
99- end
100-
101- def undo_collect_article_ifneed ( thread , article_id , % User { id: user_id } = user ) do
102- with findby_args <- collection_findby_args ( thread , article_id , user_id ) ,
103- { :ok , article_collect } = ORM . find_by ( ArticleCollect , findby_args ) do
104- case article_collect . collect_folders |> length <= 1 do
105- true -> undo_collect_article ( thread , article_id , user )
106- false -> { :ok , article_collect }
107- end
108- end
109- end
110-
111- defp collection_findby_args ( thread , article_id , user_id ) do
112- with { :ok , info } <- match ( thread ) do
113- thread_upcase = thread |> to_string |> String . upcase ( )
114- % { thread: thread_upcase , user_id: user_id } |> Map . put ( info . foreign_key , article_id )
115- end
116- end
117-
118- def set_collect_folder ( % ArticleCollect { } = collect , folder ) do
119- collect_folders = ( collect . collect_folders ++ [ folder ] ) |> Enum . uniq ( )
120-
121- collect
122- |> Ecto.Changeset . change ( )
123- |> Ecto.Changeset . put_embed ( :collect_folders , collect_folders )
124- |> Repo . update ( )
125- end
126-
127- def undo_set_collect_folder ( % ArticleCollect { } = collect , folder ) do
128- collect_folders = Enum . reject ( collect . collect_folders , & ( & 1 . id == folder . id ) )
129-
130- case collect_folders do
131- # means collect already delete
132- [ ] ->
133- { :ok , :pass }
134-
135- _ ->
136- collect
137- |> Ecto.Changeset . change ( )
138- |> Ecto.Changeset . put_embed ( :collect_folders , collect_folders )
139- |> Repo . update ( )
140- end
141- end
142-
14335 @ doc "upvote to a article-like content"
14436 def upvote_article ( thread , article_id , % User { id: user_id } ) do
14537 with { :ok , info } <- match ( thread ) ,
@@ -164,7 +56,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleReaction do
16456 end
16557 end )
16658 |> Repo . transaction ( )
167- |> reaction_result ( )
59+ |> result ( )
16860 end
16961 end
17062
@@ -186,7 +78,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleReaction do
18678 ORM . find ( info . model , article . id )
18779 end )
18880 |> Repo . transaction ( )
189- |> reaction_result ( )
81+ |> result ( )
19082 end
19183 end
19284
@@ -248,13 +140,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleReaction do
248140 ORM . update_meta ( article , meta )
249141 end
250142
251- defp reaction_result ( { :ok , % { create_upvote: result } } ) , do: result |> done ( )
252- defp reaction_result ( { :ok , % { undo_upvote: result } } ) , do: result |> done ( )
253-
254- defp reaction_result ( { :ok , % { create_collect: result } } ) , do: result |> done ( )
255- defp reaction_result ( { :ok , % { undo_collect: result } } ) , do: result |> done ( )
143+ defp result ( { :ok , % { create_upvote: result } } ) , do: result |> done ( )
144+ defp result ( { :ok , % { undo_upvote: result } } ) , do: result |> done ( )
256145
257- defp reaction_result ( { :error , _ , result , _steps } ) do
146+ defp result ( { :error , _ , result , _steps } ) do
258147 { :error , result }
259148 end
260149end
0 commit comments