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(:note_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(:locked, :boolean, default: false)
field(:confirmation_pending, :boolean, default: false)

View File

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