Basic AP user building.
authorRoger Braun <roger@rogerbraun.net>
Sun, 11 Feb 2018 16:20:02 +0000 (17:20 +0100)
committerlain <lain@soykaf.club>
Sun, 11 Feb 2018 16:20:43 +0000 (17:20 +0100)
lib/pleroma/user.ex
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/ostatus/ostatus.ex
test/web/activity_pub/activity_pub_test.exs

index e544d3772451d2378bc14781fd00e7996c76ab23..47aefaebabc178f3621ddf90995d2938855a246f 100644 (file)
@@ -80,9 +80,15 @@ defmodule Pleroma.User 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 @@ defmodule Pleroma.User 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
index 7b85770b71b9f62d55740172f7c5384b21c47dfa..4d0de71e467ea81a3d8143e47ba47eb24ac977d1 100644 (file)
@@ -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 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
   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
index c35ba42bee6d5bc263d72da8769bbc1128eee5cd..91c4474c52d7618c0745efca6b8aff3028071727 100644 (file)
@@ -218,11 +218,6 @@ defmodule Pleroma.Web.OStatus 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 @@ defmodule Pleroma.Web.OStatus 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
index a38ca84ad5b785af5e479beaec1a52258a618d1b..42d3980ea956c5a3b7692332dc3bf38b9afcd11a 100644 (file)
@@ -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)