mirror of
https://github.com/bonfire-networks/bonfire-app.git
synced 2024-05-18 00:52:40 +00:00
Start modularazing the adapter
This commit is contained in:
parent
032b90a391
commit
33fe020c55
|
@ -4,3 +4,6 @@ config :activity_pub, :adapter, Bonfire.ActivityPub.Adapter
|
|||
config :activity_pub, :repo, Bonfire.Repo
|
||||
|
||||
config :nodeinfo, :adapter, Bonfire.NodeinfoAdapter
|
||||
|
||||
config :bonfire, Bonfire.ActivityPub.Adapter,
|
||||
users_module: Bonfire.Me.Identity.Users.ActivityPub
|
||||
|
|
|
@ -1,54 +1,13 @@
|
|||
defmodule Bonfire.ActivityPub.Adapter do
|
||||
@behaviour ActivityPub.Adapter
|
||||
|
||||
alias ActivityPub.Actor
|
||||
alias Bonfire.Me.Identity.Users
|
||||
|
||||
defp format_actor(user) do
|
||||
user = Bonfire.Repo.preload(user, [:actor])
|
||||
ap_base_path = Bonfire.Common.Config.get(:ap_base_path, "/pub")
|
||||
id = Bonfire.Web.Endpoint.url() <> ap_base_path <> "/actors/#{user.character.username}"
|
||||
|
||||
data = %{
|
||||
"type" => "Person",
|
||||
"id" => id,
|
||||
"inbox" => "#{id}/inbox",
|
||||
"outbox" => "#{id}/outbox",
|
||||
"followers" => "#{id}/followers",
|
||||
"following" => "#{id}/following",
|
||||
"preferredUsername" => user.character.username,
|
||||
"name" => user.profile.name,
|
||||
"summary" => Map.get(user.profile, :summary)
|
||||
}
|
||||
|
||||
%Actor{
|
||||
id: user.id,
|
||||
data: data,
|
||||
keys: Bonfire.Common.Utils.maybe_get(user.actor, :signing_key),
|
||||
local: true,
|
||||
ap_id: id,
|
||||
pointer_id: user.id,
|
||||
username: user.character.username,
|
||||
deactivated: false
|
||||
}
|
||||
end
|
||||
|
||||
def get_actor_by_username(username) do
|
||||
with {:ok, user} <- Users.ActivityPub.by_username(username),
|
||||
actor <- format_actor(user) do
|
||||
{:ok, actor}
|
||||
else
|
||||
_ ->
|
||||
{:error, :not_found}
|
||||
end
|
||||
module = Application.get_env(:bonfire, Bonfire.ActivityPub.Adapter)[:users_module]
|
||||
apply(module, :get_actor_by_username, [username])
|
||||
end
|
||||
|
||||
def update_local_actor(actor, params) do
|
||||
with {:ok, user} <- Users.ActivityPub.by_username(actor.username),
|
||||
{:ok, user} <-
|
||||
Users.ActivityPub.update(user, Map.put(params, :actor, %{signing_key: params.keys})),
|
||||
actor <- format_actor(user) do
|
||||
{:ok, actor}
|
||||
end
|
||||
module = Application.get_env(:bonfire, Bonfire.ActivityPub.Adapter)[:users_module]
|
||||
apply(module, :update_local_actor, [actor, params])
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,17 +1,15 @@
|
|||
defmodule Bonfire.ActivityPub.AdapterTest do
|
||||
use Bonfire.DataCase
|
||||
alias Bonfire.Me.Fake
|
||||
alias Bonfire.Me.Identity.{Accounts, Users}
|
||||
alias Bonfire.ActivityPub.Adapter
|
||||
|
||||
describe "actor fetching" do
|
||||
test "by username" do
|
||||
attrs = fake_user!()
|
||||
assert {:ok, actor} = Adapter.get_actor_by_username(attrs.character.username)
|
||||
assert actor.data["summary"] == attrs.profile.summary
|
||||
assert actor.data["preferredUsername"] == attrs.character.username
|
||||
assert actor.data["name"] == attrs.profile.name
|
||||
assert actor.username == attrs.character.username
|
||||
user = fake_user!()
|
||||
assert {:ok, actor} = Adapter.get_actor_by_username(user.character.username)
|
||||
assert actor.data["summary"] == user.profile.summary
|
||||
assert actor.data["preferredUsername"] == user.character.username
|
||||
assert actor.data["name"] == user.profile.name
|
||||
assert actor.username == user.character.username
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,19 +1,17 @@
|
|||
defmodule Bonfire.ActivityPub.IntegrationTest do
|
||||
use Bonfire.ConnCase
|
||||
alias Bonfire.Me.Fake
|
||||
alias Bonfire.Me.Identity.{Accounts, Users}
|
||||
|
||||
test "fetch users from AP API" do
|
||||
attrs = fake_user!()
|
||||
user = fake_user!()
|
||||
|
||||
conn =
|
||||
build_conn()
|
||||
|> get("/pub/actors/#{attrs.character.username}")
|
||||
|> get("/pub/actors/#{user.character.username}")
|
||||
|> response(200)
|
||||
|> Jason.decode!
|
||||
|
||||
assert conn["preferredUsername"] == attrs.character.username
|
||||
assert conn["name"] == attrs.profile.name
|
||||
assert conn["summary"] == attrs.profile.summary
|
||||
assert conn["preferredUsername"] == user.character.username
|
||||
assert conn["name"] == user.profile.name
|
||||
assert conn["summary"] == user.profile.summary
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue