Refactor Follows/Followers counter syncronization

- Actually sync counters in the database instead of info cache (which got
overriden after user update was finished anyway)
- Add following count field to user info
- Set hide_followers/hide_follows for remote users based on http status
codes for the first collection page
This commit is contained in:
rinpatch 2019-07-13 19:17:57 +03:00
commit e8fa477793
7 changed files with 73 additions and 61 deletions

View file

@ -1013,6 +1013,56 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub 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),
{: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
info = %{
"hide_follows" => hide_follows,
"follower_count" => followers_count,
"following_count" => following_count,
"hide_followers" => hide_followers
}
info = Map.merge(data.info, info)
Map.put(data, :info, info)
else
{:enabled, false} ->
data
e ->
Logger.error(
"Follower/Following counter update for #{data.ap_id} failed.\n" <> inspect(e)
)
data
end
end
defp collection_private?(data) do
if is_map(data["first"]) and
data["first"]["type"] in ["CollectionPage", "OrderedCollectionPage"] do
false
else
with {:ok, _data} <- Fetcher.fetch_and_contain_remote_object_from_id(data["first"]) do
false
else
{:error, {:ok, %{status: code}}} when code in [401, 403] ->
true
_e ->
false
end
end
end
def user_data_from_user_object(data) do
with {:ok, data} <- MRF.filter(data),
{:ok, data} <- object_to_user_data(data) do
@ -1024,7 +1074,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
def fetch_and_prepare_user_from_ap_id(ap_id) do
with {:ok, data} <- Fetcher.fetch_and_contain_remote_object_from_id(ap_id),
{:ok, data} <- user_data_from_user_object(data) do
{:ok, data} <- user_data_from_user_object(data),
data <- maybe_update_follow_information(data) do
{:ok, data}
else
e -> Logger.error("Could not decode user at fetch #{ap_id}, #{inspect(e)}")