@@ -71,7 +71,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
71
71
:insert_editor ,
72
72
CommunityEditor . changeset ( % CommunityEditor { } , ~m( user_id community_id title) a )
73
73
)
74
- |> Multi . run ( :update_editors_count , fn _ , _ ->
74
+ |> Multi . run ( :update_community_count , fn _ , _ ->
75
75
with { :ok , community } <- ORM . find ( Community , community_id ) do
76
76
CommunityCURD . update_community_count_field ( community , user_id , :editors_count , :inc )
77
77
end
@@ -92,7 +92,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
92
92
|> Multi . run ( :delete_editor , fn _ , _ ->
93
93
ORM . findby_delete! ( CommunityEditor , ~m( user_id community_id) a )
94
94
end )
95
- |> Multi . run ( :update_editors_count , fn _ , _ ->
95
+ |> Multi . run ( :update_community_count , fn _ , _ ->
96
96
with { :ok , community } <- ORM . find ( Community , community_id ) do
97
97
CommunityCURD . update_community_count_field ( community , user_id , :editors_count , :dec )
98
98
end
@@ -143,16 +143,14 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
143
143
"""
144
144
def unsubscribe_community ( % Community { id: community_id } , % User { id: user_id } ) do
145
145
with { :ok , community } <- ORM . find ( Community , community_id ) ,
146
- true <- community . raw !== "home" ,
147
- { :ok , record } <-
148
- ORM . findby_delete! ( CommunitySubscriber , community_id: community . id , user_id: user_id ) do
146
+ true <- community . raw !== "home" do
149
147
Multi . new ( )
150
- |> Multi . run ( :unsubscribed_community , fn _ , _ ->
151
- ORM . find ( Community , record . community_id )
152
- end )
153
- |> Multi . run ( :update_community_count , fn _ , % { unsubscribed_community: community } ->
148
+ |> Multi . run ( :update_community_count , fn _ , _ ->
154
149
CommunityCURD . update_community_count_field ( community , user_id , :subscribers_count , :dec )
155
150
end )
151
+ |> Multi . run ( :unsubscribed_community , fn _ , _ ->
152
+ ORM . findby_delete! ( CommunitySubscriber , % { community_id: community . id , user_id: user_id } )
153
+ end )
156
154
|> Repo . transaction ( )
157
155
|> result ( )
158
156
else
@@ -170,19 +168,17 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
170
168
remote_ip
171
169
) do
172
170
with { :ok , community } <- ORM . find ( Community , community_id ) ,
173
- true <- community . raw !== "home" ,
174
- { :ok , record } <-
175
- CommunitySubscriber |> ORM . findby_delete! ( community_id: community . id , user_id: user_id ) do
171
+ true <- community . raw !== "home" do
176
172
Multi . new ( )
173
+ |> Multi . run ( :update_community_count , fn _ , _ ->
174
+ CommunityCURD . update_community_count_field ( community , user_id , :subscribers_count , :dec )
175
+ end )
177
176
|> Multi . run ( :unsubscribed_community , fn _ , _ ->
178
- ORM . find ( Community , record . community_id )
177
+ ORM . findby_delete! ( CommunitySubscriber , % { community_id: community . id , user_id: user_id } )
179
178
end )
180
179
|> Multi . run ( :update_community_geo , fn _ , _ ->
181
180
update_community_geo ( community_id , user_id , remote_ip , :dec )
182
181
end )
183
- |> Multi . run ( :update_community_count , fn _ , % { unsubscribed_community: community } ->
184
- CommunityCURD . update_community_count_field ( community , user_id , :subscribers_count , :dec )
185
- end )
186
182
|> Repo . transaction ( )
187
183
|> result ( )
188
184
else
@@ -200,19 +196,17 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
200
196
_remote_ip
201
197
) do
202
198
with { :ok , community } <- ORM . find ( Community , community_id ) ,
203
- true <- community . raw !== "home" ,
204
- { :ok , record } <-
205
- CommunitySubscriber |> ORM . findby_delete! ( community_id: community . id , user_id: user_id ) do
199
+ true <- community . raw !== "home" do
206
200
Multi . new ( )
201
+ |> Multi . run ( :update_community_count , fn _ , _ ->
202
+ CommunityCURD . update_community_count_field ( community , user_id , :subscribers_count , :dec )
203
+ end )
207
204
|> Multi . run ( :unsubscribed_community , fn _ , _ ->
208
- ORM . find ( Community , record . community_id )
205
+ ORM . findby_delete! ( CommunitySubscriber , % { community_id: community . id , user_id: user_id } )
209
206
end )
210
207
|> Multi . run ( :update_community_geo_city , fn _ , _ ->
211
208
update_community_geo_map ( community . id , city , :dec )
212
209
end )
213
- |> Multi . run ( :update_community_count , fn _ , % { unsubscribed_community: community } ->
214
- CommunityCURD . update_community_count_field ( community , user_id , :subscribers_count , :dec )
215
- end )
216
210
|> Repo . transaction ( )
217
211
|> result ( )
218
212
else
@@ -311,7 +305,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
311
305
{ :ok , result }
312
306
end
313
307
314
- defp result ( { :ok , % { update_editors_count :result } } ) do
308
+ defp result ( { :ok , % { update_community_count :result } } ) do
315
309
{ :ok , result }
316
310
end
317
311