@@ -35,8 +35,8 @@ defmodule GroupherServer.Accounts.Delegate.Fans do
35
35
following_id: target_user . id
36
36
} )
37
37
)
38
- |> Multi . run ( :update_user_meta , fn _ , _ ->
39
- update_follow_meta ( target_user , user_id , :add )
38
+ |> Multi . run ( :update_user_follow_info , fn _ , _ ->
39
+ update_user_follow_info ( target_user , user_id , :add )
40
40
end )
41
41
|> Multi . run ( :add_achievement , fn _ , _ ->
42
42
Accounts . achieve ( % User { id: target_user . id } , :inc , :follow )
@@ -63,8 +63,8 @@ defmodule GroupherServer.Accounts.Delegate.Fans do
63
63
|> Multi . run ( :delete_following , fn _ , _ ->
64
64
ORM . findby_delete! ( UserFollowing , % { user_id: user_id , following_id: target_user . id } )
65
65
end )
66
- |> Multi . run ( :update_user_meta , fn _ , _ ->
67
- update_follow_meta ( target_user , user_id , :remove )
66
+ |> Multi . run ( :update_user_follow_info , fn _ , _ ->
67
+ update_user_follow_info ( target_user , user_id , :remove )
68
68
end )
69
69
|> Multi . run ( :minus_achievement , fn _ , _ ->
70
70
Accounts . achieve ( % User { id: target_user . id } , :dec , :follow )
@@ -78,31 +78,35 @@ defmodule GroupherServer.Accounts.Delegate.Fans do
78
78
end
79
79
80
80
# update follow in user meta
81
- defp update_follow_meta ( % User { } = target_user , user_id , opt ) do
81
+ defp update_user_follow_info ( % User { } = target_user , user_id , opt ) do
82
82
with { :ok , user } <- ORM . find ( User , user_id ) do
83
83
target_user_meta = ensure ( target_user . meta , @ default_user_meta )
84
84
user_meta = ensure ( user . meta , @ default_user_meta )
85
85
86
+ follower_user_ids =
87
+ case opt do
88
+ :add -> ( target_user_meta . follower_user_ids ++ [ user_id ] ) |> Enum . uniq ( )
89
+ :remove -> ( target_user_meta . follower_user_ids -- [ user_id ] ) |> Enum . uniq ( )
90
+ end
91
+
92
+ following_user_ids =
93
+ case opt do
94
+ :add -> ( user_meta . following_user_ids ++ [ target_user . id ] ) |> Enum . uniq ( )
95
+ :remove -> ( user_meta . following_user_ids -- [ target_user . id ] ) |> Enum . uniq ( )
96
+ end
97
+
86
98
Multi . new ( )
87
99
|> Multi . run ( :update_follower_meta , fn _ , _ ->
88
- follower_user_ids =
89
- case opt do
90
- :add -> target_user_meta . follower_user_ids ++ [ user_id ]
91
- :remove -> target_user_meta . follower_user_ids -- [ user_id ]
92
- end
93
-
100
+ followers_count = length ( follower_user_ids )
94
101
meta = Map . merge ( target_user_meta , % { follower_user_ids: follower_user_ids } )
95
- ORM . update_meta ( target_user , meta )
102
+
103
+ ORM . update_meta ( target_user , meta , changes: % { followers_count: followers_count } )
96
104
end )
97
105
|> Multi . run ( :update_following_meta , fn _ , _ ->
98
- following_user_ids =
99
- case opt do
100
- :add -> user_meta . following_user_ids ++ [ target_user . id ]
101
- :remove -> user_meta . following_user_ids -- [ target_user . id ]
102
- end
103
-
106
+ followings_count = length ( following_user_ids )
104
107
meta = Map . merge ( user_meta , % { following_user_ids: following_user_ids } )
105
- ORM . update_meta ( user , meta )
108
+
109
+ ORM . update_meta ( user , meta , changes: % { followings_count: followings_count } )
106
110
end )
107
111
|> Repo . transaction ( )
108
112
|> result ( )
0 commit comments