From e9de04b74bb772fc1b2bff5201195e63eee198be Mon Sep 17 00:00:00 2001 From: lain Date: Sun, 25 Feb 2018 21:02:44 +0100 Subject: [PATCH] Add support for outgoing update. --- lib/pleroma/web/activity_pub/utils.ex | 2 +- lib/pleroma/web/activity_pub/views/user_view.ex | 2 ++ lib/pleroma/web/common_api/common_api.ex | 4 ++++ .../web/mastodon_api/mastodon_api_controller.ex | 10 +++++++--- lib/pleroma/web/twitter_api/twitter_api_controller.ex | 5 ++++- test/web/activity_pub/activity_pub_test.exs | 3 ++- 6 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/pleroma/web/activity_pub/utils.ex b/lib/pleroma/web/activity_pub/utils.ex index 919cd0bd5..cda106283 100644 --- a/lib/pleroma/web/activity_pub/utils.ex +++ b/lib/pleroma/web/activity_pub/utils.ex @@ -68,7 +68,7 @@ def lazy_put_object_defaults(map) do @doc """ 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 :ok end diff --git a/lib/pleroma/web/activity_pub/views/user_view.ex b/lib/pleroma/web/activity_pub/views/user_view.ex index b96ac7b27..179636884 100644 --- a/lib/pleroma/web/activity_pub/views/user_view.ex +++ b/lib/pleroma/web/activity_pub/views/user_view.ex @@ -1,9 +1,11 @@ defmodule Pleroma.Web.ActivityPub.UserView do use Pleroma.Web, :view alias Pleroma.Web.Salmon + alias Pleroma.Web.WebFinger alias Pleroma.User def render("user.json", %{user: user}) do + {:ok, user} = WebFinger.ensure_keys_present(user) {: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_encode([public_key]) diff --git a/lib/pleroma/web/common_api/common_api.ex b/lib/pleroma/web/common_api/common_api.ex index d85a7cf5e..0f84542f0 100644 --- a/lib/pleroma/web/common_api/common_api.ex +++ b/lib/pleroma/web/common_api/common_api.ex @@ -74,4 +74,8 @@ def post(user, %{"status" => status} = data) do res 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 diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 1f010a8ee..ca1f8154c 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -24,6 +24,7 @@ def create_app(conn, params) do end def update_credentials(%{assigns: %{user: user}} = conn, params) do + original_user = user params = if bio = params["note"] do Map.put(params, "bio", bio) else @@ -40,7 +41,7 @@ def update_credentials(%{assigns: %{user: user}} = conn, params) do with %Plug.Upload{} <- avatar, {:ok, object} <- ActivityPub.upload(avatar), change = Ecto.Changeset.change(user, %{avatar: object.data}), - {:ok, user} = Repo.update(change) do + {:ok, user} = User.update_and_set_cache(change) do user else _e -> user @@ -54,7 +55,7 @@ def update_credentials(%{assigns: %{user: user}} = conn, params) do {:ok, object} <- ActivityPub.upload(banner), new_info <- Map.put(user.info, "banner", object.data), change <- User.info_changeset(user, %{info: new_info}), - {:ok, user} <- Repo.update(change) do + {:ok, user} <- User.update_and_set_cache(change) do user else _e -> user @@ -64,7 +65,10 @@ def update_credentials(%{assigns: %{user: user}} = conn, params) do end 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}) else _e -> diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 3eb4f5d63..848ec218f 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -208,6 +208,7 @@ def update_avatar(%{assigns: %{user: user}} = conn, params) do {:ok, object} = ActivityPub.upload(params) change = Changeset.change(user, %{avatar: object.data}) {:ok, user} = User.update_and_set_cache(change) + CommonAPI.update(user) render(conn, UserView, "show.json", %{user: user, for: user}) end @@ -216,7 +217,8 @@ def update_banner(%{assigns: %{user: user}} = conn, params) do with {:ok, object} <- ActivityPub.upload(%{"img" => params["banner"]}), new_info <- Map.put(user.info, "banner", object.data), 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 response = %{ url: href } |> Poison.encode! conn @@ -306,6 +308,7 @@ def update_profile(%{assigns: %{user: user}} = conn, params) do with changeset <- User.update_changeset(user, params), {:ok, user} <- User.update_and_set_cache(changeset) do + CommonAPI.update(user) render(conn, UserView, "user.json", %{user: user, for: user}) else error -> diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 5ab7c8495..96792bca5 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -47,9 +47,10 @@ test "inserts a given map into the activity database, giving it an id if it has assert activity.data["id"] == given_id 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 = %{ "object" => %{ + "type" => "Note", "ok" => true } }