user: move initial post fetching to job queue

This commit is contained in:
William Pitcock 2019-05-13 01:58:30 +00:00
parent b14a314ce7
commit 32d4630c9c

View File

@ -552,8 +552,7 @@ defmodule Pleroma.User do
with [_nick, _domain] <- String.split(nickname, "@"), with [_nick, _domain] <- String.split(nickname, "@"),
{:ok, user} <- fetch_by_nickname(nickname) do {:ok, user} <- fetch_by_nickname(nickname) do
if Pleroma.Config.get([:fetch_initial_posts, :enabled]) do if Pleroma.Config.get([:fetch_initial_posts, :enabled]) do
# TODO turn into job fetch_initial_posts(user)
{:ok, _} = Task.start(__MODULE__, :fetch_initial_posts, [user])
end end
{:ok, user} {:ok, user}
@ -564,15 +563,8 @@ defmodule Pleroma.User do
end end
@doc "Fetch some posts when the user has just been federated with" @doc "Fetch some posts when the user has just been federated with"
def fetch_initial_posts(user) do def fetch_initial_posts(user),
pages = Pleroma.Config.get!([:fetch_initial_posts, :pages]) do: PleromaJobQueue.enqueue(:background, __MODULE__, [:fetch_initial_posts, user])
Enum.each(
# Insert all the posts in reverse order, so they're in the right order on the timeline
Enum.reverse(Utils.fetch_ordered_collection(user.info.source_data["outbox"], pages)),
&Pleroma.Web.Federator.incoming_ap_doc/1
)
end
@spec get_followers_query(User.t(), pos_integer() | nil) :: Ecto.Query.t() @spec get_followers_query(User.t(), pos_integer() | nil) :: Ecto.Query.t()
def get_followers_query(%User{} = user, nil) do def get_followers_query(%User{} = user, nil) do
@ -1077,6 +1069,19 @@ defmodule Pleroma.User do
delete_user_activities(user) delete_user_activities(user)
end end
@spec perform(atom(), User.t()) :: {:ok, User.t()}
def perform(:fetch_initial_posts, %User{} = user) do
pages = Pleroma.Config.get!([:fetch_initial_posts, :pages])
Enum.each(
# Insert all the posts in reverse order, so they're in the right order on the timeline
Enum.reverse(Utils.fetch_ordered_collection(user.info.source_data["outbox"], pages)),
&Pleroma.Web.Federator.incoming_ap_doc/1
)
{:ok, user}
end
def delete_user_activities(%User{ap_id: ap_id} = user) do def delete_user_activities(%User{ap_id: ap_id} = user) do
stream = stream =
ap_id ap_id
@ -1130,8 +1135,8 @@ defmodule Pleroma.User do
resp = fetch_by_ap_id(ap_id) resp = fetch_by_ap_id(ap_id)
if should_fetch_initial do if should_fetch_initial do
with {:ok, %User{} = user} = resp do with {:ok, %User{} = user} <- resp do
{:ok, _} = Task.start(__MODULE__, :fetch_initial_posts, [user]) fetch_initial_posts(user)
end end
end end