@@ -180,15 +180,25 @@ defmodule CF.Comments do
180
180
181
181
# Delete prev directly vote if any (without logging a delete action)
182
182
Multi . new ( )
183
- |> Multi . delete_all ( :prev_vote , Vote . user_comment_vote ( user , comment ) , returning: [ :value ] )
183
+ |> Multi . delete_all ( :prev_votes , Vote . user_comment_vote ( user , comment ) , returning: [ :value ] )
184
+ |> Multi . run ( :revert_vote , fn _repo , % { prev_votes: { _count , prev_votes } } ->
185
+ unless Enum . empty? ( prev_votes ) do
186
+ prev_vote = List . first ( prev_votes )
187
+ prev_vote_type = Vote . vote_type ( user , comment , prev_vote . value )
188
+ revert_vote_type = reverse_vote_type ( prev_vote_type )
189
+ DB.Repo . insert ( action_revert_vote ( user . id , video_id , revert_vote_type , comment ) )
190
+ else
191
+ { :ok , nil }
192
+ end
193
+ end )
184
194
|> Multi . insert ( :vote , Vote . changeset_new ( user , comment , value ) )
185
195
|> Multi . insert ( :vote_action , action_vote ( user . id , video_id , vote_type , comment ) )
186
196
|> Repo . transaction ( )
187
197
|> case do
188
- { :ok , % { vote: vote , prev_vote : { 0 , [ ] } } } ->
198
+ { :ok , % { vote: vote , prev_votes : { 0 , [ ] } } } ->
189
199
{ :ok , comment , % { vote | comment_id: comment . id , comment: comment } , 0 }
190
200
191
- { :ok , % { vote: vote , prev_vote : { _ , [ % { value: prev_value } ] } } } ->
201
+ { :ok , % { vote: vote , prev_votes : { _ , [ % { value: prev_value } ] } } } ->
192
202
{ :ok , comment , % { vote | comment_id: comment . id , comment: comment } , prev_value }
193
203
194
204
{ :error , _ , reason , _ } ->
0 commit comments