Refactor fetching follow information to a separate function

This commit is contained in:
rinpatch 2019-07-13 23:56:10 +03:00
parent e8fa477793
commit e5b850a991
2 changed files with 34 additions and 18 deletions

View File

@ -16,6 +16,7 @@ defmodule Pleroma.User.Info do
field(:source_data, :map, default: %{}) field(:source_data, :map, default: %{})
field(:note_count, :integer, default: 0) field(:note_count, :integer, default: 0)
field(:follower_count, :integer, default: 0) field(:follower_count, :integer, default: 0)
# Should be filled in only for remote users
field(:following_count, :integer, default: nil) field(:following_count, :integer, default: nil)
field(:locked, :boolean, default: false) field(:locked, :boolean, default: false)
field(:confirmation_pending, :boolean, default: false) field(:confirmation_pending, :boolean, default: false)

View File

@ -1013,17 +1013,15 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
{:ok, user_data} {:ok, user_data}
end end
defp maybe_update_follow_information(data) do def fetch_follow_information_for_user(user) do
with {:enabled, true} <- with {:ok, following_data} <-
{:enabled, Pleroma.Config.get([:instance, :external_user_synchronization])}, Fetcher.fetch_and_contain_remote_object_from_id(user.following_address),
{:ok, following_data} <- following_count when is_integer(following_count) <- following_data["totalItems"],
Fetcher.fetch_and_contain_remote_object_from_id(data.following_address), {:ok, hide_follows} <- collection_private(following_data),
following_count <- following_data["totalItems"],
hide_follows <- collection_private?(following_data),
{:ok, followers_data} <- {:ok, followers_data} <-
Fetcher.fetch_and_contain_remote_object_from_id(data.follower_address), Fetcher.fetch_and_contain_remote_object_from_id(user.follower_address),
followers_count <- followers_data["totalItems"], followers_count when is_integer(followers_count) <- followers_data["totalItems"],
hide_followers <- collection_private?(followers_data) do {:ok, hide_followers} <- collection_private(followers_data) do
info = %{ info = %{
"hide_follows" => hide_follows, "hide_follows" => hide_follows,
"follower_count" => followers_count, "follower_count" => followers_count,
@ -1031,8 +1029,22 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
"hide_followers" => hide_followers "hide_followers" => hide_followers
} }
info = Map.merge(data.info, info) info = Map.merge(user.info, info)
Map.put(data, :info, info) {:ok, Map.put(user, :info, info)}
else
{:error, _} = e ->
e
e ->
{:error, e}
end
end
defp maybe_update_follow_information(data) do
with {:enabled, true} <-
{:enabled, Pleroma.Config.get([:instance, :external_user_synchronization])},
{:ok, data} <- fetch_follow_information_for_user(data) do
data
else else
{:enabled, false} -> {:enabled, false} ->
data data
@ -1046,19 +1058,22 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
end end
end end
defp collection_private?(data) do defp collection_private(data) do
if is_map(data["first"]) and if is_map(data["first"]) and
data["first"]["type"] in ["CollectionPage", "OrderedCollectionPage"] do data["first"]["type"] in ["CollectionPage", "OrderedCollectionPage"] do
false {:ok, false}
else else
with {:ok, _data} <- Fetcher.fetch_and_contain_remote_object_from_id(data["first"]) do with {:ok, _data} <- Fetcher.fetch_and_contain_remote_object_from_id(data["first"]) do
false {:ok, false}
else else
{:error, {:ok, %{status: code}}} when code in [401, 403] -> {:error, {:ok, %{status: code}}} when code in [401, 403] ->
true {:ok, true}
_e -> {:error, _} = e ->
false e
e ->
{:error, e}
end end
end end
end end