Add support for outgoing update.

This commit is contained in:
lain 2018-02-25 21:02:44 +01:00
parent 5ea6d96dbe
commit e9de04b74b
6 changed files with 20 additions and 6 deletions

View file

@ -68,7 +68,7 @@ defmodule Pleroma.Web.ActivityPub.Utils do
@doc """ @doc """
Inserts a full object if it is contained in an activity. Inserts a full object if it is contained in an activity.
""" """
def insert_full_object(%{"object" => object_data}) when is_map(object_data) do def insert_full_object(%{"object" => %{"type" => type} = object_data}) when is_map(object_data) and type in ["Note"] do
with {:ok, _} <- Object.create(object_data) do with {:ok, _} <- Object.create(object_data) do
:ok :ok
end end

View file

@ -1,9 +1,11 @@
defmodule Pleroma.Web.ActivityPub.UserView do defmodule Pleroma.Web.ActivityPub.UserView do
use Pleroma.Web, :view use Pleroma.Web, :view
alias Pleroma.Web.Salmon alias Pleroma.Web.Salmon
alias Pleroma.Web.WebFinger
alias Pleroma.User alias Pleroma.User
def render("user.json", %{user: user}) do def render("user.json", %{user: user}) do
{:ok, user} = WebFinger.ensure_keys_present(user)
{:ok, _, public_key} = Salmon.keys_from_pem(user.info["keys"]) {:ok, _, public_key} = Salmon.keys_from_pem(user.info["keys"])
public_key = :public_key.pem_entry_encode(:RSAPublicKey, public_key) public_key = :public_key.pem_entry_encode(:RSAPublicKey, public_key)
public_key = :public_key.pem_encode([public_key]) public_key = :public_key.pem_encode([public_key])

View file

@ -74,4 +74,8 @@ defmodule Pleroma.Web.CommonAPI do
res res
end end
end end
def update(user) do
ActivityPub.update(%{local: true, to: [user.follower_address], cc: [], actor: user.ap_id, object: Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})})
end
end end

View file

@ -24,6 +24,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
end end
def update_credentials(%{assigns: %{user: user}} = conn, params) do def update_credentials(%{assigns: %{user: user}} = conn, params) do
original_user = user
params = if bio = params["note"] do params = if bio = params["note"] do
Map.put(params, "bio", bio) Map.put(params, "bio", bio)
else else
@ -40,7 +41,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
with %Plug.Upload{} <- avatar, with %Plug.Upload{} <- avatar,
{:ok, object} <- ActivityPub.upload(avatar), {:ok, object} <- ActivityPub.upload(avatar),
change = Ecto.Changeset.change(user, %{avatar: object.data}), change = Ecto.Changeset.change(user, %{avatar: object.data}),
{:ok, user} = Repo.update(change) do {:ok, user} = User.update_and_set_cache(change) do
user user
else else
_e -> user _e -> user
@ -54,7 +55,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
{:ok, object} <- ActivityPub.upload(banner), {:ok, object} <- ActivityPub.upload(banner),
new_info <- Map.put(user.info, "banner", object.data), new_info <- Map.put(user.info, "banner", object.data),
change <- User.info_changeset(user, %{info: new_info}), change <- User.info_changeset(user, %{info: new_info}),
{:ok, user} <- Repo.update(change) do {:ok, user} <- User.update_and_set_cache(change) do
user user
else else
_e -> user _e -> user
@ -64,7 +65,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
end end
with changeset <- User.update_changeset(user, params), with changeset <- User.update_changeset(user, params),
{:ok, user} <- Repo.update(changeset) do {:ok, user} <- User.update_and_set_cache(changeset) do
if original_user != user do
CommonAPI.update(user)
end
json conn, AccountView.render("account.json", %{user: user}) json conn, AccountView.render("account.json", %{user: user})
else else
_e -> _e ->

View file

@ -208,6 +208,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
{:ok, object} = ActivityPub.upload(params) {:ok, object} = ActivityPub.upload(params)
change = Changeset.change(user, %{avatar: object.data}) change = Changeset.change(user, %{avatar: object.data})
{:ok, user} = User.update_and_set_cache(change) {:ok, user} = User.update_and_set_cache(change)
CommonAPI.update(user)
render(conn, UserView, "show.json", %{user: user, for: user}) render(conn, UserView, "show.json", %{user: user, for: user})
end end
@ -216,7 +217,8 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
with {:ok, object} <- ActivityPub.upload(%{"img" => params["banner"]}), with {:ok, object} <- ActivityPub.upload(%{"img" => params["banner"]}),
new_info <- Map.put(user.info, "banner", object.data), new_info <- Map.put(user.info, "banner", object.data),
change <- User.info_changeset(user, %{info: new_info}), change <- User.info_changeset(user, %{info: new_info}),
{:ok, _user} <- User.update_and_set_cache(change) do {:ok, user} <- User.update_and_set_cache(change) do
CommonAPI.update(user)
%{"url" => [ %{ "href" => href } | _ ]} = object.data %{"url" => [ %{ "href" => href } | _ ]} = object.data
response = %{ url: href } |> Poison.encode! response = %{ url: href } |> Poison.encode!
conn conn
@ -306,6 +308,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
with changeset <- User.update_changeset(user, params), with changeset <- User.update_changeset(user, params),
{:ok, user} <- User.update_and_set_cache(changeset) do {:ok, user} <- User.update_and_set_cache(changeset) do
CommonAPI.update(user)
render(conn, UserView, "user.json", %{user: user, for: user}) render(conn, UserView, "user.json", %{user: user, for: user})
else else
error -> error ->

View file

@ -47,9 +47,10 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
assert activity.data["id"] == given_id assert activity.data["id"] == given_id
end end
test "adds an id to a given object if it lacks one and inserts it to the object database" do test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
data = %{ data = %{
"object" => %{ "object" => %{
"type" => "Note",
"ok" => true "ok" => true
} }
} }