diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index e544d3772..47aefaeba 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -80,9 +80,15 @@ def remote_user_creation(params) do |> 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 @@ -386,4 +392,9 @@ def get_public_key_for_ap_id(ap_id) do _ -> :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 diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 7b85770b7..4d0de71e4 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -5,6 +5,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do import Pleroma.Web.ActivityPub.Utils require Logger + @httpoison Application.get_env(:pleroma, :httpoison) + def get_recipients(data) do (data["to"] || []) ++ (data["cc"] || []) end @@ -232,4 +234,22 @@ def prepare_incoming(%{"type" => "Create", "object" => %{"type" => "Note"} = obj 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 diff --git a/lib/pleroma/web/ostatus/ostatus.ex b/lib/pleroma/web/ostatus/ostatus.ex index c35ba42be..91c4474c5 100644 --- a/lib/pleroma/web/ostatus/ostatus.ex +++ b/lib/pleroma/web/ostatus/ostatus.ex @@ -218,11 +218,6 @@ def find_or_make_user(uri) do 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 = %{ @@ -236,7 +231,7 @@ def make_user(uri, update \\ false) do 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 diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index a38ca84ad..42d3980ea 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -7,6 +7,18 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do 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)