Commit e9de04b7 authored by lambda's avatar lambda

Add support for outgoing update.

parent 5ea6d96d
Pipeline #846 passed with stage
in 2 minutes and 33 seconds
......@@ -68,7 +68,7 @@ defmodule Pleroma.Web.ActivityPub.Utils 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
......
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])
......
......@@ -74,4 +74,8 @@ defmodule Pleroma.Web.CommonAPI 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
......@@ -24,6 +24,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController 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 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController 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 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController 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 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController 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 ->
......
......@@ -208,6 +208,7 @@ defmodule Pleroma.Web.TwitterAPI.Controller 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 @@ defmodule Pleroma.Web.TwitterAPI.Controller 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 @@ defmodule Pleroma.Web.TwitterAPI.Controller 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 ->
......
......@@ -47,9 +47,10 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
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
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment