forked from AkkomaGang/akkoma
Basic AP user building.
This commit is contained in:
parent
c1d26751e6
commit
ae1ec858f4
4 changed files with 47 additions and 9 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue