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 @@ 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

View File

@ -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])

View File

@ -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

View File

@ -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 ->

View File

@ -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 ->

View File

@ -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
}
}