Merge branch 'fix/1019-refactor' into 'develop'
Fix/1019 refactor See merge request pleroma/pleroma!1397
This commit is contained in:
commit
e4e3fd7e55
17 changed files with 132 additions and 342 deletions
|
|
@ -1,60 +0,0 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
defmodule Pleroma.User.Synchronization do
|
||||
alias Pleroma.HTTP
|
||||
alias Pleroma.User
|
||||
|
||||
@spec call([User.t()], map(), keyword()) :: {User.t(), map()}
|
||||
def call(users, errors, opts \\ []) do
|
||||
do_call(users, errors, opts)
|
||||
end
|
||||
|
||||
defp do_call([user | []], errors, opts) do
|
||||
updated = fetch_counters(user, errors, opts)
|
||||
{user, updated}
|
||||
end
|
||||
|
||||
defp do_call([user | others], errors, opts) do
|
||||
updated = fetch_counters(user, errors, opts)
|
||||
do_call(others, updated, opts)
|
||||
end
|
||||
|
||||
defp fetch_counters(user, errors, opts) do
|
||||
%{host: host} = URI.parse(user.ap_id)
|
||||
|
||||
info = %{}
|
||||
{following, errors} = fetch_counter(user.ap_id <> "/following", host, errors, opts)
|
||||
info = if following, do: Map.put(info, :following_count, following), else: info
|
||||
|
||||
{followers, errors} = fetch_counter(user.ap_id <> "/followers", host, errors, opts)
|
||||
info = if followers, do: Map.put(info, :follower_count, followers), else: info
|
||||
|
||||
User.set_info_cache(user, info)
|
||||
errors
|
||||
end
|
||||
|
||||
defp available_domain?(domain, errors, opts) do
|
||||
max_retries = Keyword.get(opts, :max_retries, 3)
|
||||
not (Map.has_key?(errors, domain) && errors[domain] >= max_retries)
|
||||
end
|
||||
|
||||
defp fetch_counter(url, host, errors, opts) do
|
||||
with true <- available_domain?(host, errors, opts),
|
||||
{:ok, %{body: body, status: code}} when code in 200..299 <-
|
||||
HTTP.get(
|
||||
url,
|
||||
[{:Accept, "application/activity+json"}]
|
||||
),
|
||||
{:ok, data} <- Jason.decode(body) do
|
||||
{data["totalItems"], errors}
|
||||
else
|
||||
false ->
|
||||
{nil, errors}
|
||||
|
||||
_ ->
|
||||
{nil, Map.update(errors, host, 1, &(&1 + 1))}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,32 +0,0 @@
|
|||
# Pleroma: A lightweight social networking server
|
||||
# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
|
||||
# SPDX-License-Identifier: AGPL-3.0-onl
|
||||
|
||||
defmodule Pleroma.User.SynchronizationWorker do
|
||||
use GenServer
|
||||
|
||||
def start_link do
|
||||
config = Pleroma.Config.get([:instance, :external_user_synchronization])
|
||||
|
||||
if config[:enabled] do
|
||||
GenServer.start_link(__MODULE__, interval: config[:interval])
|
||||
else
|
||||
:ignore
|
||||
end
|
||||
end
|
||||
|
||||
def init(opts) do
|
||||
schedule_next(opts)
|
||||
{:ok, opts}
|
||||
end
|
||||
|
||||
def handle_info(:sync_follow_counters, opts) do
|
||||
Pleroma.User.sync_follow_counter()
|
||||
schedule_next(opts)
|
||||
{:noreply, opts}
|
||||
end
|
||||
|
||||
defp schedule_next(opts) do
|
||||
Process.send_after(self(), :sync_follow_counters, opts[:interval])
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue