|> validate_length(:name, max: 100)
|> put_change(:local, false)
if changes.valid? do
- followers = User.ap_followers(%User{nickname: changes.changes[:nickname]})
- changes
- |> put_change(:follower_address, followers)
+ case changes.changes[:info]["source_data"] do
+ %{"followers" => followers} ->
+ changes
+ |> put_change(:follower_address, followers)
+ _ ->
+ followers = User.ap_followers(%User{nickname: changes.changes[:nickname]})
+ changes
+ |> put_change(:follower_address, followers)
+ end
else
changes
end
_ -> :error
end
end
+
+ def insert_or_update_user(data) do
+ cs = User.remote_user_creation(data)
+ Repo.insert(cs, on_conflict: :replace_all, conflict_target: :nickname)
+ end
end
import Pleroma.Web.ActivityPub.Utils
require Logger
+ @httpoison Application.get_env(:pleroma, :httpoison)
+
def get_recipients(data) do
(data["to"] || []) ++ (data["cc"] || [])
end
def prepare_incoming(_) do
:error
end
+
+ def make_user_from_ap_id(ap_id) do
+ with {:ok, %{status_code: 200, body: body}} <- @httpoison.get(ap_id, ["Accept": "application/activity+json"]),
+ {:ok, data} <- Poison.decode(body)
+ do
+ user_data = %{
+ ap_id: data["id"],
+ info: %{
+ "ap_enabled" => true,
+ "source_data" => data
+ },
+ nickname: "#{data["preferredUsername"]}@#{URI.parse(ap_id).host}",
+ name: data["name"]
+ }
+
+ User.insert_or_update_user(user_data)
+ end
+ end
end
end
end
- def insert_or_update_user(data) do
- cs = User.remote_user_creation(data)
- Repo.insert(cs, on_conflict: :replace_all, conflict_target: :nickname)
- end
-
def make_user(uri, update \\ false) do
with {:ok, info} <- gather_user_info(uri) do
data = %{
with false <- update,
%User{} = user <- User.get_by_ap_id(data.ap_id) do
{:ok, user}
- else _e -> insert_or_update_user(data)
+ else _e -> User.insert_or_update_user(data)
end
end
end
import Pleroma.Factory
+ describe "building a user from his ap id" do
+ test "it returns a user" do
+ user_id = "http://mastodon.example.org/users/admin"
+ {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
+ assert user.ap_id == user_id
+ assert user.nickname == "admin@mastodon.example.org"
+ assert user.info["source_data"]
+ assert user.info["ap_enabled"]
+ assert user.follower_address == "http://mastodon.example.org/users/admin/followers"
+ end
+ end
+
describe "insertion" do
test "returns the activity if one with the same id is already in" do
activity = insert(:note_activity)