Basic AP user building.

This commit is contained in:
Roger Braun 2018-02-11 17:20:02 +01:00 committed by lain
parent c1d26751e6
commit ae1ec858f4
4 changed files with 47 additions and 9 deletions

View 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

View 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

View 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

View 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)