Use race-condition free following method.
This commit is contained in:
parent
aa98a100b4
commit
935e65e261
|
@ -343,18 +343,17 @@ def follow(%User{} = follower, %User{info: info} = followed) do
|
||||||
Websub.subscribe(follower, followed)
|
Websub.subscribe(follower, followed)
|
||||||
end
|
end
|
||||||
|
|
||||||
following =
|
q =
|
||||||
[ap_followers | follower.following]
|
from(u in User,
|
||||||
|> Enum.uniq()
|
where: u.id == ^follower.id,
|
||||||
|
update: [push: [following: ^ap_followers]]
|
||||||
|
)
|
||||||
|
|
||||||
follower =
|
{1, [follower]} = Repo.update_all(q, [], returning: true)
|
||||||
follower
|
|
||||||
|> follow_changeset(%{following: following})
|
|
||||||
|> update_and_set_cache
|
|
||||||
|
|
||||||
{:ok, _} = update_follower_count(followed)
|
{:ok, _} = update_follower_count(followed)
|
||||||
|
|
||||||
follower
|
set_cache(follower)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -362,17 +361,18 @@ def unfollow(%User{} = follower, %User{} = followed) do
|
||||||
ap_followers = followed.follower_address
|
ap_followers = followed.follower_address
|
||||||
|
|
||||||
if following?(follower, followed) and follower.ap_id != followed.ap_id do
|
if following?(follower, followed) and follower.ap_id != followed.ap_id do
|
||||||
following =
|
q =
|
||||||
follower.following
|
from(u in User,
|
||||||
|> List.delete(ap_followers)
|
where: u.id == ^follower.id,
|
||||||
|
update: [pull: [following: ^ap_followers]]
|
||||||
|
)
|
||||||
|
|
||||||
{:ok, follower} =
|
{1, [follower]} = Repo.update_all(q, [], returning: true)
|
||||||
follower
|
|
||||||
|> follow_changeset(%{following: following})
|
|
||||||
|> update_and_set_cache
|
|
||||||
|
|
||||||
{:ok, followed} = update_follower_count(followed)
|
{:ok, followed} = update_follower_count(followed)
|
||||||
|
|
||||||
|
set_cache(follower)
|
||||||
|
|
||||||
{:ok, follower, Utils.fetch_latest_follow(follower, followed)}
|
{:ok, follower, Utils.fetch_latest_follow(follower, followed)}
|
||||||
else
|
else
|
||||||
{:error, "Not subscribed!"}
|
{:error, "Not subscribed!"}
|
||||||
|
@ -423,12 +423,16 @@ def get_by_guessed_nickname(ap_id) do
|
||||||
get_by_nickname(nickname)
|
get_by_nickname(nickname)
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_and_set_cache(changeset) do
|
def set_cache(user) do
|
||||||
with {:ok, user} <- Repo.update(changeset) do
|
|
||||||
Cachex.put(:user_cache, "ap_id:#{user.ap_id}", user)
|
Cachex.put(:user_cache, "ap_id:#{user.ap_id}", user)
|
||||||
Cachex.put(:user_cache, "nickname:#{user.nickname}", user)
|
Cachex.put(:user_cache, "nickname:#{user.nickname}", user)
|
||||||
Cachex.put(:user_cache, "user_info:#{user.id}", user_info(user))
|
Cachex.put(:user_cache, "user_info:#{user.id}", user_info(user))
|
||||||
{:ok, user}
|
{:ok, user}
|
||||||
|
end
|
||||||
|
|
||||||
|
def update_and_set_cache(changeset) do
|
||||||
|
with {:ok, user} <- Repo.update(changeset) do
|
||||||
|
set_cache(user)
|
||||||
else
|
else
|
||||||
e -> e
|
e -> e
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue