2020-09-06 12:42:51 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 13:38:50 -07:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2020-09-06 12:42:51 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.User.Import do
|
|
|
|
use Ecto.Schema
|
|
|
|
|
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.CommonAPI
|
|
|
|
alias Pleroma.Workers.BackgroundWorker
|
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
@spec perform(atom(), User.t(), list()) :: :ok | list() | {:error, any()}
|
|
|
|
def perform(:mutes_import, %User{} = user, [_ | _] = identifiers) do
|
|
|
|
Enum.map(
|
|
|
|
identifiers,
|
|
|
|
fn identifier ->
|
|
|
|
with {:ok, %User{} = muted_user} <- User.get_or_fetch(identifier),
|
|
|
|
{:ok, _} <- User.mute(user, muted_user) do
|
|
|
|
muted_user
|
|
|
|
else
|
|
|
|
error -> handle_error(:mutes_import, identifier, error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform(:blocks_import, %User{} = blocker, [_ | _] = identifiers) do
|
|
|
|
Enum.map(
|
|
|
|
identifiers,
|
|
|
|
fn identifier ->
|
|
|
|
with {:ok, %User{} = blocked} <- User.get_or_fetch(identifier),
|
|
|
|
{:ok, _block} <- CommonAPI.block(blocker, blocked) do
|
|
|
|
blocked
|
|
|
|
else
|
|
|
|
error -> handle_error(:blocks_import, identifier, error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform(:follow_import, %User{} = follower, [_ | _] = identifiers) do
|
|
|
|
Enum.map(
|
|
|
|
identifiers,
|
|
|
|
fn identifier ->
|
|
|
|
with {:ok, %User{} = followed} <- User.get_or_fetch(identifier),
|
2020-12-01 13:17:52 -07:00
|
|
|
{:ok, follower, followed} <- User.maybe_direct_follow(follower, followed),
|
2020-09-06 12:42:51 -06:00
|
|
|
{:ok, _, _, _} <- CommonAPI.follow(follower, followed) do
|
|
|
|
followed
|
|
|
|
else
|
|
|
|
error -> handle_error(:follow_import, identifier, error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform(_, _, _), do: :ok
|
|
|
|
|
|
|
|
defp handle_error(op, user_id, error) do
|
|
|
|
Logger.debug("#{op} failed for #{user_id} with: #{inspect(error)}")
|
|
|
|
error
|
|
|
|
end
|
|
|
|
|
|
|
|
def blocks_import(%User{} = blocker, [_ | _] = identifiers) do
|
|
|
|
BackgroundWorker.enqueue(
|
|
|
|
"blocks_import",
|
2020-09-17 00:13:43 -06:00
|
|
|
%{"user_id" => blocker.id, "identifiers" => identifiers}
|
2020-09-06 12:42:51 -06:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def follow_import(%User{} = follower, [_ | _] = identifiers) do
|
|
|
|
BackgroundWorker.enqueue(
|
|
|
|
"follow_import",
|
2020-09-17 00:13:43 -06:00
|
|
|
%{"user_id" => follower.id, "identifiers" => identifiers}
|
2020-09-06 12:42:51 -06:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def mutes_import(%User{} = user, [_ | _] = identifiers) do
|
|
|
|
BackgroundWorker.enqueue(
|
|
|
|
"mutes_import",
|
|
|
|
%{"user_id" => user.id, "identifiers" => identifiers}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|